blob: cfb370d0e8e90d8ff369609a123d5f16998ef659 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- CommandObjectProcess.cpp --------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010// C Includes
11// C++ Includes
12// Other libraries and framework includes
13// Project includes
Eugene Zelenko49bcfd82016-02-23 01:43:44 +000014#include "CommandObjectProcess.h"
Jim Ingham0e410842012-08-11 01:27:55 +000015#include "lldb/Breakpoint/Breakpoint.h"
16#include "lldb/Breakpoint/BreakpointLocation.h"
17#include "lldb/Breakpoint/BreakpointSite.h"
Greg Clayton1f746072012-08-29 21:13:06 +000018#include "lldb/Core/Module.h"
Greg Claytona2715cf2014-06-13 00:54:12 +000019#include "lldb/Core/PluginManager.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000020#include "lldb/Core/State.h"
Greg Clayton7260f622011-04-18 08:33:37 +000021#include "lldb/Host/Host.h"
Zachary Turner3eb2b442017-03-22 23:33:16 +000022#include "lldb/Host/OptionParser.h"
Vince Harron5275aaa2015-01-15 20:08:35 +000023#include "lldb/Host/StringConvert.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Interpreter/CommandInterpreter.h"
25#include "lldb/Interpreter/CommandReturnObject.h"
Pavel Labath47cbf4a2018-04-10 09:03:59 +000026#include "lldb/Interpreter/OptionArgParser.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000027#include "lldb/Interpreter/Options.h"
Greg Claytone996fd32011-03-08 22:40:15 +000028#include "lldb/Target/Platform.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Target/Process.h"
Jim Ingham0e410842012-08-11 01:27:55 +000030#include "lldb/Target/StopInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Target/Target.h"
32#include "lldb/Target/Thread.h"
Zachary Turner93749ab2015-03-03 21:51:25 +000033#include "lldb/Target/UnixSignals.h"
Pavel Labath145d95c2018-04-17 18:53:35 +000034#include "lldb/Utility/Args.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035
36using namespace lldb;
37using namespace lldb_private;
38
Kate Stoneb9c1b512016-09-06 20:57:50 +000039class CommandObjectProcessLaunchOrAttach : public CommandObjectParsed {
Jim Inghamdcb1d852013-03-29 00:56:30 +000040public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000041 CommandObjectProcessLaunchOrAttach(CommandInterpreter &interpreter,
42 const char *name, const char *help,
43 const char *syntax, uint32_t flags,
44 const char *new_process_action)
45 : CommandObjectParsed(interpreter, name, help, syntax, flags),
46 m_new_process_action(new_process_action) {}
Eugene Zelenko49bcfd82016-02-23 01:43:44 +000047
Kate Stoneb9c1b512016-09-06 20:57:50 +000048 ~CommandObjectProcessLaunchOrAttach() override = default;
Eugene Zelenko49bcfd82016-02-23 01:43:44 +000049
Jim Inghamdcb1d852013-03-29 00:56:30 +000050protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +000051 bool StopProcessIfNecessary(Process *process, StateType &state,
52 CommandReturnObject &result) {
53 state = eStateInvalid;
54 if (process) {
55 state = process->GetState();
Eugene Zelenko49bcfd82016-02-23 01:43:44 +000056
Kate Stoneb9c1b512016-09-06 20:57:50 +000057 if (process->IsAlive() && state != eStateConnected) {
58 char message[1024];
59 if (process->GetState() == eStateAttaching)
60 ::snprintf(message, sizeof(message),
61 "There is a pending attach, abort it and %s?",
62 m_new_process_action.c_str());
63 else if (process->GetShouldDetach())
64 ::snprintf(message, sizeof(message),
65 "There is a running process, detach from it and %s?",
66 m_new_process_action.c_str());
67 else
68 ::snprintf(message, sizeof(message),
69 "There is a running process, kill it and %s?",
70 m_new_process_action.c_str());
71
72 if (!m_interpreter.Confirm(message, true)) {
73 result.SetStatus(eReturnStatusFailed);
74 return false;
75 } else {
76 if (process->GetShouldDetach()) {
77 bool keep_stopped = false;
Zachary Turner97206d52017-05-12 04:51:55 +000078 Status detach_error(process->Detach(keep_stopped));
Kate Stoneb9c1b512016-09-06 20:57:50 +000079 if (detach_error.Success()) {
80 result.SetStatus(eReturnStatusSuccessFinishResult);
81 process = nullptr;
82 } else {
83 result.AppendErrorWithFormat(
84 "Failed to detach from process: %s\n",
85 detach_error.AsCString());
86 result.SetStatus(eReturnStatusFailed);
87 }
88 } else {
Zachary Turner97206d52017-05-12 04:51:55 +000089 Status destroy_error(process->Destroy(false));
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 if (destroy_error.Success()) {
91 result.SetStatus(eReturnStatusSuccessFinishResult);
92 process = nullptr;
93 } else {
94 result.AppendErrorWithFormat("Failed to kill process: %s\n",
95 destroy_error.AsCString());
96 result.SetStatus(eReturnStatusFailed);
97 }
98 }
99 }
100 }
101 }
102 return result.Succeeded();
103 }
104
105 std::string m_new_process_action;
Jim Inghamdcb1d852013-03-29 00:56:30 +0000106};
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000107
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108//-------------------------------------------------------------------------
109// CommandObjectProcessLaunch
110//-------------------------------------------------------------------------
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000111#pragma mark CommandObjectProcessLaunch
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112class CommandObjectProcessLaunch : public CommandObjectProcessLaunchOrAttach {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114 CommandObjectProcessLaunch(CommandInterpreter &interpreter)
115 : CommandObjectProcessLaunchOrAttach(
116 interpreter, "process launch",
117 "Launch the executable in the debugger.", nullptr,
118 eCommandRequiresTarget, "restart"),
119 m_options() {
120 CommandArgumentEntry arg;
121 CommandArgumentData run_args_arg;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000122
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 // Define the first (and only) variant of this arg.
124 run_args_arg.arg_type = eArgTypeRunArgs;
125 run_args_arg.arg_repetition = eArgRepeatOptional;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127 // There is only one variant this argument could be; put it into the
128 // argument entry.
129 arg.push_back(run_args_arg);
Todd Fialae1cfbc72016-08-11 23:51:28 +0000130
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131 // Push the data for the first argument into the m_arguments vector.
132 m_arguments.push_back(arg);
133 }
Jim Inghame9ce62b2012-08-10 21:48:41 +0000134
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135 ~CommandObjectProcessLaunch() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000136
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137 int HandleArgumentCompletion(Args &input, int &cursor_index,
138 int &cursor_char_position,
139 OptionElementVector &opt_element_vector,
140 int match_start_point, int max_return_elements,
141 bool &word_complete,
142 StringList &matches) override {
143 std::string completion_str(input.GetArgumentAtIndex(cursor_index));
144 completion_str.erase(cursor_char_position);
145
146 CommandCompletions::InvokeCommonCompletionCallbacks(
147 GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
148 completion_str.c_str(), match_start_point, max_return_elements, nullptr,
149 word_complete, matches);
150 return matches.GetSize();
151 }
152
153 Options *GetOptions() override { return &m_options; }
154
155 const char *GetRepeatCommand(Args &current_command_args,
156 uint32_t index) override {
157 // No repeat for "process launch"...
158 return "";
159 }
Jim Ingham5a988412012-06-08 21:56:10 +0000160
161protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 bool DoExecute(Args &launch_args, CommandReturnObject &result) override {
163 Debugger &debugger = m_interpreter.GetDebugger();
164 Target *target = debugger.GetSelectedTarget().get();
165 // If our listener is nullptr, users aren't allows to launch
166 ModuleSP exe_module_sp = target->GetExecutableModule();
Greg Clayton71337622011-02-24 22:24:29 +0000167
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168 if (exe_module_sp == nullptr) {
169 result.AppendError("no file in target, create a debug target using the "
170 "'target create' command");
171 result.SetStatus(eReturnStatusFailed);
172 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000173 }
174
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175 StateType state = eStateInvalid;
176
177 if (!StopProcessIfNecessary(m_exe_ctx.GetProcessPtr(), state, result))
178 return false;
179
Zachary Turner31d97a52016-11-17 18:08:12 +0000180 llvm::StringRef target_settings_argv0 = target->GetArg0();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181
182 // Determine whether we will disable ASLR or leave it in the default state
Adrian Prantl05097242018-04-30 16:49:04 +0000183 // (i.e. enabled if the platform supports it). First check if the process
184 // launch options explicitly turn on/off
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185 // disabling ASLR. If so, use that setting;
186 // otherwise, use the 'settings target.disable-aslr' setting.
187 bool disable_aslr = false;
188 if (m_options.disable_aslr != eLazyBoolCalculate) {
Adrian Prantl05097242018-04-30 16:49:04 +0000189 // The user specified an explicit setting on the process launch line.
190 // Use it.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191 disable_aslr = (m_options.disable_aslr == eLazyBoolYes);
192 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000193 // The user did not explicitly specify whether to disable ASLR. Fall
194 // back to the target.disable-aslr setting.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195 disable_aslr = target->GetDisableASLR();
196 }
197
198 if (disable_aslr)
199 m_options.launch_info.GetFlags().Set(eLaunchFlagDisableASLR);
200 else
201 m_options.launch_info.GetFlags().Clear(eLaunchFlagDisableASLR);
202
203 if (target->GetDetachOnError())
204 m_options.launch_info.GetFlags().Set(eLaunchFlagDetachOnError);
205
206 if (target->GetDisableSTDIO())
207 m_options.launch_info.GetFlags().Set(eLaunchFlagDisableSTDIO);
208
Pavel Labath62930e52018-01-10 11:57:31 +0000209 m_options.launch_info.GetEnvironment() = target->GetEnvironment();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210
Zachary Turner31d97a52016-11-17 18:08:12 +0000211 if (!target_settings_argv0.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212 m_options.launch_info.GetArguments().AppendArgument(
Zachary Turner31d97a52016-11-17 18:08:12 +0000213 target_settings_argv0);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214 m_options.launch_info.SetExecutableFile(
215 exe_module_sp->GetPlatformFileSpec(), false);
216 } else {
217 m_options.launch_info.SetExecutableFile(
218 exe_module_sp->GetPlatformFileSpec(), true);
219 }
220
221 if (launch_args.GetArgumentCount() == 0) {
222 m_options.launch_info.GetArguments().AppendArguments(
223 target->GetProcessLaunchInfo().GetArguments());
224 } else {
225 m_options.launch_info.GetArguments().AppendArguments(launch_args);
226 // Save the arguments for subsequent runs in the current target.
227 target->SetRunArguments(launch_args);
228 }
229
230 StreamString stream;
Zachary Turner97206d52017-05-12 04:51:55 +0000231 Status error = target->Launch(m_options.launch_info, &stream);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000232
233 if (error.Success()) {
234 ProcessSP process_sp(target->GetProcessSP());
235 if (process_sp) {
236 // There is a race condition where this thread will return up the call
Adrian Prantl05097242018-04-30 16:49:04 +0000237 // stack to the main command handler and show an (lldb) prompt before
238 // HandlePrivateEvent (from PrivateStateThread) has a chance to call
239 // PushProcessIOHandler().
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240 process_sp->SyncIOHandler(0, 2000);
241
Zachary Turnerc1564272016-11-16 21:15:24 +0000242 llvm::StringRef data = stream.GetString();
243 if (!data.empty())
244 result.AppendMessage(data);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000245 const char *archname =
246 exe_module_sp->GetArchitecture().GetArchitectureName();
247 result.AppendMessageWithFormat(
248 "Process %" PRIu64 " launched: '%s' (%s)\n", process_sp->GetID(),
249 exe_module_sp->GetFileSpec().GetPath().c_str(), archname);
250 result.SetStatus(eReturnStatusSuccessFinishResult);
251 result.SetDidChangeProcessState(true);
252 } else {
253 result.AppendError(
254 "no error returned from Target::Launch, and target has no process");
255 result.SetStatus(eReturnStatusFailed);
256 }
257 } else {
258 result.AppendError(error.AsCString());
259 result.SetStatus(eReturnStatusFailed);
260 }
261 return result.Succeeded();
262 }
263
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 ProcessLaunchCommandOptions m_options;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000266};
267
Greg Clayton982c9762011-11-03 21:22:33 +0000268//#define SET1 LLDB_OPT_SET_1
269//#define SET2 LLDB_OPT_SET_2
270//#define SET3 LLDB_OPT_SET_3
271//
Kate Stoneb9c1b512016-09-06 20:57:50 +0000272// OptionDefinition
273// CommandObjectProcessLaunch::CommandOptions::g_option_table[] =
Greg Clayton982c9762011-11-03 21:22:33 +0000274//{
Kate Stoneac9c3a62016-08-26 23:28:47 +0000275// // clang-format off
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276// {SET1 | SET2 | SET3, false, "stop-at-entry", 's', OptionParser::eNoArgument,
277// nullptr, 0, eArgTypeNone, "Stop at the entry point of the program
278// when launching a process."},
279// {SET1, false, "stdin", 'i',
280// OptionParser::eRequiredArgument, nullptr, 0, eArgTypeDirectoryName,
281// "Redirect stdin for the process to <path>."},
282// {SET1, false, "stdout", 'o',
283// OptionParser::eRequiredArgument, nullptr, 0, eArgTypeDirectoryName,
284// "Redirect stdout for the process to <path>."},
285// {SET1, false, "stderr", 'e',
286// OptionParser::eRequiredArgument, nullptr, 0, eArgTypeDirectoryName,
287// "Redirect stderr for the process to <path>."},
288// {SET1 | SET2 | SET3, false, "plugin", 'p',
289// OptionParser::eRequiredArgument, nullptr, 0, eArgTypePlugin, "Name of
290// the process plugin you want to use."},
291// { SET2, false, "tty", 't',
292// OptionParser::eOptionalArgument, nullptr, 0, eArgTypeDirectoryName, "Start
293// the process in a terminal. If <path> is specified, look for a terminal whose
294// name contains <path>, else start the process in a new terminal."},
295// { SET3, false, "no-stdio", 'n', OptionParser::eNoArgument,
296// nullptr, 0, eArgTypeNone, "Do not set up for terminal I/O to go to
297// running process."},
298// {SET1 | SET2 | SET3, false, "working-dir", 'w',
299// OptionParser::eRequiredArgument, nullptr, 0, eArgTypeDirectoryName, "Set the
300// current working directory to <path> when running the inferior."},
Kate Stoneac9c3a62016-08-26 23:28:47 +0000301// {0, false, nullptr, 0, 0, nullptr, 0, eArgTypeNone, nullptr}
302// // clang-format on
Greg Clayton982c9762011-11-03 21:22:33 +0000303//};
304//
305//#undef SET1
306//#undef SET2
307//#undef SET3
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000308
309//-------------------------------------------------------------------------
310// CommandObjectProcessAttach
311//-------------------------------------------------------------------------
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000312
313static OptionDefinition g_process_attach_options[] = {
314 // clang-format off
315 { LLDB_OPT_SET_ALL, false, "continue", 'c', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Immediately continue the process once attached." },
316 { LLDB_OPT_SET_ALL, false, "plugin", 'P', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePlugin, "Name of the process plugin you want to use." },
317 { LLDB_OPT_SET_1, false, "pid", 'p', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePid, "The process ID of an existing process to attach to." },
318 { LLDB_OPT_SET_2, false, "name", 'n', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeProcessName, "The name of the process to attach to." },
319 { LLDB_OPT_SET_2, false, "include-existing", 'i', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Include existing processes when doing attach -w." },
320 { LLDB_OPT_SET_2, false, "waitfor", 'w', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Wait for the process with <process-name> to launch." },
321 // clang-format on
322};
323
Jim Inghambb9caf72010-12-09 18:58:16 +0000324#pragma mark CommandObjectProcessAttach
Kate Stoneb9c1b512016-09-06 20:57:50 +0000325class CommandObjectProcessAttach : public CommandObjectProcessLaunchOrAttach {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000326public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000327 class CommandOptions : public Options {
328 public:
329 CommandOptions() : Options() {
330 // Keep default values of all options in one place: OptionParsingStarting
331 // ()
332 OptionParsingStarting(nullptr);
Jim Ingham5aee1622010-08-09 23:31:02 +0000333 }
334
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335 ~CommandOptions() override = default;
Jim Ingham5aee1622010-08-09 23:31:02 +0000336
Zachary Turner97206d52017-05-12 04:51:55 +0000337 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
338 ExecutionContext *execution_context) override {
339 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000340 const int short_option = m_getopt_table[option_idx].val;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000341 switch (short_option) {
342 case 'c':
343 attach_info.SetContinueOnceAttached(true);
344 break;
345
346 case 'p': {
Zachary Turnerfe114832016-11-12 16:56:47 +0000347 lldb::pid_t pid;
348 if (option_arg.getAsInteger(0, pid)) {
349 error.SetErrorStringWithFormat("invalid process ID '%s'",
350 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000351 } else {
352 attach_info.SetProcessID(pid);
353 }
354 } break;
355
356 case 'P':
357 attach_info.SetProcessPluginName(option_arg);
358 break;
359
360 case 'n':
361 attach_info.GetExecutableFile().SetFile(option_arg, false);
362 break;
363
364 case 'w':
365 attach_info.SetWaitForLaunch(true);
366 break;
367
368 case 'i':
369 attach_info.SetIgnoreExisting(false);
370 break;
371
372 default:
373 error.SetErrorStringWithFormat("invalid short option character '%c'",
374 short_option);
375 break;
376 }
377 return error;
Jim Ingham5a988412012-06-08 21:56:10 +0000378 }
379
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380 void OptionParsingStarting(ExecutionContext *execution_context) override {
381 attach_info.Clear();
382 }
383
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000384 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000385 return llvm::makeArrayRef(g_process_attach_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000386 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000387
388 bool HandleOptionArgumentCompletion(
389 Args &input, int cursor_index, int char_pos,
390 OptionElementVector &opt_element_vector, int opt_element_index,
391 int match_start_point, int max_return_elements,
392 CommandInterpreter &interpreter, bool &word_complete,
393 StringList &matches) override {
394 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
395 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
396
397 // We are only completing the name option for now...
398
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000399 if (GetDefinitions()[opt_defs_index].short_option == 'n') {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400 // Are we in the name?
401
402 // Look to see if there is a -P argument provided, and if so use that
Adrian Prantl05097242018-04-30 16:49:04 +0000403 // plugin, otherwise use the default plugin.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000404
405 const char *partial_name = nullptr;
406 partial_name = input.GetArgumentAtIndex(opt_arg_pos);
407
408 PlatformSP platform_sp(interpreter.GetPlatform(true));
409 if (platform_sp) {
410 ProcessInstanceInfoList process_infos;
411 ProcessInstanceInfoMatch match_info;
412 if (partial_name) {
413 match_info.GetProcessInfo().GetExecutableFile().SetFile(
414 partial_name, false);
Pavel Labathc4a33952017-02-20 11:35:33 +0000415 match_info.SetNameMatchType(NameMatch::StartsWith);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000416 }
417 platform_sp->FindProcesses(match_info, process_infos);
418 const size_t num_matches = process_infos.GetSize();
419 if (num_matches > 0) {
420 for (size_t i = 0; i < num_matches; ++i) {
421 matches.AppendString(
422 process_infos.GetProcessNameAtIndex(i),
423 process_infos.GetProcessNameLengthAtIndex(i));
424 }
425 }
426 }
427 }
428
429 return false;
430 }
431
Kate Stoneb9c1b512016-09-06 20:57:50 +0000432 // Instance variables to hold the values for command options.
433
434 ProcessAttachInfo attach_info;
435 };
436
437 CommandObjectProcessAttach(CommandInterpreter &interpreter)
438 : CommandObjectProcessLaunchOrAttach(
439 interpreter, "process attach", "Attach to a process.",
440 "process attach <cmd-options>", 0, "attach"),
441 m_options() {}
442
443 ~CommandObjectProcessAttach() override = default;
444
445 Options *GetOptions() override { return &m_options; }
446
Jim Ingham5a988412012-06-08 21:56:10 +0000447protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000448 bool DoExecute(Args &command, CommandReturnObject &result) override {
449 PlatformSP platform_sp(
450 m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
Oleksiy Vyalov926af0c2015-02-03 00:04:35 +0000451
Kate Stoneb9c1b512016-09-06 20:57:50 +0000452 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
453 // N.B. The attach should be synchronous. It doesn't help much to get the
Adrian Prantl05097242018-04-30 16:49:04 +0000454 // prompt back between initiating the attach and the target actually
455 // stopping. So even if the interpreter is set to be asynchronous, we wait
456 // for the stop ourselves here.
Jim Ingham5aee1622010-08-09 23:31:02 +0000457
Kate Stoneb9c1b512016-09-06 20:57:50 +0000458 StateType state = eStateInvalid;
459 Process *process = m_exe_ctx.GetProcessPtr();
Oleksiy Vyalov926af0c2015-02-03 00:04:35 +0000460
Kate Stoneb9c1b512016-09-06 20:57:50 +0000461 if (!StopProcessIfNecessary(process, state, result))
462 return false;
Oleksiy Vyalov926af0c2015-02-03 00:04:35 +0000463
Kate Stoneb9c1b512016-09-06 20:57:50 +0000464 if (target == nullptr) {
465 // If there isn't a current target create one.
466 TargetSP new_target_sp;
Zachary Turner97206d52017-05-12 04:51:55 +0000467 Status error;
Oleksiy Vyalov926af0c2015-02-03 00:04:35 +0000468
Kate Stoneb9c1b512016-09-06 20:57:50 +0000469 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget(
Zachary Turnera47464b2016-11-18 20:44:46 +0000470 m_interpreter.GetDebugger(), "", "", false,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000471 nullptr, // No platform options
472 new_target_sp);
473 target = new_target_sp.get();
474 if (target == nullptr || error.Fail()) {
475 result.AppendError(error.AsCString("Error creating target"));
476 return false;
477 }
478 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target);
Jim Ingham5aee1622010-08-09 23:31:02 +0000479 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000480
481 // Record the old executable module, we want to issue a warning if the
Adrian Prantl05097242018-04-30 16:49:04 +0000482 // process of attaching changed the current executable (like somebody said
483 // "file foo" then attached to a PID whose executable was bar.)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000484
485 ModuleSP old_exec_module_sp = target->GetExecutableModule();
486 ArchSpec old_arch_spec = target->GetArchitecture();
487
488 if (command.GetArgumentCount()) {
489 result.AppendErrorWithFormat("Invalid arguments for '%s'.\nUsage: %s\n",
490 m_cmd_name.c_str(), m_cmd_syntax.c_str());
491 result.SetStatus(eReturnStatusFailed);
492 return false;
493 }
494
495 m_interpreter.UpdateExecutionContext(nullptr);
496 StreamString stream;
497 const auto error = target->Attach(m_options.attach_info, &stream);
498 if (error.Success()) {
499 ProcessSP process_sp(target->GetProcessSP());
500 if (process_sp) {
Zachary Turnerc1564272016-11-16 21:15:24 +0000501 result.AppendMessage(stream.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000502 result.SetStatus(eReturnStatusSuccessFinishNoResult);
503 result.SetDidChangeProcessState(true);
504 result.SetAbnormalStopWasExpected(true);
505 } else {
506 result.AppendError(
507 "no error returned from Target::Attach, and target has no process");
508 result.SetStatus(eReturnStatusFailed);
509 }
510 } else {
511 result.AppendErrorWithFormat("attach failed: %s\n", error.AsCString());
512 result.SetStatus(eReturnStatusFailed);
513 }
514
515 if (!result.Succeeded())
516 return false;
517
518 // Okay, we're done. Last step is to warn if the executable module has
519 // changed:
520 char new_path[PATH_MAX];
521 ModuleSP new_exec_module_sp(target->GetExecutableModule());
522 if (!old_exec_module_sp) {
523 // We might not have a module if we attached to a raw pid...
524 if (new_exec_module_sp) {
525 new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
526 result.AppendMessageWithFormat("Executable module set to \"%s\".\n",
527 new_path);
528 }
529 } else if (old_exec_module_sp->GetFileSpec() !=
530 new_exec_module_sp->GetFileSpec()) {
531 char old_path[PATH_MAX];
532
533 old_exec_module_sp->GetFileSpec().GetPath(old_path, PATH_MAX);
534 new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
535
536 result.AppendWarningWithFormat(
537 "Executable module changed from \"%s\" to \"%s\".\n", old_path,
538 new_path);
539 }
540
541 if (!old_arch_spec.IsValid()) {
542 result.AppendMessageWithFormat(
543 "Architecture set to: %s.\n",
544 target->GetArchitecture().GetTriple().getTriple().c_str());
545 } else if (!old_arch_spec.IsExactMatch(target->GetArchitecture())) {
546 result.AppendWarningWithFormat(
547 "Architecture changed from %s to %s.\n",
548 old_arch_spec.GetTriple().getTriple().c_str(),
549 target->GetArchitecture().GetTriple().getTriple().c_str());
550 }
551
Adrian Prantl05097242018-04-30 16:49:04 +0000552 // This supports the use-case scenario of immediately continuing the
553 // process once attached.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000554 if (m_options.attach_info.GetContinueOnceAttached())
555 m_interpreter.HandleCommand("process continue", eLazyBoolNo, result);
556
557 return result.Succeeded();
558 }
559
560 CommandOptions m_options;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000561};
562
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000563//-------------------------------------------------------------------------
564// CommandObjectProcessContinue
565//-------------------------------------------------------------------------
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000566
567static OptionDefinition g_process_continue_options[] = {
568 // clang-format off
569 { LLDB_OPT_SET_ALL, false, "ignore-count",'i', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeUnsignedInteger, "Ignore <N> crossings of the breakpoint (if it exists) for the currently selected thread." }
570 // clang-format on
571};
572
Jim Inghambb9caf72010-12-09 18:58:16 +0000573#pragma mark CommandObjectProcessContinue
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000574
Kate Stoneb9c1b512016-09-06 20:57:50 +0000575class CommandObjectProcessContinue : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000576public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000577 CommandObjectProcessContinue(CommandInterpreter &interpreter)
578 : CommandObjectParsed(
579 interpreter, "process continue",
580 "Continue execution of all threads in the current process.",
581 "process continue",
582 eCommandRequiresProcess | eCommandTryTargetAPILock |
583 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused),
584 m_options() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000585
Kate Stoneb9c1b512016-09-06 20:57:50 +0000586 ~CommandObjectProcessContinue() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587
Jim Ingham5a988412012-06-08 21:56:10 +0000588protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000589 class CommandOptions : public Options {
590 public:
591 CommandOptions() : Options() {
592 // Keep default values of all options in one place: OptionParsingStarting
593 // ()
594 OptionParsingStarting(nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000595 }
Jim Ingham0e410842012-08-11 01:27:55 +0000596
Kate Stoneb9c1b512016-09-06 20:57:50 +0000597 ~CommandOptions() override = default;
598
Zachary Turner97206d52017-05-12 04:51:55 +0000599 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
600 ExecutionContext *execution_context) override {
601 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000602 const int short_option = m_getopt_table[option_idx].val;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000603 switch (short_option) {
604 case 'i':
Zachary Turnerfe114832016-11-12 16:56:47 +0000605 if (option_arg.getAsInteger(0, m_ignore))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000606 error.SetErrorStringWithFormat(
607 "invalid value for ignore option: \"%s\", should be a number.",
Zachary Turnerfe114832016-11-12 16:56:47 +0000608 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000609 break;
610
611 default:
612 error.SetErrorStringWithFormat("invalid short option character '%c'",
613 short_option);
614 break;
615 }
616 return error;
Jim Ingham0e410842012-08-11 01:27:55 +0000617 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000618
619 void OptionParsingStarting(ExecutionContext *execution_context) override {
620 m_ignore = 0;
621 }
622
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000623 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000624 return llvm::makeArrayRef(g_process_continue_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000625 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000626
627 uint32_t m_ignore;
628 };
629
630 bool DoExecute(Args &command, CommandReturnObject &result) override {
631 Process *process = m_exe_ctx.GetProcessPtr();
632 bool synchronous_execution = m_interpreter.GetSynchronous();
633 StateType state = process->GetState();
634 if (state == eStateStopped) {
635 if (command.GetArgumentCount() != 0) {
636 result.AppendErrorWithFormat(
637 "The '%s' command does not take any arguments.\n",
638 m_cmd_name.c_str());
639 result.SetStatus(eReturnStatusFailed);
640 return false;
641 }
642
643 if (m_options.m_ignore > 0) {
644 ThreadSP sel_thread_sp(GetDefaultThread()->shared_from_this());
645 if (sel_thread_sp) {
646 StopInfoSP stop_info_sp = sel_thread_sp->GetStopInfo();
647 if (stop_info_sp &&
648 stop_info_sp->GetStopReason() == eStopReasonBreakpoint) {
649 lldb::break_id_t bp_site_id =
650 (lldb::break_id_t)stop_info_sp->GetValue();
651 BreakpointSiteSP bp_site_sp(
652 process->GetBreakpointSiteList().FindByID(bp_site_id));
653 if (bp_site_sp) {
654 const size_t num_owners = bp_site_sp->GetNumberOfOwners();
655 for (size_t i = 0; i < num_owners; i++) {
656 Breakpoint &bp_ref =
657 bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
658 if (!bp_ref.IsInternal()) {
659 bp_ref.SetIgnoreCount(m_options.m_ignore);
660 }
661 }
662 }
663 }
664 }
665 }
666
667 { // Scope for thread list mutex:
668 std::lock_guard<std::recursive_mutex> guard(
669 process->GetThreadList().GetMutex());
670 const uint32_t num_threads = process->GetThreadList().GetSize();
671
672 // Set the actions that the threads should each take when resuming
673 for (uint32_t idx = 0; idx < num_threads; ++idx) {
674 const bool override_suspend = false;
675 process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState(
676 eStateRunning, override_suspend);
677 }
678 }
679
680 const uint32_t iohandler_id = process->GetIOHandlerID();
681
682 StreamString stream;
Zachary Turner97206d52017-05-12 04:51:55 +0000683 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000684 if (synchronous_execution)
685 error = process->ResumeSynchronous(&stream);
686 else
687 error = process->Resume();
688
689 if (error.Success()) {
690 // There is a race condition where this thread will return up the call
Adrian Prantl05097242018-04-30 16:49:04 +0000691 // stack to the main command handler and show an (lldb) prompt before
692 // HandlePrivateEvent (from PrivateStateThread) has a chance to call
693 // PushProcessIOHandler().
Kate Stoneb9c1b512016-09-06 20:57:50 +0000694 process->SyncIOHandler(iohandler_id, 2000);
695
696 result.AppendMessageWithFormat("Process %" PRIu64 " resuming\n",
697 process->GetID());
698 if (synchronous_execution) {
699 // If any state changed events had anything to say, add that to the
700 // result
Zachary Turnerc1564272016-11-16 21:15:24 +0000701 result.AppendMessage(stream.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000702
703 result.SetDidChangeProcessState(true);
704 result.SetStatus(eReturnStatusSuccessFinishNoResult);
705 } else {
706 result.SetStatus(eReturnStatusSuccessContinuingNoResult);
707 }
708 } else {
709 result.AppendErrorWithFormat("Failed to resume process: %s.\n",
710 error.AsCString());
711 result.SetStatus(eReturnStatusFailed);
712 }
713 } else {
714 result.AppendErrorWithFormat(
715 "Process cannot be continued from its current state (%s).\n",
716 StateAsCString(state));
717 result.SetStatus(eReturnStatusFailed);
718 }
719 return result.Succeeded();
720 }
721
722 Options *GetOptions() override { return &m_options; }
723
724 CommandOptions m_options;
Jim Ingham0e410842012-08-11 01:27:55 +0000725};
726
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000727//-------------------------------------------------------------------------
728// CommandObjectProcessDetach
729//-------------------------------------------------------------------------
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000730static OptionDefinition g_process_detach_options[] = {
731 // clang-format off
732 { LLDB_OPT_SET_1, false, "keep-stopped", 's', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Whether or not the process should be kept stopped on detach (if possible)." },
733 // clang-format on
734};
735
Jim Inghambb9caf72010-12-09 18:58:16 +0000736#pragma mark CommandObjectProcessDetach
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000737
Kate Stoneb9c1b512016-09-06 20:57:50 +0000738class CommandObjectProcessDetach : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000739public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000740 class CommandOptions : public Options {
741 public:
742 CommandOptions() : Options() { OptionParsingStarting(nullptr); }
743
744 ~CommandOptions() override = default;
745
Zachary Turner97206d52017-05-12 04:51:55 +0000746 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
747 ExecutionContext *execution_context) override {
748 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000749 const int short_option = m_getopt_table[option_idx].val;
750
751 switch (short_option) {
752 case 's':
753 bool tmp_result;
754 bool success;
Pavel Labath47cbf4a2018-04-10 09:03:59 +0000755 tmp_result = OptionArgParser::ToBoolean(option_arg, false, &success);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000756 if (!success)
757 error.SetErrorStringWithFormat("invalid boolean option: \"%s\"",
Zachary Turnerfe114832016-11-12 16:56:47 +0000758 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000759 else {
760 if (tmp_result)
761 m_keep_stopped = eLazyBoolYes;
762 else
763 m_keep_stopped = eLazyBoolNo;
Jim Inghamacff8952013-05-02 00:27:30 +0000764 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000765 break;
766 default:
767 error.SetErrorStringWithFormat("invalid short option character '%c'",
768 short_option);
769 break;
770 }
771 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000772 }
773
Kate Stoneb9c1b512016-09-06 20:57:50 +0000774 void OptionParsingStarting(ExecutionContext *execution_context) override {
775 m_keep_stopped = eLazyBoolCalculate;
Jim Inghamacff8952013-05-02 00:27:30 +0000776 }
777
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000778 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000779 return llvm::makeArrayRef(g_process_detach_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000780 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000781
782 // Instance variables to hold the values for command options.
783 LazyBool m_keep_stopped;
784 };
785
786 CommandObjectProcessDetach(CommandInterpreter &interpreter)
787 : CommandObjectParsed(interpreter, "process detach",
788 "Detach from the current target process.",
789 "process detach",
790 eCommandRequiresProcess | eCommandTryTargetAPILock |
791 eCommandProcessMustBeLaunched),
792 m_options() {}
793
794 ~CommandObjectProcessDetach() override = default;
795
796 Options *GetOptions() override { return &m_options; }
797
Jim Ingham5a988412012-06-08 21:56:10 +0000798protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000799 bool DoExecute(Args &command, CommandReturnObject &result) override {
800 Process *process = m_exe_ctx.GetProcessPtr();
801 // FIXME: This will be a Command Option:
802 bool keep_stopped;
803 if (m_options.m_keep_stopped == eLazyBoolCalculate) {
804 // Check the process default:
805 keep_stopped = process->GetDetachKeepsStopped();
806 } else if (m_options.m_keep_stopped == eLazyBoolYes)
807 keep_stopped = true;
808 else
809 keep_stopped = false;
Jim Inghamacff8952013-05-02 00:27:30 +0000810
Zachary Turner97206d52017-05-12 04:51:55 +0000811 Status error(process->Detach(keep_stopped));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000812 if (error.Success()) {
813 result.SetStatus(eReturnStatusSuccessFinishResult);
814 } else {
815 result.AppendErrorWithFormat("Detach failed: %s\n", error.AsCString());
816 result.SetStatus(eReturnStatusFailed);
817 return false;
818 }
819 return result.Succeeded();
820 }
821
822 CommandOptions m_options;
Jim Inghamacff8952013-05-02 00:27:30 +0000823};
824
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000825//-------------------------------------------------------------------------
Greg Claytonb766a732011-02-04 01:58:07 +0000826// CommandObjectProcessConnect
827//-------------------------------------------------------------------------
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000828
829static OptionDefinition g_process_connect_options[] = {
830 // clang-format off
831 { LLDB_OPT_SET_ALL, false, "plugin", 'p', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePlugin, "Name of the process plugin you want to use." },
832 // clang-format on
833};
834
Greg Claytonb766a732011-02-04 01:58:07 +0000835#pragma mark CommandObjectProcessConnect
836
Kate Stoneb9c1b512016-09-06 20:57:50 +0000837class CommandObjectProcessConnect : public CommandObjectParsed {
Greg Claytonb766a732011-02-04 01:58:07 +0000838public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000839 class CommandOptions : public Options {
840 public:
841 CommandOptions() : Options() {
842 // Keep default values of all options in one place: OptionParsingStarting
843 // ()
844 OptionParsingStarting(nullptr);
Greg Claytonb766a732011-02-04 01:58:07 +0000845 }
Greg Claytonb766a732011-02-04 01:58:07 +0000846
Kate Stoneb9c1b512016-09-06 20:57:50 +0000847 ~CommandOptions() override = default;
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000848
Zachary Turner97206d52017-05-12 04:51:55 +0000849 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
850 ExecutionContext *execution_context) override {
851 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000852 const int short_option = m_getopt_table[option_idx].val;
853
854 switch (short_option) {
855 case 'p':
856 plugin_name.assign(option_arg);
857 break;
858
859 default:
860 error.SetErrorStringWithFormat("invalid short option character '%c'",
861 short_option);
862 break;
863 }
864 return error;
Jim Ingham5a988412012-06-08 21:56:10 +0000865 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000866
867 void OptionParsingStarting(ExecutionContext *execution_context) override {
868 plugin_name.clear();
869 }
870
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000871 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000872 return llvm::makeArrayRef(g_process_connect_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000873 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000874
875 // Instance variables to hold the values for command options.
876
877 std::string plugin_name;
878 };
879
880 CommandObjectProcessConnect(CommandInterpreter &interpreter)
881 : CommandObjectParsed(interpreter, "process connect",
882 "Connect to a remote debug service.",
883 "process connect <remote-url>", 0),
884 m_options() {}
885
886 ~CommandObjectProcessConnect() override = default;
887
888 Options *GetOptions() override { return &m_options; }
889
Jim Ingham5a988412012-06-08 21:56:10 +0000890protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000891 bool DoExecute(Args &command, CommandReturnObject &result) override {
892 if (command.GetArgumentCount() != 1) {
893 result.AppendErrorWithFormat(
894 "'%s' takes exactly one argument:\nUsage: %s\n", m_cmd_name.c_str(),
895 m_cmd_syntax.c_str());
896 result.SetStatus(eReturnStatusFailed);
897 return false;
Greg Claytonb766a732011-02-04 01:58:07 +0000898 }
Tamas Berghammerccd6cff2015-12-08 14:08:19 +0000899
Kate Stoneb9c1b512016-09-06 20:57:50 +0000900 Process *process = m_exe_ctx.GetProcessPtr();
901 if (process && process->IsAlive()) {
902 result.AppendErrorWithFormat(
903 "Process %" PRIu64
904 " is currently being debugged, kill the process before connecting.\n",
905 process->GetID());
906 result.SetStatus(eReturnStatusFailed);
907 return false;
908 }
909
910 const char *plugin_name = nullptr;
911 if (!m_options.plugin_name.empty())
912 plugin_name = m_options.plugin_name.c_str();
913
Zachary Turner97206d52017-05-12 04:51:55 +0000914 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000915 Debugger &debugger = m_interpreter.GetDebugger();
916 PlatformSP platform_sp = m_interpreter.GetPlatform(true);
917 ProcessSP process_sp = platform_sp->ConnectProcess(
918 command.GetArgumentAtIndex(0), plugin_name, debugger,
919 debugger.GetSelectedTarget().get(), error);
920 if (error.Fail() || process_sp == nullptr) {
921 result.AppendError(error.AsCString("Error connecting to the process"));
922 result.SetStatus(eReturnStatusFailed);
923 return false;
924 }
925 return true;
926 }
927
928 CommandOptions m_options;
Greg Claytonb766a732011-02-04 01:58:07 +0000929};
930
Greg Claytonb766a732011-02-04 01:58:07 +0000931//-------------------------------------------------------------------------
Greg Clayton998255b2012-10-13 02:07:45 +0000932// CommandObjectProcessPlugin
933//-------------------------------------------------------------------------
934#pragma mark CommandObjectProcessPlugin
935
Kate Stoneb9c1b512016-09-06 20:57:50 +0000936class CommandObjectProcessPlugin : public CommandObjectProxy {
Greg Clayton998255b2012-10-13 02:07:45 +0000937public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000938 CommandObjectProcessPlugin(CommandInterpreter &interpreter)
939 : CommandObjectProxy(
940 interpreter, "process plugin",
941 "Send a custom command to the current target process plug-in.",
942 "process plugin <args>", 0) {}
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000943
Kate Stoneb9c1b512016-09-06 20:57:50 +0000944 ~CommandObjectProcessPlugin() override = default;
Greg Clayton998255b2012-10-13 02:07:45 +0000945
Kate Stoneb9c1b512016-09-06 20:57:50 +0000946 CommandObject *GetProxyCommandObject() override {
947 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
948 if (process)
949 return process->GetPluginCommandObject();
950 return nullptr;
951 }
Greg Clayton998255b2012-10-13 02:07:45 +0000952};
953
Greg Clayton998255b2012-10-13 02:07:45 +0000954//-------------------------------------------------------------------------
Greg Clayton8f343b02010-11-04 01:54:29 +0000955// CommandObjectProcessLoad
956//-------------------------------------------------------------------------
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000957
958static OptionDefinition g_process_load_options[] = {
959 // clang-format off
960 { LLDB_OPT_SET_ALL, false, "install", 'i', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypePath, "Install the shared library to the target. If specified without an argument then the library will installed in the current working directory." },
961 // clang-format on
962};
963
Jim Inghambb9caf72010-12-09 18:58:16 +0000964#pragma mark CommandObjectProcessLoad
Greg Clayton8f343b02010-11-04 01:54:29 +0000965
Kate Stoneb9c1b512016-09-06 20:57:50 +0000966class CommandObjectProcessLoad : public CommandObjectParsed {
Greg Clayton8f343b02010-11-04 01:54:29 +0000967public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000968 class CommandOptions : public Options {
969 public:
970 CommandOptions() : Options() {
971 // Keep default values of all options in one place: OptionParsingStarting
972 // ()
973 OptionParsingStarting(nullptr);
Greg Clayton8f343b02010-11-04 01:54:29 +0000974 }
975
Kate Stoneb9c1b512016-09-06 20:57:50 +0000976 ~CommandOptions() override = default;
Tamas Berghammer4fbd67a2015-12-08 13:43:59 +0000977
Zachary Turner97206d52017-05-12 04:51:55 +0000978 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
979 ExecutionContext *execution_context) override {
980 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000981 const int short_option = m_getopt_table[option_idx].val;
982 switch (short_option) {
983 case 'i':
984 do_install = true;
Zachary Turnerfe114832016-11-12 16:56:47 +0000985 if (!option_arg.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000986 install_path.SetFile(option_arg, false);
987 break;
988 default:
989 error.SetErrorStringWithFormat("invalid short option character '%c'",
990 short_option);
991 break;
992 }
993 return error;
Greg Clayton8f343b02010-11-04 01:54:29 +0000994 }
995
Kate Stoneb9c1b512016-09-06 20:57:50 +0000996 void OptionParsingStarting(ExecutionContext *execution_context) override {
997 do_install = false;
998 install_path.Clear();
999 }
1000
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001001 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +00001002 return llvm::makeArrayRef(g_process_load_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001003 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001004
1005 // Instance variables to hold the values for command options.
1006 bool do_install;
1007 FileSpec install_path;
1008 };
1009
1010 CommandObjectProcessLoad(CommandInterpreter &interpreter)
1011 : CommandObjectParsed(interpreter, "process load",
1012 "Load a shared library into the current process.",
1013 "process load <filename> [<filename> ...]",
1014 eCommandRequiresProcess | eCommandTryTargetAPILock |
1015 eCommandProcessMustBeLaunched |
1016 eCommandProcessMustBePaused),
1017 m_options() {}
1018
1019 ~CommandObjectProcessLoad() override = default;
1020
1021 Options *GetOptions() override { return &m_options; }
1022
Jim Ingham5a988412012-06-08 21:56:10 +00001023protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001024 bool DoExecute(Args &command, CommandReturnObject &result) override {
1025 Process *process = m_exe_ctx.GetProcessPtr();
Greg Clayton8f343b02010-11-04 01:54:29 +00001026
Zachary Turner97d2c402016-10-05 23:40:23 +00001027 for (auto &entry : command.entries()) {
Zachary Turner97206d52017-05-12 04:51:55 +00001028 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001029 PlatformSP platform = process->GetTarget().GetPlatform();
Zachary Turner97d2c402016-10-05 23:40:23 +00001030 llvm::StringRef image_path = entry.ref;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001031 uint32_t image_token = LLDB_INVALID_IMAGE_TOKEN;
Tamas Berghammer4fbd67a2015-12-08 13:43:59 +00001032
Kate Stoneb9c1b512016-09-06 20:57:50 +00001033 if (!m_options.do_install) {
1034 FileSpec image_spec(image_path, false);
1035 platform->ResolveRemotePath(image_spec, image_spec);
1036 image_token =
1037 platform->LoadImage(process, FileSpec(), image_spec, error);
1038 } else if (m_options.install_path) {
1039 FileSpec image_spec(image_path, true);
1040 platform->ResolveRemotePath(m_options.install_path,
1041 m_options.install_path);
1042 image_token = platform->LoadImage(process, image_spec,
1043 m_options.install_path, error);
1044 } else {
1045 FileSpec image_spec(image_path, true);
1046 image_token =
1047 platform->LoadImage(process, image_spec, FileSpec(), error);
1048 }
Tamas Berghammer4fbd67a2015-12-08 13:43:59 +00001049
Kate Stoneb9c1b512016-09-06 20:57:50 +00001050 if (image_token != LLDB_INVALID_IMAGE_TOKEN) {
1051 result.AppendMessageWithFormat(
Zachary Turner97d2c402016-10-05 23:40:23 +00001052 "Loading \"%s\"...ok\nImage %u loaded.\n", image_path.str().c_str(),
1053 image_token);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001054 result.SetStatus(eReturnStatusSuccessFinishResult);
1055 } else {
Zachary Turner97d2c402016-10-05 23:40:23 +00001056 result.AppendErrorWithFormat("failed to load '%s': %s",
1057 image_path.str().c_str(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001058 error.AsCString());
1059 result.SetStatus(eReturnStatusFailed);
1060 }
Greg Clayton8f343b02010-11-04 01:54:29 +00001061 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001062 return result.Succeeded();
1063 }
1064
1065 CommandOptions m_options;
Greg Clayton8f343b02010-11-04 01:54:29 +00001066};
1067
Greg Clayton8f343b02010-11-04 01:54:29 +00001068//-------------------------------------------------------------------------
1069// CommandObjectProcessUnload
1070//-------------------------------------------------------------------------
Jim Inghambb9caf72010-12-09 18:58:16 +00001071#pragma mark CommandObjectProcessUnload
Greg Clayton8f343b02010-11-04 01:54:29 +00001072
Kate Stoneb9c1b512016-09-06 20:57:50 +00001073class CommandObjectProcessUnload : public CommandObjectParsed {
Greg Clayton8f343b02010-11-04 01:54:29 +00001074public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001075 CommandObjectProcessUnload(CommandInterpreter &interpreter)
1076 : CommandObjectParsed(
1077 interpreter, "process unload",
1078 "Unload a shared library from the current process using the index "
1079 "returned by a previous call to \"process load\".",
1080 "process unload <index>",
1081 eCommandRequiresProcess | eCommandTryTargetAPILock |
1082 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {}
Greg Clayton8f343b02010-11-04 01:54:29 +00001083
Kate Stoneb9c1b512016-09-06 20:57:50 +00001084 ~CommandObjectProcessUnload() override = default;
Greg Clayton8f343b02010-11-04 01:54:29 +00001085
Jim Ingham5a988412012-06-08 21:56:10 +00001086protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001087 bool DoExecute(Args &command, CommandReturnObject &result) override {
1088 Process *process = m_exe_ctx.GetProcessPtr();
Greg Clayton8f343b02010-11-04 01:54:29 +00001089
Zachary Turner97d2c402016-10-05 23:40:23 +00001090 for (auto &entry : command.entries()) {
1091 uint32_t image_token;
1092 if (entry.ref.getAsInteger(0, image_token)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001093 result.AppendErrorWithFormat("invalid image index argument '%s'",
Zachary Turner97d2c402016-10-05 23:40:23 +00001094 entry.ref.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001095 result.SetStatus(eReturnStatusFailed);
1096 break;
1097 } else {
Zachary Turner97206d52017-05-12 04:51:55 +00001098 Status error(process->GetTarget().GetPlatform()->UnloadImage(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001099 process, image_token));
1100 if (error.Success()) {
1101 result.AppendMessageWithFormat(
1102 "Unloading shared library with index %u...ok\n", image_token);
1103 result.SetStatus(eReturnStatusSuccessFinishResult);
1104 } else {
1105 result.AppendErrorWithFormat("failed to unload image: %s",
1106 error.AsCString());
1107 result.SetStatus(eReturnStatusFailed);
1108 break;
Greg Clayton8f343b02010-11-04 01:54:29 +00001109 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001110 }
Greg Clayton8f343b02010-11-04 01:54:29 +00001111 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001112 return result.Succeeded();
1113 }
Greg Clayton8f343b02010-11-04 01:54:29 +00001114};
1115
1116//-------------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001117// CommandObjectProcessSignal
1118//-------------------------------------------------------------------------
Jim Inghambb9caf72010-12-09 18:58:16 +00001119#pragma mark CommandObjectProcessSignal
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001120
Kate Stoneb9c1b512016-09-06 20:57:50 +00001121class CommandObjectProcessSignal : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001122public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001123 CommandObjectProcessSignal(CommandInterpreter &interpreter)
1124 : CommandObjectParsed(interpreter, "process signal",
1125 "Send a UNIX signal to the current target process.",
1126 nullptr, eCommandRequiresProcess |
1127 eCommandTryTargetAPILock) {
1128 CommandArgumentEntry arg;
1129 CommandArgumentData signal_arg;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001130
Kate Stoneb9c1b512016-09-06 20:57:50 +00001131 // Define the first (and only) variant of this arg.
1132 signal_arg.arg_type = eArgTypeUnixSignal;
1133 signal_arg.arg_repetition = eArgRepeatPlain;
1134
1135 // There is only one variant this argument could be; put it into the
1136 // argument entry.
1137 arg.push_back(signal_arg);
1138
1139 // Push the data for the first argument into the m_arguments vector.
1140 m_arguments.push_back(arg);
1141 }
1142
1143 ~CommandObjectProcessSignal() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001144
Jim Ingham5a988412012-06-08 21:56:10 +00001145protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001146 bool DoExecute(Args &command, CommandReturnObject &result) override {
1147 Process *process = m_exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001148
Kate Stoneb9c1b512016-09-06 20:57:50 +00001149 if (command.GetArgumentCount() == 1) {
1150 int signo = LLDB_INVALID_SIGNAL_NUMBER;
1151
1152 const char *signal_name = command.GetArgumentAtIndex(0);
1153 if (::isxdigit(signal_name[0]))
1154 signo =
1155 StringConvert::ToSInt32(signal_name, LLDB_INVALID_SIGNAL_NUMBER, 0);
1156 else
1157 signo = process->GetUnixSignals()->GetSignalNumberFromName(signal_name);
1158
1159 if (signo == LLDB_INVALID_SIGNAL_NUMBER) {
1160 result.AppendErrorWithFormat("Invalid signal argument '%s'.\n",
1161 command.GetArgumentAtIndex(0));
1162 result.SetStatus(eReturnStatusFailed);
1163 } else {
Zachary Turner97206d52017-05-12 04:51:55 +00001164 Status error(process->Signal(signo));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001165 if (error.Success()) {
1166 result.SetStatus(eReturnStatusSuccessFinishResult);
1167 } else {
1168 result.AppendErrorWithFormat("Failed to send signal %i: %s\n", signo,
1169 error.AsCString());
1170 result.SetStatus(eReturnStatusFailed);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001171 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001172 }
1173 } else {
1174 result.AppendErrorWithFormat(
1175 "'%s' takes exactly one signal number argument:\nUsage: %s\n",
1176 m_cmd_name.c_str(), m_cmd_syntax.c_str());
1177 result.SetStatus(eReturnStatusFailed);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001178 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001179 return result.Succeeded();
1180 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001181};
1182
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001183//-------------------------------------------------------------------------
1184// CommandObjectProcessInterrupt
1185//-------------------------------------------------------------------------
Jim Inghambb9caf72010-12-09 18:58:16 +00001186#pragma mark CommandObjectProcessInterrupt
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001187
Kate Stoneb9c1b512016-09-06 20:57:50 +00001188class CommandObjectProcessInterrupt : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001189public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001190 CommandObjectProcessInterrupt(CommandInterpreter &interpreter)
1191 : CommandObjectParsed(interpreter, "process interrupt",
1192 "Interrupt the current target process.",
1193 "process interrupt",
1194 eCommandRequiresProcess | eCommandTryTargetAPILock |
1195 eCommandProcessMustBeLaunched) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001196
Kate Stoneb9c1b512016-09-06 20:57:50 +00001197 ~CommandObjectProcessInterrupt() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001198
Jim Ingham5a988412012-06-08 21:56:10 +00001199protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001200 bool DoExecute(Args &command, CommandReturnObject &result) override {
1201 Process *process = m_exe_ctx.GetProcessPtr();
1202 if (process == nullptr) {
1203 result.AppendError("no process to halt");
1204 result.SetStatus(eReturnStatusFailed);
1205 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001206 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001207
1208 if (command.GetArgumentCount() == 0) {
1209 bool clear_thread_plans = true;
Zachary Turner97206d52017-05-12 04:51:55 +00001210 Status error(process->Halt(clear_thread_plans));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001211 if (error.Success()) {
1212 result.SetStatus(eReturnStatusSuccessFinishResult);
1213 } else {
1214 result.AppendErrorWithFormat("Failed to halt process: %s\n",
1215 error.AsCString());
1216 result.SetStatus(eReturnStatusFailed);
1217 }
1218 } else {
1219 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
1220 m_cmd_name.c_str(), m_cmd_syntax.c_str());
1221 result.SetStatus(eReturnStatusFailed);
1222 }
1223 return result.Succeeded();
1224 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001225};
1226
1227//-------------------------------------------------------------------------
1228// CommandObjectProcessKill
1229//-------------------------------------------------------------------------
Jim Inghambb9caf72010-12-09 18:58:16 +00001230#pragma mark CommandObjectProcessKill
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001231
Kate Stoneb9c1b512016-09-06 20:57:50 +00001232class CommandObjectProcessKill : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001233public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001234 CommandObjectProcessKill(CommandInterpreter &interpreter)
1235 : CommandObjectParsed(interpreter, "process kill",
1236 "Terminate the current target process.",
1237 "process kill",
1238 eCommandRequiresProcess | eCommandTryTargetAPILock |
1239 eCommandProcessMustBeLaunched) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001240
Kate Stoneb9c1b512016-09-06 20:57:50 +00001241 ~CommandObjectProcessKill() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001242
Jim Ingham5a988412012-06-08 21:56:10 +00001243protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001244 bool DoExecute(Args &command, CommandReturnObject &result) override {
1245 Process *process = m_exe_ctx.GetProcessPtr();
1246 if (process == nullptr) {
1247 result.AppendError("no process to kill");
1248 result.SetStatus(eReturnStatusFailed);
1249 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001250 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001251
1252 if (command.GetArgumentCount() == 0) {
Zachary Turner97206d52017-05-12 04:51:55 +00001253 Status error(process->Destroy(true));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001254 if (error.Success()) {
1255 result.SetStatus(eReturnStatusSuccessFinishResult);
1256 } else {
1257 result.AppendErrorWithFormat("Failed to kill process: %s\n",
1258 error.AsCString());
1259 result.SetStatus(eReturnStatusFailed);
1260 }
1261 } else {
1262 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
1263 m_cmd_name.c_str(), m_cmd_syntax.c_str());
1264 result.SetStatus(eReturnStatusFailed);
1265 }
1266 return result.Succeeded();
1267 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001268};
1269
1270//-------------------------------------------------------------------------
Greg Claytona2715cf2014-06-13 00:54:12 +00001271// CommandObjectProcessSaveCore
1272//-------------------------------------------------------------------------
1273#pragma mark CommandObjectProcessSaveCore
1274
Kate Stoneb9c1b512016-09-06 20:57:50 +00001275class CommandObjectProcessSaveCore : public CommandObjectParsed {
Greg Claytona2715cf2014-06-13 00:54:12 +00001276public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001277 CommandObjectProcessSaveCore(CommandInterpreter &interpreter)
1278 : CommandObjectParsed(interpreter, "process save-core",
1279 "Save the current process as a core file using an "
1280 "appropriate file type.",
1281 "process save-core FILE",
1282 eCommandRequiresProcess | eCommandTryTargetAPILock |
1283 eCommandProcessMustBeLaunched) {}
Eugene Zelenko49bcfd82016-02-23 01:43:44 +00001284
Kate Stoneb9c1b512016-09-06 20:57:50 +00001285 ~CommandObjectProcessSaveCore() override = default;
Eugene Zelenko49bcfd82016-02-23 01:43:44 +00001286
Greg Claytona2715cf2014-06-13 00:54:12 +00001287protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001288 bool DoExecute(Args &command, CommandReturnObject &result) override {
1289 ProcessSP process_sp = m_exe_ctx.GetProcessSP();
1290 if (process_sp) {
1291 if (command.GetArgumentCount() == 1) {
1292 FileSpec output_file(command.GetArgumentAtIndex(0), false);
Zachary Turner97206d52017-05-12 04:51:55 +00001293 Status error = PluginManager::SaveCore(process_sp, output_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001294 if (error.Success()) {
1295 result.SetStatus(eReturnStatusSuccessFinishResult);
1296 } else {
1297 result.AppendErrorWithFormat(
1298 "Failed to save core file for process: %s\n", error.AsCString());
1299 result.SetStatus(eReturnStatusFailed);
Greg Claytona2715cf2014-06-13 00:54:12 +00001300 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001301 } else {
1302 result.AppendErrorWithFormat("'%s' takes one arguments:\nUsage: %s\n",
1303 m_cmd_name.c_str(), m_cmd_syntax.c_str());
1304 result.SetStatus(eReturnStatusFailed);
1305 }
1306 } else {
1307 result.AppendError("invalid process");
1308 result.SetStatus(eReturnStatusFailed);
1309 return false;
Greg Claytona2715cf2014-06-13 00:54:12 +00001310 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001311
1312 return result.Succeeded();
1313 }
Greg Claytona2715cf2014-06-13 00:54:12 +00001314};
1315
1316//-------------------------------------------------------------------------
Jim Ingham4b9bea82010-06-18 01:23:09 +00001317// CommandObjectProcessStatus
1318//-------------------------------------------------------------------------
Jim Inghambb9caf72010-12-09 18:58:16 +00001319#pragma mark CommandObjectProcessStatus
1320
Kate Stoneb9c1b512016-09-06 20:57:50 +00001321class CommandObjectProcessStatus : public CommandObjectParsed {
Jim Ingham4b9bea82010-06-18 01:23:09 +00001322public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001323 CommandObjectProcessStatus(CommandInterpreter &interpreter)
1324 : CommandObjectParsed(
1325 interpreter, "process status",
1326 "Show status and stop location for the current target process.",
1327 "process status",
1328 eCommandRequiresProcess | eCommandTryTargetAPILock) {}
Jim Ingham4b9bea82010-06-18 01:23:09 +00001329
Kate Stoneb9c1b512016-09-06 20:57:50 +00001330 ~CommandObjectProcessStatus() override = default;
Jim Ingham4b9bea82010-06-18 01:23:09 +00001331
Kate Stoneb9c1b512016-09-06 20:57:50 +00001332 bool DoExecute(Args &command, CommandReturnObject &result) override {
1333 Stream &strm = result.GetOutputStream();
1334 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1335 // No need to check "process" for validity as eCommandRequiresProcess
1336 // ensures it is valid
1337 Process *process = m_exe_ctx.GetProcessPtr();
1338 const bool only_threads_with_stop_reason = true;
1339 const uint32_t start_frame = 0;
1340 const uint32_t num_frames = 1;
1341 const uint32_t num_frames_with_source = 1;
Jim Ingham6a9767c2016-11-08 20:36:40 +00001342 const bool stop_format = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001343 process->GetStatus(strm);
1344 process->GetThreadStatus(strm, only_threads_with_stop_reason, start_frame,
Jim Ingham6a9767c2016-11-08 20:36:40 +00001345 num_frames, num_frames_with_source, stop_format);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001346 return result.Succeeded();
1347 }
Jim Ingham4b9bea82010-06-18 01:23:09 +00001348};
1349
1350//-------------------------------------------------------------------------
Caroline Tice35731352010-10-13 20:44:39 +00001351// CommandObjectProcessHandle
1352//-------------------------------------------------------------------------
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001353
1354static OptionDefinition g_process_handle_options[] = {
1355 // clang-format off
1356 { LLDB_OPT_SET_1, false, "stop", 's', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Whether or not the process should be stopped if the signal is received." },
1357 { LLDB_OPT_SET_1, false, "notify", 'n', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Whether or not the debugger should notify the user if the signal is received." },
1358 { LLDB_OPT_SET_1, false, "pass", 'p', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Whether or not the signal should be passed to the process." }
1359 // clang-format on
1360};
1361
Jim Inghambb9caf72010-12-09 18:58:16 +00001362#pragma mark CommandObjectProcessHandle
Caroline Tice35731352010-10-13 20:44:39 +00001363
Kate Stoneb9c1b512016-09-06 20:57:50 +00001364class CommandObjectProcessHandle : public CommandObjectParsed {
Caroline Tice35731352010-10-13 20:44:39 +00001365public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001366 class CommandOptions : public Options {
1367 public:
1368 CommandOptions() : Options() { OptionParsingStarting(nullptr); }
Caroline Tice35731352010-10-13 20:44:39 +00001369
Kate Stoneb9c1b512016-09-06 20:57:50 +00001370 ~CommandOptions() override = default;
Caroline Tice35731352010-10-13 20:44:39 +00001371
Zachary Turner97206d52017-05-12 04:51:55 +00001372 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1373 ExecutionContext *execution_context) override {
1374 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001375 const int short_option = m_getopt_table[option_idx].val;
Caroline Tice35731352010-10-13 20:44:39 +00001376
Kate Stoneb9c1b512016-09-06 20:57:50 +00001377 switch (short_option) {
1378 case 's':
1379 stop = option_arg;
1380 break;
1381 case 'n':
1382 notify = option_arg;
1383 break;
1384 case 'p':
1385 pass = option_arg;
1386 break;
1387 default:
1388 error.SetErrorStringWithFormat("invalid short option character '%c'",
1389 short_option);
1390 break;
1391 }
1392 return error;
Caroline Tice35731352010-10-13 20:44:39 +00001393 }
1394
Kate Stoneb9c1b512016-09-06 20:57:50 +00001395 void OptionParsingStarting(ExecutionContext *execution_context) override {
1396 stop.clear();
1397 notify.clear();
1398 pass.clear();
Caroline Tice35731352010-10-13 20:44:39 +00001399 }
1400
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001401 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +00001402 return llvm::makeArrayRef(g_process_handle_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001403 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001404
1405 // Instance variables to hold the values for command options.
1406
1407 std::string stop;
1408 std::string notify;
1409 std::string pass;
1410 };
1411
1412 CommandObjectProcessHandle(CommandInterpreter &interpreter)
1413 : CommandObjectParsed(interpreter, "process handle",
1414 "Manage LLDB handling of OS signals for the "
1415 "current target process. Defaults to showing "
1416 "current policy.",
1417 nullptr),
1418 m_options() {
1419 SetHelpLong("\nIf no signals are specified, update them all. If no update "
1420 "option is specified, list the current values.");
1421 CommandArgumentEntry arg;
1422 CommandArgumentData signal_arg;
1423
1424 signal_arg.arg_type = eArgTypeUnixSignal;
1425 signal_arg.arg_repetition = eArgRepeatStar;
1426
1427 arg.push_back(signal_arg);
1428
1429 m_arguments.push_back(arg);
1430 }
1431
1432 ~CommandObjectProcessHandle() override = default;
1433
1434 Options *GetOptions() override { return &m_options; }
1435
1436 bool VerifyCommandOptionValue(const std::string &option, int &real_value) {
1437 bool okay = true;
1438 bool success = false;
Pavel Labath47cbf4a2018-04-10 09:03:59 +00001439 bool tmp_value = OptionArgParser::ToBoolean(option, false, &success);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001440
1441 if (success && tmp_value)
1442 real_value = 1;
1443 else if (success && !tmp_value)
1444 real_value = 0;
1445 else {
1446 // If the value isn't 'true' or 'false', it had better be 0 or 1.
1447 real_value = StringConvert::ToUInt32(option.c_str(), 3);
1448 if (real_value != 0 && real_value != 1)
1449 okay = false;
Caroline Tice35731352010-10-13 20:44:39 +00001450 }
1451
Kate Stoneb9c1b512016-09-06 20:57:50 +00001452 return okay;
1453 }
Caroline Tice10ad7992010-10-14 21:31:13 +00001454
Kate Stoneb9c1b512016-09-06 20:57:50 +00001455 void PrintSignalHeader(Stream &str) {
1456 str.Printf("NAME PASS STOP NOTIFY\n");
1457 str.Printf("=========== ===== ===== ======\n");
1458 }
Caroline Tice10ad7992010-10-14 21:31:13 +00001459
Kate Stoneb9c1b512016-09-06 20:57:50 +00001460 void PrintSignal(Stream &str, int32_t signo, const char *sig_name,
1461 const UnixSignalsSP &signals_sp) {
1462 bool stop;
1463 bool suppress;
1464 bool notify;
1465
1466 str.Printf("%-11s ", sig_name);
1467 if (signals_sp->GetSignalInfo(signo, suppress, stop, notify)) {
1468 bool pass = !suppress;
1469 str.Printf("%s %s %s", (pass ? "true " : "false"),
1470 (stop ? "true " : "false"), (notify ? "true " : "false"));
Caroline Tice10ad7992010-10-14 21:31:13 +00001471 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001472 str.Printf("\n");
1473 }
Caroline Tice10ad7992010-10-14 21:31:13 +00001474
Kate Stoneb9c1b512016-09-06 20:57:50 +00001475 void PrintSignalInformation(Stream &str, Args &signal_args,
1476 int num_valid_signals,
1477 const UnixSignalsSP &signals_sp) {
1478 PrintSignalHeader(str);
1479
1480 if (num_valid_signals > 0) {
1481 size_t num_args = signal_args.GetArgumentCount();
1482 for (size_t i = 0; i < num_args; ++i) {
1483 int32_t signo = signals_sp->GetSignalNumberFromName(
1484 signal_args.GetArgumentAtIndex(i));
1485 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
1486 PrintSignal(str, signo, signal_args.GetArgumentAtIndex(i),
1487 signals_sp);
1488 }
1489 } else // Print info for ALL signals
Caroline Tice10ad7992010-10-14 21:31:13 +00001490 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001491 int32_t signo = signals_sp->GetFirstSignalNumber();
1492 while (signo != LLDB_INVALID_SIGNAL_NUMBER) {
1493 PrintSignal(str, signo, signals_sp->GetSignalAsCString(signo),
1494 signals_sp);
1495 signo = signals_sp->GetNextSignalNumber(signo);
1496 }
Caroline Tice10ad7992010-10-14 21:31:13 +00001497 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001498 }
Caroline Tice10ad7992010-10-14 21:31:13 +00001499
Jim Ingham5a988412012-06-08 21:56:10 +00001500protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001501 bool DoExecute(Args &signal_args, CommandReturnObject &result) override {
1502 TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
Caroline Tice35731352010-10-13 20:44:39 +00001503
Kate Stoneb9c1b512016-09-06 20:57:50 +00001504 if (!target_sp) {
1505 result.AppendError("No current target;"
1506 " cannot handle signals until you have a valid target "
1507 "and process.\n");
1508 result.SetStatus(eReturnStatusFailed);
1509 return false;
Caroline Tice35731352010-10-13 20:44:39 +00001510 }
1511
Kate Stoneb9c1b512016-09-06 20:57:50 +00001512 ProcessSP process_sp = target_sp->GetProcessSP();
1513
1514 if (!process_sp) {
1515 result.AppendError("No current process; cannot handle signals until you "
1516 "have a valid process.\n");
1517 result.SetStatus(eReturnStatusFailed);
1518 return false;
1519 }
1520
1521 int stop_action = -1; // -1 means leave the current setting alone
1522 int pass_action = -1; // -1 means leave the current setting alone
1523 int notify_action = -1; // -1 means leave the current setting alone
1524
1525 if (!m_options.stop.empty() &&
1526 !VerifyCommandOptionValue(m_options.stop, stop_action)) {
1527 result.AppendError("Invalid argument for command option --stop; must be "
1528 "true or false.\n");
1529 result.SetStatus(eReturnStatusFailed);
1530 return false;
1531 }
1532
1533 if (!m_options.notify.empty() &&
1534 !VerifyCommandOptionValue(m_options.notify, notify_action)) {
1535 result.AppendError("Invalid argument for command option --notify; must "
1536 "be true or false.\n");
1537 result.SetStatus(eReturnStatusFailed);
1538 return false;
1539 }
1540
1541 if (!m_options.pass.empty() &&
1542 !VerifyCommandOptionValue(m_options.pass, pass_action)) {
1543 result.AppendError("Invalid argument for command option --pass; must be "
1544 "true or false.\n");
1545 result.SetStatus(eReturnStatusFailed);
1546 return false;
1547 }
1548
1549 size_t num_args = signal_args.GetArgumentCount();
1550 UnixSignalsSP signals_sp = process_sp->GetUnixSignals();
1551 int num_signals_set = 0;
1552
1553 if (num_args > 0) {
Zachary Turnerd6a24752016-11-22 17:10:15 +00001554 for (const auto &arg : signal_args) {
1555 int32_t signo = signals_sp->GetSignalNumberFromName(arg.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001556 if (signo != LLDB_INVALID_SIGNAL_NUMBER) {
1557 // Casting the actions as bools here should be okay, because
Adrian Prantl05097242018-04-30 16:49:04 +00001558 // VerifyCommandOptionValue guarantees the value is either 0 or 1.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001559 if (stop_action != -1)
1560 signals_sp->SetShouldStop(signo, stop_action);
1561 if (pass_action != -1) {
1562 bool suppress = !pass_action;
1563 signals_sp->SetShouldSuppress(signo, suppress);
1564 }
1565 if (notify_action != -1)
1566 signals_sp->SetShouldNotify(signo, notify_action);
1567 ++num_signals_set;
1568 } else {
1569 result.AppendErrorWithFormat("Invalid signal name '%s'\n",
Zachary Turnerd6a24752016-11-22 17:10:15 +00001570 arg.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001571 }
1572 }
1573 } else {
1574 // No signal specified, if any command options were specified, update ALL
1575 // signals.
1576 if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1)) {
1577 if (m_interpreter.Confirm(
1578 "Do you really want to update all the signals?", false)) {
1579 int32_t signo = signals_sp->GetFirstSignalNumber();
1580 while (signo != LLDB_INVALID_SIGNAL_NUMBER) {
1581 if (notify_action != -1)
1582 signals_sp->SetShouldNotify(signo, notify_action);
1583 if (stop_action != -1)
1584 signals_sp->SetShouldStop(signo, stop_action);
1585 if (pass_action != -1) {
1586 bool suppress = !pass_action;
1587 signals_sp->SetShouldSuppress(signo, suppress);
1588 }
1589 signo = signals_sp->GetNextSignalNumber(signo);
1590 }
1591 }
1592 }
1593 }
1594
1595 PrintSignalInformation(result.GetOutputStream(), signal_args,
1596 num_signals_set, signals_sp);
1597
1598 if (num_signals_set > 0)
1599 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1600 else
1601 result.SetStatus(eReturnStatusFailed);
1602
1603 return result.Succeeded();
1604 }
1605
1606 CommandOptions m_options;
Caroline Tice35731352010-10-13 20:44:39 +00001607};
1608
Caroline Tice35731352010-10-13 20:44:39 +00001609//-------------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001610// CommandObjectMultiwordProcess
1611//-------------------------------------------------------------------------
1612
Kate Stoneb9c1b512016-09-06 20:57:50 +00001613CommandObjectMultiwordProcess::CommandObjectMultiwordProcess(
1614 CommandInterpreter &interpreter)
1615 : CommandObjectMultiword(
1616 interpreter, "process",
1617 "Commands for interacting with processes on the current platform.",
1618 "process <subcommand> [<subcommand-options>]") {
1619 LoadSubCommand("attach",
1620 CommandObjectSP(new CommandObjectProcessAttach(interpreter)));
1621 LoadSubCommand("launch",
1622 CommandObjectSP(new CommandObjectProcessLaunch(interpreter)));
1623 LoadSubCommand("continue", CommandObjectSP(new CommandObjectProcessContinue(
1624 interpreter)));
1625 LoadSubCommand("connect",
1626 CommandObjectSP(new CommandObjectProcessConnect(interpreter)));
1627 LoadSubCommand("detach",
1628 CommandObjectSP(new CommandObjectProcessDetach(interpreter)));
1629 LoadSubCommand("load",
1630 CommandObjectSP(new CommandObjectProcessLoad(interpreter)));
1631 LoadSubCommand("unload",
1632 CommandObjectSP(new CommandObjectProcessUnload(interpreter)));
1633 LoadSubCommand("signal",
1634 CommandObjectSP(new CommandObjectProcessSignal(interpreter)));
1635 LoadSubCommand("handle",
1636 CommandObjectSP(new CommandObjectProcessHandle(interpreter)));
1637 LoadSubCommand("status",
1638 CommandObjectSP(new CommandObjectProcessStatus(interpreter)));
1639 LoadSubCommand("interrupt", CommandObjectSP(new CommandObjectProcessInterrupt(
1640 interpreter)));
1641 LoadSubCommand("kill",
1642 CommandObjectSP(new CommandObjectProcessKill(interpreter)));
1643 LoadSubCommand("plugin",
1644 CommandObjectSP(new CommandObjectProcessPlugin(interpreter)));
1645 LoadSubCommand("save-core", CommandObjectSP(new CommandObjectProcessSaveCore(
1646 interpreter)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001647}
1648
Eugene Zelenko49bcfd82016-02-23 01:43:44 +00001649CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess() = default;