blob: da5110907f12e0cbbd5d2073c8f21df74a5048bc [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
183 // (i.e. enabled if the platform supports it).
184 // First check if the process launch options explicitly turn on/off
185 // 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) {
189 // The user specified an explicit setting on the process launch line. Use
190 // it.
191 disable_aslr = (m_options.disable_aslr == eLazyBoolYes);
192 } else {
193 // The user did not explicitly specify whether to disable ASLR. Fall back
194 // to the target.disable-aslr setting.
195 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
237 // stack to the main command
238 // handler and show an (lldb) prompt before HandlePrivateEvent (from
239 // PrivateStateThread) has
240 // a chance to call PushProcessIOHandler().
241 process_sp->SyncIOHandler(0, 2000);
242
Zachary Turnerc1564272016-11-16 21:15:24 +0000243 llvm::StringRef data = stream.GetString();
244 if (!data.empty())
245 result.AppendMessage(data);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246 const char *archname =
247 exe_module_sp->GetArchitecture().GetArchitectureName();
248 result.AppendMessageWithFormat(
249 "Process %" PRIu64 " launched: '%s' (%s)\n", process_sp->GetID(),
250 exe_module_sp->GetFileSpec().GetPath().c_str(), archname);
251 result.SetStatus(eReturnStatusSuccessFinishResult);
252 result.SetDidChangeProcessState(true);
253 } else {
254 result.AppendError(
255 "no error returned from Target::Launch, and target has no process");
256 result.SetStatus(eReturnStatusFailed);
257 }
258 } else {
259 result.AppendError(error.AsCString());
260 result.SetStatus(eReturnStatusFailed);
261 }
262 return result.Succeeded();
263 }
264
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000265protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000266 ProcessLaunchCommandOptions m_options;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000267};
268
Greg Clayton982c9762011-11-03 21:22:33 +0000269//#define SET1 LLDB_OPT_SET_1
270//#define SET2 LLDB_OPT_SET_2
271//#define SET3 LLDB_OPT_SET_3
272//
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273// OptionDefinition
274// CommandObjectProcessLaunch::CommandOptions::g_option_table[] =
Greg Clayton982c9762011-11-03 21:22:33 +0000275//{
Kate Stoneac9c3a62016-08-26 23:28:47 +0000276// // clang-format off
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277// {SET1 | SET2 | SET3, false, "stop-at-entry", 's', OptionParser::eNoArgument,
278// nullptr, 0, eArgTypeNone, "Stop at the entry point of the program
279// when launching a process."},
280// {SET1, false, "stdin", 'i',
281// OptionParser::eRequiredArgument, nullptr, 0, eArgTypeDirectoryName,
282// "Redirect stdin for the process to <path>."},
283// {SET1, false, "stdout", 'o',
284// OptionParser::eRequiredArgument, nullptr, 0, eArgTypeDirectoryName,
285// "Redirect stdout for the process to <path>."},
286// {SET1, false, "stderr", 'e',
287// OptionParser::eRequiredArgument, nullptr, 0, eArgTypeDirectoryName,
288// "Redirect stderr for the process to <path>."},
289// {SET1 | SET2 | SET3, false, "plugin", 'p',
290// OptionParser::eRequiredArgument, nullptr, 0, eArgTypePlugin, "Name of
291// the process plugin you want to use."},
292// { SET2, false, "tty", 't',
293// OptionParser::eOptionalArgument, nullptr, 0, eArgTypeDirectoryName, "Start
294// the process in a terminal. If <path> is specified, look for a terminal whose
295// name contains <path>, else start the process in a new terminal."},
296// { SET3, false, "no-stdio", 'n', OptionParser::eNoArgument,
297// nullptr, 0, eArgTypeNone, "Do not set up for terminal I/O to go to
298// running process."},
299// {SET1 | SET2 | SET3, false, "working-dir", 'w',
300// OptionParser::eRequiredArgument, nullptr, 0, eArgTypeDirectoryName, "Set the
301// current working directory to <path> when running the inferior."},
Kate Stoneac9c3a62016-08-26 23:28:47 +0000302// {0, false, nullptr, 0, 0, nullptr, 0, eArgTypeNone, nullptr}
303// // clang-format on
Greg Clayton982c9762011-11-03 21:22:33 +0000304//};
305//
306//#undef SET1
307//#undef SET2
308//#undef SET3
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309
310//-------------------------------------------------------------------------
311// CommandObjectProcessAttach
312//-------------------------------------------------------------------------
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000313
314static OptionDefinition g_process_attach_options[] = {
315 // clang-format off
316 { LLDB_OPT_SET_ALL, false, "continue", 'c', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Immediately continue the process once attached." },
317 { LLDB_OPT_SET_ALL, false, "plugin", 'P', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePlugin, "Name of the process plugin you want to use." },
318 { LLDB_OPT_SET_1, false, "pid", 'p', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePid, "The process ID of an existing process to attach to." },
319 { LLDB_OPT_SET_2, false, "name", 'n', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeProcessName, "The name of the process to attach to." },
320 { LLDB_OPT_SET_2, false, "include-existing", 'i', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Include existing processes when doing attach -w." },
321 { LLDB_OPT_SET_2, false, "waitfor", 'w', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Wait for the process with <process-name> to launch." },
322 // clang-format on
323};
324
Jim Inghambb9caf72010-12-09 18:58:16 +0000325#pragma mark CommandObjectProcessAttach
Kate Stoneb9c1b512016-09-06 20:57:50 +0000326class CommandObjectProcessAttach : public CommandObjectProcessLaunchOrAttach {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000327public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000328 class CommandOptions : public Options {
329 public:
330 CommandOptions() : Options() {
331 // Keep default values of all options in one place: OptionParsingStarting
332 // ()
333 OptionParsingStarting(nullptr);
Jim Ingham5aee1622010-08-09 23:31:02 +0000334 }
335
Kate Stoneb9c1b512016-09-06 20:57:50 +0000336 ~CommandOptions() override = default;
Jim Ingham5aee1622010-08-09 23:31:02 +0000337
Zachary Turner97206d52017-05-12 04:51:55 +0000338 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
339 ExecutionContext *execution_context) override {
340 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000341 const int short_option = m_getopt_table[option_idx].val;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342 switch (short_option) {
343 case 'c':
344 attach_info.SetContinueOnceAttached(true);
345 break;
346
347 case 'p': {
Zachary Turnerfe114832016-11-12 16:56:47 +0000348 lldb::pid_t pid;
349 if (option_arg.getAsInteger(0, pid)) {
350 error.SetErrorStringWithFormat("invalid process ID '%s'",
351 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000352 } else {
353 attach_info.SetProcessID(pid);
354 }
355 } break;
356
357 case 'P':
358 attach_info.SetProcessPluginName(option_arg);
359 break;
360
361 case 'n':
362 attach_info.GetExecutableFile().SetFile(option_arg, false);
363 break;
364
365 case 'w':
366 attach_info.SetWaitForLaunch(true);
367 break;
368
369 case 'i':
370 attach_info.SetIgnoreExisting(false);
371 break;
372
373 default:
374 error.SetErrorStringWithFormat("invalid short option character '%c'",
375 short_option);
376 break;
377 }
378 return error;
Jim Ingham5a988412012-06-08 21:56:10 +0000379 }
380
Kate Stoneb9c1b512016-09-06 20:57:50 +0000381 void OptionParsingStarting(ExecutionContext *execution_context) override {
382 attach_info.Clear();
383 }
384
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000385 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000386 return llvm::makeArrayRef(g_process_attach_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000387 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000388
389 bool HandleOptionArgumentCompletion(
390 Args &input, int cursor_index, int char_pos,
391 OptionElementVector &opt_element_vector, int opt_element_index,
392 int match_start_point, int max_return_elements,
393 CommandInterpreter &interpreter, bool &word_complete,
394 StringList &matches) override {
395 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
396 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
397
398 // We are only completing the name option for now...
399
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000400 if (GetDefinitions()[opt_defs_index].short_option == 'n') {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000401 // Are we in the name?
402
403 // Look to see if there is a -P argument provided, and if so use that
404 // plugin, otherwise
405 // use the default plugin.
406
407 const char *partial_name = nullptr;
408 partial_name = input.GetArgumentAtIndex(opt_arg_pos);
409
410 PlatformSP platform_sp(interpreter.GetPlatform(true));
411 if (platform_sp) {
412 ProcessInstanceInfoList process_infos;
413 ProcessInstanceInfoMatch match_info;
414 if (partial_name) {
415 match_info.GetProcessInfo().GetExecutableFile().SetFile(
416 partial_name, false);
Pavel Labathc4a33952017-02-20 11:35:33 +0000417 match_info.SetNameMatchType(NameMatch::StartsWith);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000418 }
419 platform_sp->FindProcesses(match_info, process_infos);
420 const size_t num_matches = process_infos.GetSize();
421 if (num_matches > 0) {
422 for (size_t i = 0; i < num_matches; ++i) {
423 matches.AppendString(
424 process_infos.GetProcessNameAtIndex(i),
425 process_infos.GetProcessNameLengthAtIndex(i));
426 }
427 }
428 }
429 }
430
431 return false;
432 }
433
Kate Stoneb9c1b512016-09-06 20:57:50 +0000434 // Instance variables to hold the values for command options.
435
436 ProcessAttachInfo attach_info;
437 };
438
439 CommandObjectProcessAttach(CommandInterpreter &interpreter)
440 : CommandObjectProcessLaunchOrAttach(
441 interpreter, "process attach", "Attach to a process.",
442 "process attach <cmd-options>", 0, "attach"),
443 m_options() {}
444
445 ~CommandObjectProcessAttach() override = default;
446
447 Options *GetOptions() override { return &m_options; }
448
Jim Ingham5a988412012-06-08 21:56:10 +0000449protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000450 bool DoExecute(Args &command, CommandReturnObject &result) override {
451 PlatformSP platform_sp(
452 m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
Oleksiy Vyalov926af0c2015-02-03 00:04:35 +0000453
Kate Stoneb9c1b512016-09-06 20:57:50 +0000454 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
455 // N.B. The attach should be synchronous. It doesn't help much to get the
456 // prompt back between initiating the attach
457 // and the target actually stopping. So even if the interpreter is set to
458 // be asynchronous, we wait for the stop
459 // ourselves here.
Jim Ingham5aee1622010-08-09 23:31:02 +0000460
Kate Stoneb9c1b512016-09-06 20:57:50 +0000461 StateType state = eStateInvalid;
462 Process *process = m_exe_ctx.GetProcessPtr();
Oleksiy Vyalov926af0c2015-02-03 00:04:35 +0000463
Kate Stoneb9c1b512016-09-06 20:57:50 +0000464 if (!StopProcessIfNecessary(process, state, result))
465 return false;
Oleksiy Vyalov926af0c2015-02-03 00:04:35 +0000466
Kate Stoneb9c1b512016-09-06 20:57:50 +0000467 if (target == nullptr) {
468 // If there isn't a current target create one.
469 TargetSP new_target_sp;
Zachary Turner97206d52017-05-12 04:51:55 +0000470 Status error;
Oleksiy Vyalov926af0c2015-02-03 00:04:35 +0000471
Kate Stoneb9c1b512016-09-06 20:57:50 +0000472 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget(
Zachary Turnera47464b2016-11-18 20:44:46 +0000473 m_interpreter.GetDebugger(), "", "", false,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000474 nullptr, // No platform options
475 new_target_sp);
476 target = new_target_sp.get();
477 if (target == nullptr || error.Fail()) {
478 result.AppendError(error.AsCString("Error creating target"));
479 return false;
480 }
481 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target);
Jim Ingham5aee1622010-08-09 23:31:02 +0000482 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000483
484 // Record the old executable module, we want to issue a warning if the
485 // process of attaching changed the
486 // current executable (like somebody said "file foo" then attached to a PID
487 // whose executable was bar.)
488
489 ModuleSP old_exec_module_sp = target->GetExecutableModule();
490 ArchSpec old_arch_spec = target->GetArchitecture();
491
492 if (command.GetArgumentCount()) {
493 result.AppendErrorWithFormat("Invalid arguments for '%s'.\nUsage: %s\n",
494 m_cmd_name.c_str(), m_cmd_syntax.c_str());
495 result.SetStatus(eReturnStatusFailed);
496 return false;
497 }
498
499 m_interpreter.UpdateExecutionContext(nullptr);
500 StreamString stream;
501 const auto error = target->Attach(m_options.attach_info, &stream);
502 if (error.Success()) {
503 ProcessSP process_sp(target->GetProcessSP());
504 if (process_sp) {
Zachary Turnerc1564272016-11-16 21:15:24 +0000505 result.AppendMessage(stream.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000506 result.SetStatus(eReturnStatusSuccessFinishNoResult);
507 result.SetDidChangeProcessState(true);
508 result.SetAbnormalStopWasExpected(true);
509 } else {
510 result.AppendError(
511 "no error returned from Target::Attach, and target has no process");
512 result.SetStatus(eReturnStatusFailed);
513 }
514 } else {
515 result.AppendErrorWithFormat("attach failed: %s\n", error.AsCString());
516 result.SetStatus(eReturnStatusFailed);
517 }
518
519 if (!result.Succeeded())
520 return false;
521
522 // Okay, we're done. Last step is to warn if the executable module has
523 // changed:
524 char new_path[PATH_MAX];
525 ModuleSP new_exec_module_sp(target->GetExecutableModule());
526 if (!old_exec_module_sp) {
527 // We might not have a module if we attached to a raw pid...
528 if (new_exec_module_sp) {
529 new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
530 result.AppendMessageWithFormat("Executable module set to \"%s\".\n",
531 new_path);
532 }
533 } else if (old_exec_module_sp->GetFileSpec() !=
534 new_exec_module_sp->GetFileSpec()) {
535 char old_path[PATH_MAX];
536
537 old_exec_module_sp->GetFileSpec().GetPath(old_path, PATH_MAX);
538 new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
539
540 result.AppendWarningWithFormat(
541 "Executable module changed from \"%s\" to \"%s\".\n", old_path,
542 new_path);
543 }
544
545 if (!old_arch_spec.IsValid()) {
546 result.AppendMessageWithFormat(
547 "Architecture set to: %s.\n",
548 target->GetArchitecture().GetTriple().getTriple().c_str());
549 } else if (!old_arch_spec.IsExactMatch(target->GetArchitecture())) {
550 result.AppendWarningWithFormat(
551 "Architecture changed from %s to %s.\n",
552 old_arch_spec.GetTriple().getTriple().c_str(),
553 target->GetArchitecture().GetTriple().getTriple().c_str());
554 }
555
556 // This supports the use-case scenario of immediately continuing the process
557 // once attached.
558 if (m_options.attach_info.GetContinueOnceAttached())
559 m_interpreter.HandleCommand("process continue", eLazyBoolNo, result);
560
561 return result.Succeeded();
562 }
563
564 CommandOptions m_options;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000565};
566
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000567//-------------------------------------------------------------------------
568// CommandObjectProcessContinue
569//-------------------------------------------------------------------------
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000570
571static OptionDefinition g_process_continue_options[] = {
572 // clang-format off
573 { 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." }
574 // clang-format on
575};
576
Jim Inghambb9caf72010-12-09 18:58:16 +0000577#pragma mark CommandObjectProcessContinue
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000578
Kate Stoneb9c1b512016-09-06 20:57:50 +0000579class CommandObjectProcessContinue : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000580public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000581 CommandObjectProcessContinue(CommandInterpreter &interpreter)
582 : CommandObjectParsed(
583 interpreter, "process continue",
584 "Continue execution of all threads in the current process.",
585 "process continue",
586 eCommandRequiresProcess | eCommandTryTargetAPILock |
587 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused),
588 m_options() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000589
Kate Stoneb9c1b512016-09-06 20:57:50 +0000590 ~CommandObjectProcessContinue() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000591
Jim Ingham5a988412012-06-08 21:56:10 +0000592protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000593 class CommandOptions : public Options {
594 public:
595 CommandOptions() : Options() {
596 // Keep default values of all options in one place: OptionParsingStarting
597 // ()
598 OptionParsingStarting(nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000599 }
Jim Ingham0e410842012-08-11 01:27:55 +0000600
Kate Stoneb9c1b512016-09-06 20:57:50 +0000601 ~CommandOptions() override = default;
602
Zachary Turner97206d52017-05-12 04:51:55 +0000603 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
604 ExecutionContext *execution_context) override {
605 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000606 const int short_option = m_getopt_table[option_idx].val;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000607 switch (short_option) {
608 case 'i':
Zachary Turnerfe114832016-11-12 16:56:47 +0000609 if (option_arg.getAsInteger(0, m_ignore))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000610 error.SetErrorStringWithFormat(
611 "invalid value for ignore option: \"%s\", should be a number.",
Zachary Turnerfe114832016-11-12 16:56:47 +0000612 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000613 break;
614
615 default:
616 error.SetErrorStringWithFormat("invalid short option character '%c'",
617 short_option);
618 break;
619 }
620 return error;
Jim Ingham0e410842012-08-11 01:27:55 +0000621 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000622
623 void OptionParsingStarting(ExecutionContext *execution_context) override {
624 m_ignore = 0;
625 }
626
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000627 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000628 return llvm::makeArrayRef(g_process_continue_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000629 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000630
631 uint32_t m_ignore;
632 };
633
634 bool DoExecute(Args &command, CommandReturnObject &result) override {
635 Process *process = m_exe_ctx.GetProcessPtr();
636 bool synchronous_execution = m_interpreter.GetSynchronous();
637 StateType state = process->GetState();
638 if (state == eStateStopped) {
639 if (command.GetArgumentCount() != 0) {
640 result.AppendErrorWithFormat(
641 "The '%s' command does not take any arguments.\n",
642 m_cmd_name.c_str());
643 result.SetStatus(eReturnStatusFailed);
644 return false;
645 }
646
647 if (m_options.m_ignore > 0) {
648 ThreadSP sel_thread_sp(GetDefaultThread()->shared_from_this());
649 if (sel_thread_sp) {
650 StopInfoSP stop_info_sp = sel_thread_sp->GetStopInfo();
651 if (stop_info_sp &&
652 stop_info_sp->GetStopReason() == eStopReasonBreakpoint) {
653 lldb::break_id_t bp_site_id =
654 (lldb::break_id_t)stop_info_sp->GetValue();
655 BreakpointSiteSP bp_site_sp(
656 process->GetBreakpointSiteList().FindByID(bp_site_id));
657 if (bp_site_sp) {
658 const size_t num_owners = bp_site_sp->GetNumberOfOwners();
659 for (size_t i = 0; i < num_owners; i++) {
660 Breakpoint &bp_ref =
661 bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
662 if (!bp_ref.IsInternal()) {
663 bp_ref.SetIgnoreCount(m_options.m_ignore);
664 }
665 }
666 }
667 }
668 }
669 }
670
671 { // Scope for thread list mutex:
672 std::lock_guard<std::recursive_mutex> guard(
673 process->GetThreadList().GetMutex());
674 const uint32_t num_threads = process->GetThreadList().GetSize();
675
676 // Set the actions that the threads should each take when resuming
677 for (uint32_t idx = 0; idx < num_threads; ++idx) {
678 const bool override_suspend = false;
679 process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState(
680 eStateRunning, override_suspend);
681 }
682 }
683
684 const uint32_t iohandler_id = process->GetIOHandlerID();
685
686 StreamString stream;
Zachary Turner97206d52017-05-12 04:51:55 +0000687 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000688 if (synchronous_execution)
689 error = process->ResumeSynchronous(&stream);
690 else
691 error = process->Resume();
692
693 if (error.Success()) {
694 // There is a race condition where this thread will return up the call
695 // stack to the main command
696 // handler and show an (lldb) prompt before HandlePrivateEvent (from
697 // PrivateStateThread) has
698 // a chance to call PushProcessIOHandler().
699 process->SyncIOHandler(iohandler_id, 2000);
700
701 result.AppendMessageWithFormat("Process %" PRIu64 " resuming\n",
702 process->GetID());
703 if (synchronous_execution) {
704 // If any state changed events had anything to say, add that to the
705 // result
Zachary Turnerc1564272016-11-16 21:15:24 +0000706 result.AppendMessage(stream.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000707
708 result.SetDidChangeProcessState(true);
709 result.SetStatus(eReturnStatusSuccessFinishNoResult);
710 } else {
711 result.SetStatus(eReturnStatusSuccessContinuingNoResult);
712 }
713 } else {
714 result.AppendErrorWithFormat("Failed to resume process: %s.\n",
715 error.AsCString());
716 result.SetStatus(eReturnStatusFailed);
717 }
718 } else {
719 result.AppendErrorWithFormat(
720 "Process cannot be continued from its current state (%s).\n",
721 StateAsCString(state));
722 result.SetStatus(eReturnStatusFailed);
723 }
724 return result.Succeeded();
725 }
726
727 Options *GetOptions() override { return &m_options; }
728
729 CommandOptions m_options;
Jim Ingham0e410842012-08-11 01:27:55 +0000730};
731
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000732//-------------------------------------------------------------------------
733// CommandObjectProcessDetach
734//-------------------------------------------------------------------------
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000735static OptionDefinition g_process_detach_options[] = {
736 // clang-format off
737 { 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)." },
738 // clang-format on
739};
740
Jim Inghambb9caf72010-12-09 18:58:16 +0000741#pragma mark CommandObjectProcessDetach
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000742
Kate Stoneb9c1b512016-09-06 20:57:50 +0000743class CommandObjectProcessDetach : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000744public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000745 class CommandOptions : public Options {
746 public:
747 CommandOptions() : Options() { OptionParsingStarting(nullptr); }
748
749 ~CommandOptions() override = default;
750
Zachary Turner97206d52017-05-12 04:51:55 +0000751 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
752 ExecutionContext *execution_context) override {
753 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000754 const int short_option = m_getopt_table[option_idx].val;
755
756 switch (short_option) {
757 case 's':
758 bool tmp_result;
759 bool success;
Pavel Labath47cbf4a2018-04-10 09:03:59 +0000760 tmp_result = OptionArgParser::ToBoolean(option_arg, false, &success);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000761 if (!success)
762 error.SetErrorStringWithFormat("invalid boolean option: \"%s\"",
Zachary Turnerfe114832016-11-12 16:56:47 +0000763 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000764 else {
765 if (tmp_result)
766 m_keep_stopped = eLazyBoolYes;
767 else
768 m_keep_stopped = eLazyBoolNo;
Jim Inghamacff8952013-05-02 00:27:30 +0000769 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000770 break;
771 default:
772 error.SetErrorStringWithFormat("invalid short option character '%c'",
773 short_option);
774 break;
775 }
776 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000777 }
778
Kate Stoneb9c1b512016-09-06 20:57:50 +0000779 void OptionParsingStarting(ExecutionContext *execution_context) override {
780 m_keep_stopped = eLazyBoolCalculate;
Jim Inghamacff8952013-05-02 00:27:30 +0000781 }
782
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000783 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000784 return llvm::makeArrayRef(g_process_detach_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000785 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000786
787 // Instance variables to hold the values for command options.
788 LazyBool m_keep_stopped;
789 };
790
791 CommandObjectProcessDetach(CommandInterpreter &interpreter)
792 : CommandObjectParsed(interpreter, "process detach",
793 "Detach from the current target process.",
794 "process detach",
795 eCommandRequiresProcess | eCommandTryTargetAPILock |
796 eCommandProcessMustBeLaunched),
797 m_options() {}
798
799 ~CommandObjectProcessDetach() override = default;
800
801 Options *GetOptions() override { return &m_options; }
802
Jim Ingham5a988412012-06-08 21:56:10 +0000803protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000804 bool DoExecute(Args &command, CommandReturnObject &result) override {
805 Process *process = m_exe_ctx.GetProcessPtr();
806 // FIXME: This will be a Command Option:
807 bool keep_stopped;
808 if (m_options.m_keep_stopped == eLazyBoolCalculate) {
809 // Check the process default:
810 keep_stopped = process->GetDetachKeepsStopped();
811 } else if (m_options.m_keep_stopped == eLazyBoolYes)
812 keep_stopped = true;
813 else
814 keep_stopped = false;
Jim Inghamacff8952013-05-02 00:27:30 +0000815
Zachary Turner97206d52017-05-12 04:51:55 +0000816 Status error(process->Detach(keep_stopped));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000817 if (error.Success()) {
818 result.SetStatus(eReturnStatusSuccessFinishResult);
819 } else {
820 result.AppendErrorWithFormat("Detach failed: %s\n", error.AsCString());
821 result.SetStatus(eReturnStatusFailed);
822 return false;
823 }
824 return result.Succeeded();
825 }
826
827 CommandOptions m_options;
Jim Inghamacff8952013-05-02 00:27:30 +0000828};
829
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000830//-------------------------------------------------------------------------
Greg Claytonb766a732011-02-04 01:58:07 +0000831// CommandObjectProcessConnect
832//-------------------------------------------------------------------------
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000833
834static OptionDefinition g_process_connect_options[] = {
835 // clang-format off
836 { LLDB_OPT_SET_ALL, false, "plugin", 'p', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePlugin, "Name of the process plugin you want to use." },
837 // clang-format on
838};
839
Greg Claytonb766a732011-02-04 01:58:07 +0000840#pragma mark CommandObjectProcessConnect
841
Kate Stoneb9c1b512016-09-06 20:57:50 +0000842class CommandObjectProcessConnect : public CommandObjectParsed {
Greg Claytonb766a732011-02-04 01:58:07 +0000843public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000844 class CommandOptions : public Options {
845 public:
846 CommandOptions() : Options() {
847 // Keep default values of all options in one place: OptionParsingStarting
848 // ()
849 OptionParsingStarting(nullptr);
Greg Claytonb766a732011-02-04 01:58:07 +0000850 }
Greg Claytonb766a732011-02-04 01:58:07 +0000851
Kate Stoneb9c1b512016-09-06 20:57:50 +0000852 ~CommandOptions() override = default;
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000853
Zachary Turner97206d52017-05-12 04:51:55 +0000854 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
855 ExecutionContext *execution_context) override {
856 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000857 const int short_option = m_getopt_table[option_idx].val;
858
859 switch (short_option) {
860 case 'p':
861 plugin_name.assign(option_arg);
862 break;
863
864 default:
865 error.SetErrorStringWithFormat("invalid short option character '%c'",
866 short_option);
867 break;
868 }
869 return error;
Jim Ingham5a988412012-06-08 21:56:10 +0000870 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000871
872 void OptionParsingStarting(ExecutionContext *execution_context) override {
873 plugin_name.clear();
874 }
875
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000876 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000877 return llvm::makeArrayRef(g_process_connect_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000878 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000879
880 // Instance variables to hold the values for command options.
881
882 std::string plugin_name;
883 };
884
885 CommandObjectProcessConnect(CommandInterpreter &interpreter)
886 : CommandObjectParsed(interpreter, "process connect",
887 "Connect to a remote debug service.",
888 "process connect <remote-url>", 0),
889 m_options() {}
890
891 ~CommandObjectProcessConnect() override = default;
892
893 Options *GetOptions() override { return &m_options; }
894
Jim Ingham5a988412012-06-08 21:56:10 +0000895protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000896 bool DoExecute(Args &command, CommandReturnObject &result) override {
897 if (command.GetArgumentCount() != 1) {
898 result.AppendErrorWithFormat(
899 "'%s' takes exactly one argument:\nUsage: %s\n", m_cmd_name.c_str(),
900 m_cmd_syntax.c_str());
901 result.SetStatus(eReturnStatusFailed);
902 return false;
Greg Claytonb766a732011-02-04 01:58:07 +0000903 }
Tamas Berghammerccd6cff2015-12-08 14:08:19 +0000904
Kate Stoneb9c1b512016-09-06 20:57:50 +0000905 Process *process = m_exe_ctx.GetProcessPtr();
906 if (process && process->IsAlive()) {
907 result.AppendErrorWithFormat(
908 "Process %" PRIu64
909 " is currently being debugged, kill the process before connecting.\n",
910 process->GetID());
911 result.SetStatus(eReturnStatusFailed);
912 return false;
913 }
914
915 const char *plugin_name = nullptr;
916 if (!m_options.plugin_name.empty())
917 plugin_name = m_options.plugin_name.c_str();
918
Zachary Turner97206d52017-05-12 04:51:55 +0000919 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000920 Debugger &debugger = m_interpreter.GetDebugger();
921 PlatformSP platform_sp = m_interpreter.GetPlatform(true);
922 ProcessSP process_sp = platform_sp->ConnectProcess(
923 command.GetArgumentAtIndex(0), plugin_name, debugger,
924 debugger.GetSelectedTarget().get(), error);
925 if (error.Fail() || process_sp == nullptr) {
926 result.AppendError(error.AsCString("Error connecting to the process"));
927 result.SetStatus(eReturnStatusFailed);
928 return false;
929 }
930 return true;
931 }
932
933 CommandOptions m_options;
Greg Claytonb766a732011-02-04 01:58:07 +0000934};
935
Greg Claytonb766a732011-02-04 01:58:07 +0000936//-------------------------------------------------------------------------
Greg Clayton998255b2012-10-13 02:07:45 +0000937// CommandObjectProcessPlugin
938//-------------------------------------------------------------------------
939#pragma mark CommandObjectProcessPlugin
940
Kate Stoneb9c1b512016-09-06 20:57:50 +0000941class CommandObjectProcessPlugin : public CommandObjectProxy {
Greg Clayton998255b2012-10-13 02:07:45 +0000942public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000943 CommandObjectProcessPlugin(CommandInterpreter &interpreter)
944 : CommandObjectProxy(
945 interpreter, "process plugin",
946 "Send a custom command to the current target process plug-in.",
947 "process plugin <args>", 0) {}
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000948
Kate Stoneb9c1b512016-09-06 20:57:50 +0000949 ~CommandObjectProcessPlugin() override = default;
Greg Clayton998255b2012-10-13 02:07:45 +0000950
Kate Stoneb9c1b512016-09-06 20:57:50 +0000951 CommandObject *GetProxyCommandObject() override {
952 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
953 if (process)
954 return process->GetPluginCommandObject();
955 return nullptr;
956 }
Greg Clayton998255b2012-10-13 02:07:45 +0000957};
958
Greg Clayton998255b2012-10-13 02:07:45 +0000959//-------------------------------------------------------------------------
Greg Clayton8f343b02010-11-04 01:54:29 +0000960// CommandObjectProcessLoad
961//-------------------------------------------------------------------------
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000962
963static OptionDefinition g_process_load_options[] = {
964 // clang-format off
965 { 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." },
966 // clang-format on
967};
968
Jim Inghambb9caf72010-12-09 18:58:16 +0000969#pragma mark CommandObjectProcessLoad
Greg Clayton8f343b02010-11-04 01:54:29 +0000970
Kate Stoneb9c1b512016-09-06 20:57:50 +0000971class CommandObjectProcessLoad : public CommandObjectParsed {
Greg Clayton8f343b02010-11-04 01:54:29 +0000972public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000973 class CommandOptions : public Options {
974 public:
975 CommandOptions() : Options() {
976 // Keep default values of all options in one place: OptionParsingStarting
977 // ()
978 OptionParsingStarting(nullptr);
Greg Clayton8f343b02010-11-04 01:54:29 +0000979 }
980
Kate Stoneb9c1b512016-09-06 20:57:50 +0000981 ~CommandOptions() override = default;
Tamas Berghammer4fbd67a2015-12-08 13:43:59 +0000982
Zachary Turner97206d52017-05-12 04:51:55 +0000983 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
984 ExecutionContext *execution_context) override {
985 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000986 const int short_option = m_getopt_table[option_idx].val;
987 switch (short_option) {
988 case 'i':
989 do_install = true;
Zachary Turnerfe114832016-11-12 16:56:47 +0000990 if (!option_arg.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000991 install_path.SetFile(option_arg, false);
992 break;
993 default:
994 error.SetErrorStringWithFormat("invalid short option character '%c'",
995 short_option);
996 break;
997 }
998 return error;
Greg Clayton8f343b02010-11-04 01:54:29 +0000999 }
1000
Kate Stoneb9c1b512016-09-06 20:57:50 +00001001 void OptionParsingStarting(ExecutionContext *execution_context) override {
1002 do_install = false;
1003 install_path.Clear();
1004 }
1005
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001006 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +00001007 return llvm::makeArrayRef(g_process_load_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001008 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001009
1010 // Instance variables to hold the values for command options.
1011 bool do_install;
1012 FileSpec install_path;
1013 };
1014
1015 CommandObjectProcessLoad(CommandInterpreter &interpreter)
1016 : CommandObjectParsed(interpreter, "process load",
1017 "Load a shared library into the current process.",
1018 "process load <filename> [<filename> ...]",
1019 eCommandRequiresProcess | eCommandTryTargetAPILock |
1020 eCommandProcessMustBeLaunched |
1021 eCommandProcessMustBePaused),
1022 m_options() {}
1023
1024 ~CommandObjectProcessLoad() override = default;
1025
1026 Options *GetOptions() override { return &m_options; }
1027
Jim Ingham5a988412012-06-08 21:56:10 +00001028protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001029 bool DoExecute(Args &command, CommandReturnObject &result) override {
1030 Process *process = m_exe_ctx.GetProcessPtr();
Greg Clayton8f343b02010-11-04 01:54:29 +00001031
Zachary Turner97d2c402016-10-05 23:40:23 +00001032 for (auto &entry : command.entries()) {
Zachary Turner97206d52017-05-12 04:51:55 +00001033 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001034 PlatformSP platform = process->GetTarget().GetPlatform();
Zachary Turner97d2c402016-10-05 23:40:23 +00001035 llvm::StringRef image_path = entry.ref;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001036 uint32_t image_token = LLDB_INVALID_IMAGE_TOKEN;
Tamas Berghammer4fbd67a2015-12-08 13:43:59 +00001037
Kate Stoneb9c1b512016-09-06 20:57:50 +00001038 if (!m_options.do_install) {
1039 FileSpec image_spec(image_path, false);
1040 platform->ResolveRemotePath(image_spec, image_spec);
1041 image_token =
1042 platform->LoadImage(process, FileSpec(), image_spec, error);
1043 } else if (m_options.install_path) {
1044 FileSpec image_spec(image_path, true);
1045 platform->ResolveRemotePath(m_options.install_path,
1046 m_options.install_path);
1047 image_token = platform->LoadImage(process, image_spec,
1048 m_options.install_path, error);
1049 } else {
1050 FileSpec image_spec(image_path, true);
1051 image_token =
1052 platform->LoadImage(process, image_spec, FileSpec(), error);
1053 }
Tamas Berghammer4fbd67a2015-12-08 13:43:59 +00001054
Kate Stoneb9c1b512016-09-06 20:57:50 +00001055 if (image_token != LLDB_INVALID_IMAGE_TOKEN) {
1056 result.AppendMessageWithFormat(
Zachary Turner97d2c402016-10-05 23:40:23 +00001057 "Loading \"%s\"...ok\nImage %u loaded.\n", image_path.str().c_str(),
1058 image_token);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001059 result.SetStatus(eReturnStatusSuccessFinishResult);
1060 } else {
Zachary Turner97d2c402016-10-05 23:40:23 +00001061 result.AppendErrorWithFormat("failed to load '%s': %s",
1062 image_path.str().c_str(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001063 error.AsCString());
1064 result.SetStatus(eReturnStatusFailed);
1065 }
Greg Clayton8f343b02010-11-04 01:54:29 +00001066 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001067 return result.Succeeded();
1068 }
1069
1070 CommandOptions m_options;
Greg Clayton8f343b02010-11-04 01:54:29 +00001071};
1072
Greg Clayton8f343b02010-11-04 01:54:29 +00001073//-------------------------------------------------------------------------
1074// CommandObjectProcessUnload
1075//-------------------------------------------------------------------------
Jim Inghambb9caf72010-12-09 18:58:16 +00001076#pragma mark CommandObjectProcessUnload
Greg Clayton8f343b02010-11-04 01:54:29 +00001077
Kate Stoneb9c1b512016-09-06 20:57:50 +00001078class CommandObjectProcessUnload : public CommandObjectParsed {
Greg Clayton8f343b02010-11-04 01:54:29 +00001079public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001080 CommandObjectProcessUnload(CommandInterpreter &interpreter)
1081 : CommandObjectParsed(
1082 interpreter, "process unload",
1083 "Unload a shared library from the current process using the index "
1084 "returned by a previous call to \"process load\".",
1085 "process unload <index>",
1086 eCommandRequiresProcess | eCommandTryTargetAPILock |
1087 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {}
Greg Clayton8f343b02010-11-04 01:54:29 +00001088
Kate Stoneb9c1b512016-09-06 20:57:50 +00001089 ~CommandObjectProcessUnload() override = default;
Greg Clayton8f343b02010-11-04 01:54:29 +00001090
Jim Ingham5a988412012-06-08 21:56:10 +00001091protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001092 bool DoExecute(Args &command, CommandReturnObject &result) override {
1093 Process *process = m_exe_ctx.GetProcessPtr();
Greg Clayton8f343b02010-11-04 01:54:29 +00001094
Zachary Turner97d2c402016-10-05 23:40:23 +00001095 for (auto &entry : command.entries()) {
1096 uint32_t image_token;
1097 if (entry.ref.getAsInteger(0, image_token)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001098 result.AppendErrorWithFormat("invalid image index argument '%s'",
Zachary Turner97d2c402016-10-05 23:40:23 +00001099 entry.ref.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001100 result.SetStatus(eReturnStatusFailed);
1101 break;
1102 } else {
Zachary Turner97206d52017-05-12 04:51:55 +00001103 Status error(process->GetTarget().GetPlatform()->UnloadImage(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001104 process, image_token));
1105 if (error.Success()) {
1106 result.AppendMessageWithFormat(
1107 "Unloading shared library with index %u...ok\n", image_token);
1108 result.SetStatus(eReturnStatusSuccessFinishResult);
1109 } else {
1110 result.AppendErrorWithFormat("failed to unload image: %s",
1111 error.AsCString());
1112 result.SetStatus(eReturnStatusFailed);
1113 break;
Greg Clayton8f343b02010-11-04 01:54:29 +00001114 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001115 }
Greg Clayton8f343b02010-11-04 01:54:29 +00001116 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001117 return result.Succeeded();
1118 }
Greg Clayton8f343b02010-11-04 01:54:29 +00001119};
1120
1121//-------------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001122// CommandObjectProcessSignal
1123//-------------------------------------------------------------------------
Jim Inghambb9caf72010-12-09 18:58:16 +00001124#pragma mark CommandObjectProcessSignal
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001125
Kate Stoneb9c1b512016-09-06 20:57:50 +00001126class CommandObjectProcessSignal : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001127public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001128 CommandObjectProcessSignal(CommandInterpreter &interpreter)
1129 : CommandObjectParsed(interpreter, "process signal",
1130 "Send a UNIX signal to the current target process.",
1131 nullptr, eCommandRequiresProcess |
1132 eCommandTryTargetAPILock) {
1133 CommandArgumentEntry arg;
1134 CommandArgumentData signal_arg;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001135
Kate Stoneb9c1b512016-09-06 20:57:50 +00001136 // Define the first (and only) variant of this arg.
1137 signal_arg.arg_type = eArgTypeUnixSignal;
1138 signal_arg.arg_repetition = eArgRepeatPlain;
1139
1140 // There is only one variant this argument could be; put it into the
1141 // argument entry.
1142 arg.push_back(signal_arg);
1143
1144 // Push the data for the first argument into the m_arguments vector.
1145 m_arguments.push_back(arg);
1146 }
1147
1148 ~CommandObjectProcessSignal() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001149
Jim Ingham5a988412012-06-08 21:56:10 +00001150protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001151 bool DoExecute(Args &command, CommandReturnObject &result) override {
1152 Process *process = m_exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001153
Kate Stoneb9c1b512016-09-06 20:57:50 +00001154 if (command.GetArgumentCount() == 1) {
1155 int signo = LLDB_INVALID_SIGNAL_NUMBER;
1156
1157 const char *signal_name = command.GetArgumentAtIndex(0);
1158 if (::isxdigit(signal_name[0]))
1159 signo =
1160 StringConvert::ToSInt32(signal_name, LLDB_INVALID_SIGNAL_NUMBER, 0);
1161 else
1162 signo = process->GetUnixSignals()->GetSignalNumberFromName(signal_name);
1163
1164 if (signo == LLDB_INVALID_SIGNAL_NUMBER) {
1165 result.AppendErrorWithFormat("Invalid signal argument '%s'.\n",
1166 command.GetArgumentAtIndex(0));
1167 result.SetStatus(eReturnStatusFailed);
1168 } else {
Zachary Turner97206d52017-05-12 04:51:55 +00001169 Status error(process->Signal(signo));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001170 if (error.Success()) {
1171 result.SetStatus(eReturnStatusSuccessFinishResult);
1172 } else {
1173 result.AppendErrorWithFormat("Failed to send signal %i: %s\n", signo,
1174 error.AsCString());
1175 result.SetStatus(eReturnStatusFailed);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001176 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001177 }
1178 } else {
1179 result.AppendErrorWithFormat(
1180 "'%s' takes exactly one signal number argument:\nUsage: %s\n",
1181 m_cmd_name.c_str(), m_cmd_syntax.c_str());
1182 result.SetStatus(eReturnStatusFailed);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001183 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001184 return result.Succeeded();
1185 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001186};
1187
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001188//-------------------------------------------------------------------------
1189// CommandObjectProcessInterrupt
1190//-------------------------------------------------------------------------
Jim Inghambb9caf72010-12-09 18:58:16 +00001191#pragma mark CommandObjectProcessInterrupt
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001192
Kate Stoneb9c1b512016-09-06 20:57:50 +00001193class CommandObjectProcessInterrupt : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001194public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001195 CommandObjectProcessInterrupt(CommandInterpreter &interpreter)
1196 : CommandObjectParsed(interpreter, "process interrupt",
1197 "Interrupt the current target process.",
1198 "process interrupt",
1199 eCommandRequiresProcess | eCommandTryTargetAPILock |
1200 eCommandProcessMustBeLaunched) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001201
Kate Stoneb9c1b512016-09-06 20:57:50 +00001202 ~CommandObjectProcessInterrupt() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001203
Jim Ingham5a988412012-06-08 21:56:10 +00001204protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001205 bool DoExecute(Args &command, CommandReturnObject &result) override {
1206 Process *process = m_exe_ctx.GetProcessPtr();
1207 if (process == nullptr) {
1208 result.AppendError("no process to halt");
1209 result.SetStatus(eReturnStatusFailed);
1210 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001211 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001212
1213 if (command.GetArgumentCount() == 0) {
1214 bool clear_thread_plans = true;
Zachary Turner97206d52017-05-12 04:51:55 +00001215 Status error(process->Halt(clear_thread_plans));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001216 if (error.Success()) {
1217 result.SetStatus(eReturnStatusSuccessFinishResult);
1218 } else {
1219 result.AppendErrorWithFormat("Failed to halt process: %s\n",
1220 error.AsCString());
1221 result.SetStatus(eReturnStatusFailed);
1222 }
1223 } else {
1224 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
1225 m_cmd_name.c_str(), m_cmd_syntax.c_str());
1226 result.SetStatus(eReturnStatusFailed);
1227 }
1228 return result.Succeeded();
1229 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001230};
1231
1232//-------------------------------------------------------------------------
1233// CommandObjectProcessKill
1234//-------------------------------------------------------------------------
Jim Inghambb9caf72010-12-09 18:58:16 +00001235#pragma mark CommandObjectProcessKill
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001236
Kate Stoneb9c1b512016-09-06 20:57:50 +00001237class CommandObjectProcessKill : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001238public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001239 CommandObjectProcessKill(CommandInterpreter &interpreter)
1240 : CommandObjectParsed(interpreter, "process kill",
1241 "Terminate the current target process.",
1242 "process kill",
1243 eCommandRequiresProcess | eCommandTryTargetAPILock |
1244 eCommandProcessMustBeLaunched) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001245
Kate Stoneb9c1b512016-09-06 20:57:50 +00001246 ~CommandObjectProcessKill() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001247
Jim Ingham5a988412012-06-08 21:56:10 +00001248protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001249 bool DoExecute(Args &command, CommandReturnObject &result) override {
1250 Process *process = m_exe_ctx.GetProcessPtr();
1251 if (process == nullptr) {
1252 result.AppendError("no process to kill");
1253 result.SetStatus(eReturnStatusFailed);
1254 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001255 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001256
1257 if (command.GetArgumentCount() == 0) {
Zachary Turner97206d52017-05-12 04:51:55 +00001258 Status error(process->Destroy(true));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001259 if (error.Success()) {
1260 result.SetStatus(eReturnStatusSuccessFinishResult);
1261 } else {
1262 result.AppendErrorWithFormat("Failed to kill process: %s\n",
1263 error.AsCString());
1264 result.SetStatus(eReturnStatusFailed);
1265 }
1266 } else {
1267 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
1268 m_cmd_name.c_str(), m_cmd_syntax.c_str());
1269 result.SetStatus(eReturnStatusFailed);
1270 }
1271 return result.Succeeded();
1272 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001273};
1274
1275//-------------------------------------------------------------------------
Greg Claytona2715cf2014-06-13 00:54:12 +00001276// CommandObjectProcessSaveCore
1277//-------------------------------------------------------------------------
1278#pragma mark CommandObjectProcessSaveCore
1279
Kate Stoneb9c1b512016-09-06 20:57:50 +00001280class CommandObjectProcessSaveCore : public CommandObjectParsed {
Greg Claytona2715cf2014-06-13 00:54:12 +00001281public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001282 CommandObjectProcessSaveCore(CommandInterpreter &interpreter)
1283 : CommandObjectParsed(interpreter, "process save-core",
1284 "Save the current process as a core file using an "
1285 "appropriate file type.",
1286 "process save-core FILE",
1287 eCommandRequiresProcess | eCommandTryTargetAPILock |
1288 eCommandProcessMustBeLaunched) {}
Eugene Zelenko49bcfd82016-02-23 01:43:44 +00001289
Kate Stoneb9c1b512016-09-06 20:57:50 +00001290 ~CommandObjectProcessSaveCore() override = default;
Eugene Zelenko49bcfd82016-02-23 01:43:44 +00001291
Greg Claytona2715cf2014-06-13 00:54:12 +00001292protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001293 bool DoExecute(Args &command, CommandReturnObject &result) override {
1294 ProcessSP process_sp = m_exe_ctx.GetProcessSP();
1295 if (process_sp) {
1296 if (command.GetArgumentCount() == 1) {
1297 FileSpec output_file(command.GetArgumentAtIndex(0), false);
Zachary Turner97206d52017-05-12 04:51:55 +00001298 Status error = PluginManager::SaveCore(process_sp, output_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001299 if (error.Success()) {
1300 result.SetStatus(eReturnStatusSuccessFinishResult);
1301 } else {
1302 result.AppendErrorWithFormat(
1303 "Failed to save core file for process: %s\n", error.AsCString());
1304 result.SetStatus(eReturnStatusFailed);
Greg Claytona2715cf2014-06-13 00:54:12 +00001305 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001306 } else {
1307 result.AppendErrorWithFormat("'%s' takes one arguments:\nUsage: %s\n",
1308 m_cmd_name.c_str(), m_cmd_syntax.c_str());
1309 result.SetStatus(eReturnStatusFailed);
1310 }
1311 } else {
1312 result.AppendError("invalid process");
1313 result.SetStatus(eReturnStatusFailed);
1314 return false;
Greg Claytona2715cf2014-06-13 00:54:12 +00001315 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001316
1317 return result.Succeeded();
1318 }
Greg Claytona2715cf2014-06-13 00:54:12 +00001319};
1320
1321//-------------------------------------------------------------------------
Jim Ingham4b9bea82010-06-18 01:23:09 +00001322// CommandObjectProcessStatus
1323//-------------------------------------------------------------------------
Jim Inghambb9caf72010-12-09 18:58:16 +00001324#pragma mark CommandObjectProcessStatus
1325
Kate Stoneb9c1b512016-09-06 20:57:50 +00001326class CommandObjectProcessStatus : public CommandObjectParsed {
Jim Ingham4b9bea82010-06-18 01:23:09 +00001327public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001328 CommandObjectProcessStatus(CommandInterpreter &interpreter)
1329 : CommandObjectParsed(
1330 interpreter, "process status",
1331 "Show status and stop location for the current target process.",
1332 "process status",
1333 eCommandRequiresProcess | eCommandTryTargetAPILock) {}
Jim Ingham4b9bea82010-06-18 01:23:09 +00001334
Kate Stoneb9c1b512016-09-06 20:57:50 +00001335 ~CommandObjectProcessStatus() override = default;
Jim Ingham4b9bea82010-06-18 01:23:09 +00001336
Kate Stoneb9c1b512016-09-06 20:57:50 +00001337 bool DoExecute(Args &command, CommandReturnObject &result) override {
1338 Stream &strm = result.GetOutputStream();
1339 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1340 // No need to check "process" for validity as eCommandRequiresProcess
1341 // ensures it is valid
1342 Process *process = m_exe_ctx.GetProcessPtr();
1343 const bool only_threads_with_stop_reason = true;
1344 const uint32_t start_frame = 0;
1345 const uint32_t num_frames = 1;
1346 const uint32_t num_frames_with_source = 1;
Jim Ingham6a9767c2016-11-08 20:36:40 +00001347 const bool stop_format = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001348 process->GetStatus(strm);
1349 process->GetThreadStatus(strm, only_threads_with_stop_reason, start_frame,
Jim Ingham6a9767c2016-11-08 20:36:40 +00001350 num_frames, num_frames_with_source, stop_format);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001351 return result.Succeeded();
1352 }
Jim Ingham4b9bea82010-06-18 01:23:09 +00001353};
1354
1355//-------------------------------------------------------------------------
Caroline Tice35731352010-10-13 20:44:39 +00001356// CommandObjectProcessHandle
1357//-------------------------------------------------------------------------
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001358
1359static OptionDefinition g_process_handle_options[] = {
1360 // clang-format off
1361 { 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." },
1362 { 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." },
1363 { LLDB_OPT_SET_1, false, "pass", 'p', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Whether or not the signal should be passed to the process." }
1364 // clang-format on
1365};
1366
Jim Inghambb9caf72010-12-09 18:58:16 +00001367#pragma mark CommandObjectProcessHandle
Caroline Tice35731352010-10-13 20:44:39 +00001368
Kate Stoneb9c1b512016-09-06 20:57:50 +00001369class CommandObjectProcessHandle : public CommandObjectParsed {
Caroline Tice35731352010-10-13 20:44:39 +00001370public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001371 class CommandOptions : public Options {
1372 public:
1373 CommandOptions() : Options() { OptionParsingStarting(nullptr); }
Caroline Tice35731352010-10-13 20:44:39 +00001374
Kate Stoneb9c1b512016-09-06 20:57:50 +00001375 ~CommandOptions() override = default;
Caroline Tice35731352010-10-13 20:44:39 +00001376
Zachary Turner97206d52017-05-12 04:51:55 +00001377 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1378 ExecutionContext *execution_context) override {
1379 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001380 const int short_option = m_getopt_table[option_idx].val;
Caroline Tice35731352010-10-13 20:44:39 +00001381
Kate Stoneb9c1b512016-09-06 20:57:50 +00001382 switch (short_option) {
1383 case 's':
1384 stop = option_arg;
1385 break;
1386 case 'n':
1387 notify = option_arg;
1388 break;
1389 case 'p':
1390 pass = option_arg;
1391 break;
1392 default:
1393 error.SetErrorStringWithFormat("invalid short option character '%c'",
1394 short_option);
1395 break;
1396 }
1397 return error;
Caroline Tice35731352010-10-13 20:44:39 +00001398 }
1399
Kate Stoneb9c1b512016-09-06 20:57:50 +00001400 void OptionParsingStarting(ExecutionContext *execution_context) override {
1401 stop.clear();
1402 notify.clear();
1403 pass.clear();
Caroline Tice35731352010-10-13 20:44:39 +00001404 }
1405
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001406 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +00001407 return llvm::makeArrayRef(g_process_handle_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001408 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001409
1410 // Instance variables to hold the values for command options.
1411
1412 std::string stop;
1413 std::string notify;
1414 std::string pass;
1415 };
1416
1417 CommandObjectProcessHandle(CommandInterpreter &interpreter)
1418 : CommandObjectParsed(interpreter, "process handle",
1419 "Manage LLDB handling of OS signals for the "
1420 "current target process. Defaults to showing "
1421 "current policy.",
1422 nullptr),
1423 m_options() {
1424 SetHelpLong("\nIf no signals are specified, update them all. If no update "
1425 "option is specified, list the current values.");
1426 CommandArgumentEntry arg;
1427 CommandArgumentData signal_arg;
1428
1429 signal_arg.arg_type = eArgTypeUnixSignal;
1430 signal_arg.arg_repetition = eArgRepeatStar;
1431
1432 arg.push_back(signal_arg);
1433
1434 m_arguments.push_back(arg);
1435 }
1436
1437 ~CommandObjectProcessHandle() override = default;
1438
1439 Options *GetOptions() override { return &m_options; }
1440
1441 bool VerifyCommandOptionValue(const std::string &option, int &real_value) {
1442 bool okay = true;
1443 bool success = false;
Pavel Labath47cbf4a2018-04-10 09:03:59 +00001444 bool tmp_value = OptionArgParser::ToBoolean(option, false, &success);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001445
1446 if (success && tmp_value)
1447 real_value = 1;
1448 else if (success && !tmp_value)
1449 real_value = 0;
1450 else {
1451 // If the value isn't 'true' or 'false', it had better be 0 or 1.
1452 real_value = StringConvert::ToUInt32(option.c_str(), 3);
1453 if (real_value != 0 && real_value != 1)
1454 okay = false;
Caroline Tice35731352010-10-13 20:44:39 +00001455 }
1456
Kate Stoneb9c1b512016-09-06 20:57:50 +00001457 return okay;
1458 }
Caroline Tice10ad7992010-10-14 21:31:13 +00001459
Kate Stoneb9c1b512016-09-06 20:57:50 +00001460 void PrintSignalHeader(Stream &str) {
1461 str.Printf("NAME PASS STOP NOTIFY\n");
1462 str.Printf("=========== ===== ===== ======\n");
1463 }
Caroline Tice10ad7992010-10-14 21:31:13 +00001464
Kate Stoneb9c1b512016-09-06 20:57:50 +00001465 void PrintSignal(Stream &str, int32_t signo, const char *sig_name,
1466 const UnixSignalsSP &signals_sp) {
1467 bool stop;
1468 bool suppress;
1469 bool notify;
1470
1471 str.Printf("%-11s ", sig_name);
1472 if (signals_sp->GetSignalInfo(signo, suppress, stop, notify)) {
1473 bool pass = !suppress;
1474 str.Printf("%s %s %s", (pass ? "true " : "false"),
1475 (stop ? "true " : "false"), (notify ? "true " : "false"));
Caroline Tice10ad7992010-10-14 21:31:13 +00001476 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001477 str.Printf("\n");
1478 }
Caroline Tice10ad7992010-10-14 21:31:13 +00001479
Kate Stoneb9c1b512016-09-06 20:57:50 +00001480 void PrintSignalInformation(Stream &str, Args &signal_args,
1481 int num_valid_signals,
1482 const UnixSignalsSP &signals_sp) {
1483 PrintSignalHeader(str);
1484
1485 if (num_valid_signals > 0) {
1486 size_t num_args = signal_args.GetArgumentCount();
1487 for (size_t i = 0; i < num_args; ++i) {
1488 int32_t signo = signals_sp->GetSignalNumberFromName(
1489 signal_args.GetArgumentAtIndex(i));
1490 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
1491 PrintSignal(str, signo, signal_args.GetArgumentAtIndex(i),
1492 signals_sp);
1493 }
1494 } else // Print info for ALL signals
Caroline Tice10ad7992010-10-14 21:31:13 +00001495 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001496 int32_t signo = signals_sp->GetFirstSignalNumber();
1497 while (signo != LLDB_INVALID_SIGNAL_NUMBER) {
1498 PrintSignal(str, signo, signals_sp->GetSignalAsCString(signo),
1499 signals_sp);
1500 signo = signals_sp->GetNextSignalNumber(signo);
1501 }
Caroline Tice10ad7992010-10-14 21:31:13 +00001502 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001503 }
Caroline Tice10ad7992010-10-14 21:31:13 +00001504
Jim Ingham5a988412012-06-08 21:56:10 +00001505protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001506 bool DoExecute(Args &signal_args, CommandReturnObject &result) override {
1507 TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
Caroline Tice35731352010-10-13 20:44:39 +00001508
Kate Stoneb9c1b512016-09-06 20:57:50 +00001509 if (!target_sp) {
1510 result.AppendError("No current target;"
1511 " cannot handle signals until you have a valid target "
1512 "and process.\n");
1513 result.SetStatus(eReturnStatusFailed);
1514 return false;
Caroline Tice35731352010-10-13 20:44:39 +00001515 }
1516
Kate Stoneb9c1b512016-09-06 20:57:50 +00001517 ProcessSP process_sp = target_sp->GetProcessSP();
1518
1519 if (!process_sp) {
1520 result.AppendError("No current process; cannot handle signals until you "
1521 "have a valid process.\n");
1522 result.SetStatus(eReturnStatusFailed);
1523 return false;
1524 }
1525
1526 int stop_action = -1; // -1 means leave the current setting alone
1527 int pass_action = -1; // -1 means leave the current setting alone
1528 int notify_action = -1; // -1 means leave the current setting alone
1529
1530 if (!m_options.stop.empty() &&
1531 !VerifyCommandOptionValue(m_options.stop, stop_action)) {
1532 result.AppendError("Invalid argument for command option --stop; must be "
1533 "true or false.\n");
1534 result.SetStatus(eReturnStatusFailed);
1535 return false;
1536 }
1537
1538 if (!m_options.notify.empty() &&
1539 !VerifyCommandOptionValue(m_options.notify, notify_action)) {
1540 result.AppendError("Invalid argument for command option --notify; must "
1541 "be true or false.\n");
1542 result.SetStatus(eReturnStatusFailed);
1543 return false;
1544 }
1545
1546 if (!m_options.pass.empty() &&
1547 !VerifyCommandOptionValue(m_options.pass, pass_action)) {
1548 result.AppendError("Invalid argument for command option --pass; must be "
1549 "true or false.\n");
1550 result.SetStatus(eReturnStatusFailed);
1551 return false;
1552 }
1553
1554 size_t num_args = signal_args.GetArgumentCount();
1555 UnixSignalsSP signals_sp = process_sp->GetUnixSignals();
1556 int num_signals_set = 0;
1557
1558 if (num_args > 0) {
Zachary Turnerd6a24752016-11-22 17:10:15 +00001559 for (const auto &arg : signal_args) {
1560 int32_t signo = signals_sp->GetSignalNumberFromName(arg.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001561 if (signo != LLDB_INVALID_SIGNAL_NUMBER) {
1562 // Casting the actions as bools here should be okay, because
1563 // VerifyCommandOptionValue guarantees
1564 // the value is either 0 or 1.
1565 if (stop_action != -1)
1566 signals_sp->SetShouldStop(signo, stop_action);
1567 if (pass_action != -1) {
1568 bool suppress = !pass_action;
1569 signals_sp->SetShouldSuppress(signo, suppress);
1570 }
1571 if (notify_action != -1)
1572 signals_sp->SetShouldNotify(signo, notify_action);
1573 ++num_signals_set;
1574 } else {
1575 result.AppendErrorWithFormat("Invalid signal name '%s'\n",
Zachary Turnerd6a24752016-11-22 17:10:15 +00001576 arg.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001577 }
1578 }
1579 } else {
1580 // No signal specified, if any command options were specified, update ALL
1581 // signals.
1582 if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1)) {
1583 if (m_interpreter.Confirm(
1584 "Do you really want to update all the signals?", false)) {
1585 int32_t signo = signals_sp->GetFirstSignalNumber();
1586 while (signo != LLDB_INVALID_SIGNAL_NUMBER) {
1587 if (notify_action != -1)
1588 signals_sp->SetShouldNotify(signo, notify_action);
1589 if (stop_action != -1)
1590 signals_sp->SetShouldStop(signo, stop_action);
1591 if (pass_action != -1) {
1592 bool suppress = !pass_action;
1593 signals_sp->SetShouldSuppress(signo, suppress);
1594 }
1595 signo = signals_sp->GetNextSignalNumber(signo);
1596 }
1597 }
1598 }
1599 }
1600
1601 PrintSignalInformation(result.GetOutputStream(), signal_args,
1602 num_signals_set, signals_sp);
1603
1604 if (num_signals_set > 0)
1605 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1606 else
1607 result.SetStatus(eReturnStatusFailed);
1608
1609 return result.Succeeded();
1610 }
1611
1612 CommandOptions m_options;
Caroline Tice35731352010-10-13 20:44:39 +00001613};
1614
Caroline Tice35731352010-10-13 20:44:39 +00001615//-------------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001616// CommandObjectMultiwordProcess
1617//-------------------------------------------------------------------------
1618
Kate Stoneb9c1b512016-09-06 20:57:50 +00001619CommandObjectMultiwordProcess::CommandObjectMultiwordProcess(
1620 CommandInterpreter &interpreter)
1621 : CommandObjectMultiword(
1622 interpreter, "process",
1623 "Commands for interacting with processes on the current platform.",
1624 "process <subcommand> [<subcommand-options>]") {
1625 LoadSubCommand("attach",
1626 CommandObjectSP(new CommandObjectProcessAttach(interpreter)));
1627 LoadSubCommand("launch",
1628 CommandObjectSP(new CommandObjectProcessLaunch(interpreter)));
1629 LoadSubCommand("continue", CommandObjectSP(new CommandObjectProcessContinue(
1630 interpreter)));
1631 LoadSubCommand("connect",
1632 CommandObjectSP(new CommandObjectProcessConnect(interpreter)));
1633 LoadSubCommand("detach",
1634 CommandObjectSP(new CommandObjectProcessDetach(interpreter)));
1635 LoadSubCommand("load",
1636 CommandObjectSP(new CommandObjectProcessLoad(interpreter)));
1637 LoadSubCommand("unload",
1638 CommandObjectSP(new CommandObjectProcessUnload(interpreter)));
1639 LoadSubCommand("signal",
1640 CommandObjectSP(new CommandObjectProcessSignal(interpreter)));
1641 LoadSubCommand("handle",
1642 CommandObjectSP(new CommandObjectProcessHandle(interpreter)));
1643 LoadSubCommand("status",
1644 CommandObjectSP(new CommandObjectProcessStatus(interpreter)));
1645 LoadSubCommand("interrupt", CommandObjectSP(new CommandObjectProcessInterrupt(
1646 interpreter)));
1647 LoadSubCommand("kill",
1648 CommandObjectSP(new CommandObjectProcessKill(interpreter)));
1649 LoadSubCommand("plugin",
1650 CommandObjectSP(new CommandObjectProcessPlugin(interpreter)));
1651 LoadSubCommand("save-core", CommandObjectSP(new CommandObjectProcessSaveCore(
1652 interpreter)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001653}
1654
Eugene Zelenko49bcfd82016-02-23 01:43:44 +00001655CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess() = default;