blob: b7f894f6dcf5c03f5803f313c170cae491853fb8 [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
10#include "CommandObjectProcess.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
Jim Ingham0e410842012-08-11 01:27:55 +000016#include "lldb/Breakpoint/Breakpoint.h"
17#include "lldb/Breakpoint/BreakpointLocation.h"
18#include "lldb/Breakpoint/BreakpointSite.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Core/State.h"
Greg Clayton1f746072012-08-29 21:13:06 +000020#include "lldb/Core/Module.h"
Greg Claytona2715cf2014-06-13 00:54:12 +000021#include "lldb/Core/PluginManager.h"
Greg Clayton7260f622011-04-18 08:33:37 +000022#include "lldb/Host/Host.h"
Vince Harron5275aaa2015-01-15 20:08:35 +000023#include "lldb/Host/StringConvert.h"
Jim Ingham0e410842012-08-11 01:27:55 +000024#include "lldb/Interpreter/Args.h"
25#include "lldb/Interpreter/Options.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "lldb/Interpreter/CommandInterpreter.h"
27#include "lldb/Interpreter/CommandReturnObject.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"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034
35using namespace lldb;
36using namespace lldb_private;
37
Jim Inghamdcb1d852013-03-29 00:56:30 +000038class CommandObjectProcessLaunchOrAttach : public CommandObjectParsed
39{
40public:
41 CommandObjectProcessLaunchOrAttach (CommandInterpreter &interpreter,
42 const char *name,
43 const char *help,
44 const char *syntax,
45 uint32_t flags,
46 const char *new_process_action) :
47 CommandObjectParsed (interpreter, name, help, syntax, flags),
48 m_new_process_action (new_process_action) {}
49
Bruce Mitchener13d21e92015-10-07 16:56:17 +000050 ~CommandObjectProcessLaunchOrAttach () override {}
Jim Inghamdcb1d852013-03-29 00:56:30 +000051protected:
52 bool
Greg Claytonb09c5382013-12-13 17:20:18 +000053 StopProcessIfNecessary (Process *process, StateType &state, CommandReturnObject &result)
Jim Inghamdcb1d852013-03-29 00:56:30 +000054 {
55 state = eStateInvalid;
56 if (process)
57 {
58 state = process->GetState();
59
60 if (process->IsAlive() && state != eStateConnected)
61 {
62 char message[1024];
63 if (process->GetState() == eStateAttaching)
64 ::snprintf (message, sizeof(message), "There is a pending attach, abort it and %s?", m_new_process_action.c_str());
65 else if (process->GetShouldDetach())
66 ::snprintf (message, sizeof(message), "There is a running process, detach from it and %s?", m_new_process_action.c_str());
67 else
68 ::snprintf (message, sizeof(message), "There is a running process, kill it and %s?", m_new_process_action.c_str());
69
70 if (!m_interpreter.Confirm (message, true))
71 {
72 result.SetStatus (eReturnStatusFailed);
73 return false;
74 }
75 else
76 {
77 if (process->GetShouldDetach())
78 {
Jim Inghamacff8952013-05-02 00:27:30 +000079 bool keep_stopped = false;
80 Error detach_error (process->Detach(keep_stopped));
Jim Inghamdcb1d852013-03-29 00:56:30 +000081 if (detach_error.Success())
82 {
83 result.SetStatus (eReturnStatusSuccessFinishResult);
84 process = NULL;
85 }
86 else
87 {
88 result.AppendErrorWithFormat ("Failed to detach from process: %s\n", detach_error.AsCString());
89 result.SetStatus (eReturnStatusFailed);
90 }
91 }
92 else
93 {
Jason Molendaede31932015-04-17 05:01:58 +000094 Error destroy_error (process->Destroy(false));
Jim Inghamdcb1d852013-03-29 00:56:30 +000095 if (destroy_error.Success())
96 {
97 result.SetStatus (eReturnStatusSuccessFinishResult);
98 process = NULL;
99 }
100 else
101 {
102 result.AppendErrorWithFormat ("Failed to kill process: %s\n", destroy_error.AsCString());
103 result.SetStatus (eReturnStatusFailed);
104 }
105 }
106 }
107 }
108 }
109 return result.Succeeded();
110 }
111 std::string m_new_process_action;
112};
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113//-------------------------------------------------------------------------
114// CommandObjectProcessLaunch
115//-------------------------------------------------------------------------
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000116#pragma mark CommandObjectProcessLaunch
Jim Inghamdcb1d852013-03-29 00:56:30 +0000117class CommandObjectProcessLaunch : public CommandObjectProcessLaunchOrAttach
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000118{
119public:
120
Greg Claytona7015092010-09-18 01:14:36 +0000121 CommandObjectProcessLaunch (CommandInterpreter &interpreter) :
Jim Inghamdcb1d852013-03-29 00:56:30 +0000122 CommandObjectProcessLaunchOrAttach (interpreter,
123 "process launch",
124 "Launch the executable in the debugger.",
125 NULL,
Enrico Granatae87764f2015-05-27 05:04:35 +0000126 eCommandRequiresTarget,
Jim Inghamdcb1d852013-03-29 00:56:30 +0000127 "restart"),
Greg Claytoneb0103f2011-04-07 22:46:35 +0000128 m_options (interpreter)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129 {
Caroline Tice405fe672010-10-04 22:28:36 +0000130 CommandArgumentEntry arg;
131 CommandArgumentData run_args_arg;
132
133 // Define the first (and only) variant of this arg.
134 run_args_arg.arg_type = eArgTypeRunArgs;
135 run_args_arg.arg_repetition = eArgRepeatOptional;
136
137 // There is only one variant this argument could be; put it into the argument entry.
138 arg.push_back (run_args_arg);
139
140 // Push the data for the first argument into the m_arguments vector.
141 m_arguments.push_back (arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142 }
143
144
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000145 ~CommandObjectProcessLaunch () override
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000146 {
147 }
148
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000149 int
Jim Inghame9ce62b2012-08-10 21:48:41 +0000150 HandleArgumentCompletion (Args &input,
151 int &cursor_index,
152 int &cursor_char_position,
153 OptionElementVector &opt_element_vector,
154 int match_start_point,
155 int max_return_elements,
156 bool &word_complete,
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000157 StringList &matches) override
Jim Inghame9ce62b2012-08-10 21:48:41 +0000158 {
159 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
160 completion_str.erase (cursor_char_position);
161
162 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
163 CommandCompletions::eDiskFileCompletion,
164 completion_str.c_str(),
165 match_start_point,
166 max_return_elements,
167 NULL,
168 word_complete,
169 matches);
170 return matches.GetSize();
171 }
172
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000173 Options *
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000174 GetOptions () override
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000175 {
176 return &m_options;
177 }
178
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000179 const char *
180 GetRepeatCommand (Args &current_command_args, uint32_t index) override
Jim Ingham5a988412012-06-08 21:56:10 +0000181 {
182 // No repeat for "process launch"...
183 return "";
184 }
185
186protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000187 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000188 DoExecute (Args& launch_args, CommandReturnObject &result) override
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000189 {
Greg Clayton1d885962011-11-08 02:43:13 +0000190 Debugger &debugger = m_interpreter.GetDebugger();
191 Target *target = debugger.GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192 // If our listener is NULL, users aren't allows to launch
Greg Claytonb09c5382013-12-13 17:20:18 +0000193 ModuleSP exe_module_sp = target->GetExecutableModule();
Greg Clayton71337622011-02-24 22:24:29 +0000194
Greg Claytonb09c5382013-12-13 17:20:18 +0000195 if (exe_module_sp == NULL)
Greg Clayton71337622011-02-24 22:24:29 +0000196 {
Greg Claytoneffe5c92011-05-03 22:09:39 +0000197 result.AppendError ("no file in target, create a debug target using the 'target create' command");
Greg Clayton71337622011-02-24 22:24:29 +0000198 result.SetStatus (eReturnStatusFailed);
199 return false;
200 }
201
Greg Clayton71337622011-02-24 22:24:29 +0000202 StateType state = eStateInvalid;
Greg Clayton71337622011-02-24 22:24:29 +0000203
Greg Claytonb09c5382013-12-13 17:20:18 +0000204 if (!StopProcessIfNecessary(m_exe_ctx.GetProcessPtr(), state, result))
Jim Inghamdcb1d852013-03-29 00:56:30 +0000205 return false;
Jim Inghambb9caf72010-12-09 18:58:16 +0000206
Greg Clayton45392552012-10-17 22:57:12 +0000207 const char *target_settings_argv0 = target->GetArg0();
208
Todd Fiala51637922014-08-19 17:40:43 +0000209 // Determine whether we will disable ASLR or leave it in the default state (i.e. enabled if the platform supports it).
210 // First check if the process launch options explicitly turn on/off disabling ASLR. If so, use that setting;
211 // otherwise, use the 'settings target.disable-aslr' setting.
212 bool disable_aslr = false;
213 if (m_options.disable_aslr != eLazyBoolCalculate)
214 {
215 // The user specified an explicit setting on the process launch line. Use it.
216 disable_aslr = (m_options.disable_aslr == eLazyBoolYes);
217 }
218 else
219 {
220 // The user did not explicitly specify whether to disable ASLR. Fall back to the target.disable-aslr setting.
221 disable_aslr = target->GetDisableASLR ();
222 }
223
224 if (disable_aslr)
Greg Claytonb09c5382013-12-13 17:20:18 +0000225 m_options.launch_info.GetFlags().Set (eLaunchFlagDisableASLR);
Todd Fiala51637922014-08-19 17:40:43 +0000226 else
227 m_options.launch_info.GetFlags().Clear (eLaunchFlagDisableASLR);
Greg Clayton45392552012-10-17 22:57:12 +0000228
Jim Ingham106d0282014-06-25 02:32:56 +0000229 if (target->GetDetachOnError())
230 m_options.launch_info.GetFlags().Set (eLaunchFlagDetachOnError);
231
Greg Claytonb09c5382013-12-13 17:20:18 +0000232 if (target->GetDisableSTDIO())
233 m_options.launch_info.GetFlags().Set (eLaunchFlagDisableSTDIO);
234
235 Args environment;
236 target->GetEnvironmentAsArgs (environment);
237 if (environment.GetArgumentCount() > 0)
238 m_options.launch_info.GetEnvironmentEntries ().AppendArguments (environment);
239
Greg Clayton45392552012-10-17 22:57:12 +0000240 if (target_settings_argv0)
241 {
242 m_options.launch_info.GetArguments().AppendArgument (target_settings_argv0);
Greg Claytonb09c5382013-12-13 17:20:18 +0000243 m_options.launch_info.SetExecutableFile(exe_module_sp->GetPlatformFileSpec(), false);
Greg Clayton45392552012-10-17 22:57:12 +0000244 }
245 else
246 {
Greg Claytonb09c5382013-12-13 17:20:18 +0000247 m_options.launch_info.SetExecutableFile(exe_module_sp->GetPlatformFileSpec(), true);
Greg Clayton45392552012-10-17 22:57:12 +0000248 }
249
Greg Clayton144f3a92011-11-15 03:53:30 +0000250 if (launch_args.GetArgumentCount() == 0)
251 {
Ilia Kcc39d3f2015-02-13 17:07:55 +0000252 m_options.launch_info.GetArguments().AppendArguments (target->GetProcessLaunchInfo().GetArguments());
Greg Clayton144f3a92011-11-15 03:53:30 +0000253 }
254 else
Greg Clayton1d885962011-11-08 02:43:13 +0000255 {
Greg Clayton45392552012-10-17 22:57:12 +0000256 m_options.launch_info.GetArguments().AppendArguments (launch_args);
Greg Clayton162b5972011-11-21 21:51:18 +0000257 // Save the arguments for subsequent runs in the current target.
258 target->SetRunArguments (launch_args);
Greg Clayton1d885962011-11-08 02:43:13 +0000259 }
Greg Claytondc6224e2014-10-21 01:00:42 +0000260
261 StreamString stream;
Greg Clayton8012cad2014-11-17 19:39:20 +0000262 Error error = target->Launch(m_options.launch_info, &stream);
Jim Inghamdcb1d852013-03-29 00:56:30 +0000263
Greg Claytona7015092010-09-18 01:14:36 +0000264 if (error.Success())
265 {
Greg Claytonb09c5382013-12-13 17:20:18 +0000266 ProcessSP process_sp (target->GetProcessSP());
267 if (process_sp)
Greg Claytona7015092010-09-18 01:14:36 +0000268 {
Ilia K8f0db3e2015-05-07 06:26:27 +0000269 // There is a race condition where this thread will return up the call stack to the main command
270 // handler and show an (lldb) prompt before HandlePrivateEvent (from PrivateStateThread) has
271 // a chance to call PushProcessIOHandler().
Pavel Labath44464872015-05-27 12:40:32 +0000272 process_sp->SyncIOHandler (0, 2000);
Ilia K8f0db3e2015-05-07 06:26:27 +0000273
Stephane Sezerf2ef94e2014-12-13 05:23:51 +0000274 const char *data = stream.GetData();
275 if (data && strlen(data) > 0)
Greg Claytondc6224e2014-10-21 01:00:42 +0000276 result.AppendMessage(stream.GetData());
Ilia K8f0db3e2015-05-07 06:26:27 +0000277 const char *archname = exe_module_sp->GetArchitecture().GetArchitectureName();
Greg Claytonb09c5382013-12-13 17:20:18 +0000278 result.AppendMessageWithFormat ("Process %" PRIu64 " launched: '%s' (%s)\n", process_sp->GetID(), exe_module_sp->GetFileSpec().GetPath().c_str(), archname);
279 result.SetStatus (eReturnStatusSuccessFinishResult);
280 result.SetDidChangeProcessState (true);
281 }
282 else
283 {
284 result.AppendError("no error returned from Target::Launch, and target has no process");
285 result.SetStatus (eReturnStatusFailed);
Greg Claytona7015092010-09-18 01:14:36 +0000286 }
287 }
Greg Clayton514487e2011-02-15 21:59:32 +0000288 else
289 {
Greg Claytonb09c5382013-12-13 17:20:18 +0000290 result.AppendError(error.AsCString());
Greg Clayton514487e2011-02-15 21:59:32 +0000291 result.SetStatus (eReturnStatusFailed);
292 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000293 return result.Succeeded();
294 }
295
296protected:
Greg Clayton982c9762011-11-03 21:22:33 +0000297 ProcessLaunchCommandOptions m_options;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000298};
299
300
Greg Clayton982c9762011-11-03 21:22:33 +0000301//#define SET1 LLDB_OPT_SET_1
302//#define SET2 LLDB_OPT_SET_2
303//#define SET3 LLDB_OPT_SET_3
304//
305//OptionDefinition
306//CommandObjectProcessLaunch::CommandOptions::g_option_table[] =
307//{
Virgile Belloe2607b52013-09-05 16:42:23 +0000308//{ SET1 | SET2 | SET3, false, "stop-at-entry", 's', OptionParser::eNoArgument, NULL, 0, eArgTypeNone, "Stop at the entry point of the program when launching a process."},
309//{ SET1 , false, "stdin", 'i', OptionParser::eRequiredArgument, NULL, 0, eArgTypeDirectoryName, "Redirect stdin for the process to <path>."},
310//{ SET1 , false, "stdout", 'o', OptionParser::eRequiredArgument, NULL, 0, eArgTypeDirectoryName, "Redirect stdout for the process to <path>."},
311//{ SET1 , false, "stderr", 'e', OptionParser::eRequiredArgument, NULL, 0, eArgTypeDirectoryName, "Redirect stderr for the process to <path>."},
312//{ SET1 | SET2 | SET3, false, "plugin", 'p', OptionParser::eRequiredArgument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
313//{ SET2 , false, "tty", 't', OptionParser::eOptionalArgument, NULL, 0, eArgTypeDirectoryName, "Start the process in a terminal. If <path> is specified, look for a terminal whose name contains <path>, else start the process in a new terminal."},
314//{ SET3, false, "no-stdio", 'n', OptionParser::eNoArgument, NULL, 0, eArgTypeNone, "Do not set up for terminal I/O to go to running process."},
315//{ SET1 | SET2 | SET3, false, "working-dir", 'w', OptionParser::eRequiredArgument, NULL, 0, eArgTypeDirectoryName, "Set the current working directory to <path> when running the inferior."},
Greg Clayton982c9762011-11-03 21:22:33 +0000316//{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
317//};
318//
319//#undef SET1
320//#undef SET2
321//#undef SET3
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000322
323//-------------------------------------------------------------------------
324// CommandObjectProcessAttach
325//-------------------------------------------------------------------------
Jim Inghambb9caf72010-12-09 18:58:16 +0000326#pragma mark CommandObjectProcessAttach
Jim Inghamdcb1d852013-03-29 00:56:30 +0000327class CommandObjectProcessAttach : public CommandObjectProcessLaunchOrAttach
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000328{
329public:
330
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000331 class CommandOptions : public Options
332 {
333 public:
334
Greg Claytoneb0103f2011-04-07 22:46:35 +0000335 CommandOptions (CommandInterpreter &interpreter) :
336 Options(interpreter)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000337 {
Greg Claytonf6b8b582011-04-13 00:18:08 +0000338 // Keep default values of all options in one place: OptionParsingStarting ()
339 OptionParsingStarting ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000340 }
341
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000342 ~CommandOptions () override
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343 {
344 }
345
346 Error
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000347 SetOptionValue (uint32_t option_idx, const char *option_arg) override
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000348 {
349 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000350 const int short_option = m_getopt_table[option_idx].val;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000351 bool success = false;
352 switch (short_option)
353 {
Johnny Chena95ce622012-05-24 00:43:00 +0000354 case 'c':
355 attach_info.SetContinueOnceAttached(true);
356 break;
357
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358 case 'p':
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000359 {
Vince Harron5275aaa2015-01-15 20:08:35 +0000360 lldb::pid_t pid = StringConvert::ToUInt32 (option_arg, LLDB_INVALID_PROCESS_ID, 0, &success);
Greg Clayton144f3a92011-11-15 03:53:30 +0000361 if (!success || pid == LLDB_INVALID_PROCESS_ID)
362 {
363 error.SetErrorStringWithFormat("invalid process ID '%s'", option_arg);
364 }
365 else
366 {
367 attach_info.SetProcessID (pid);
368 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369 }
370 break;
371
372 case 'P':
Greg Clayton144f3a92011-11-15 03:53:30 +0000373 attach_info.SetProcessPluginName (option_arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000374 break;
375
376 case 'n':
Greg Clayton144f3a92011-11-15 03:53:30 +0000377 attach_info.GetExecutableFile().SetFile(option_arg, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000378 break;
379
380 case 'w':
Greg Clayton144f3a92011-11-15 03:53:30 +0000381 attach_info.SetWaitForLaunch(true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000382 break;
Jim Inghamcd16df92012-07-20 21:37:13 +0000383
384 case 'i':
385 attach_info.SetIgnoreExisting(false);
386 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000387
388 default:
Greg Clayton86edbf42011-10-26 00:56:27 +0000389 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390 break;
391 }
392 return error;
393 }
394
395 void
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000396 OptionParsingStarting () override
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000397 {
Greg Clayton144f3a92011-11-15 03:53:30 +0000398 attach_info.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399 }
400
Greg Claytone0d378b2011-03-24 21:19:54 +0000401 const OptionDefinition*
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000402 GetDefinitions () override
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403 {
404 return g_option_table;
405 }
406
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000407 bool
Greg Claytoneb0103f2011-04-07 22:46:35 +0000408 HandleOptionArgumentCompletion (Args &input,
Jim Ingham5aee1622010-08-09 23:31:02 +0000409 int cursor_index,
410 int char_pos,
411 OptionElementVector &opt_element_vector,
412 int opt_element_index,
413 int match_start_point,
414 int max_return_elements,
415 bool &word_complete,
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000416 StringList &matches) override
Jim Ingham5aee1622010-08-09 23:31:02 +0000417 {
418 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
419 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
420
421 // We are only completing the name option for now...
422
Greg Claytone0d378b2011-03-24 21:19:54 +0000423 const OptionDefinition *opt_defs = GetDefinitions();
Jim Ingham5aee1622010-08-09 23:31:02 +0000424 if (opt_defs[opt_defs_index].short_option == 'n')
425 {
426 // Are we in the name?
427
428 // Look to see if there is a -P argument provided, and if so use that plugin, otherwise
429 // use the default plugin.
Jim Ingham5aee1622010-08-09 23:31:02 +0000430
431 const char *partial_name = NULL;
432 partial_name = input.GetArgumentAtIndex(opt_arg_pos);
Greg Claytone996fd32011-03-08 22:40:15 +0000433
Greg Clayton8b82f082011-04-12 05:54:46 +0000434 PlatformSP platform_sp (m_interpreter.GetPlatform (true));
Greg Claytone996fd32011-03-08 22:40:15 +0000435 if (platform_sp)
Jim Ingham5aee1622010-08-09 23:31:02 +0000436 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000437 ProcessInstanceInfoList process_infos;
438 ProcessInstanceInfoMatch match_info;
Greg Clayton32e0a752011-03-30 18:16:51 +0000439 if (partial_name)
440 {
Greg Clayton144f3a92011-11-15 03:53:30 +0000441 match_info.GetProcessInfo().GetExecutableFile().SetFile(partial_name, false);
Greg Clayton32e0a752011-03-30 18:16:51 +0000442 match_info.SetNameMatchType(eNameMatchStartsWith);
443 }
444 platform_sp->FindProcesses (match_info, process_infos);
Greg Claytonc7bece562013-01-25 18:06:21 +0000445 const size_t num_matches = process_infos.GetSize();
Greg Claytone996fd32011-03-08 22:40:15 +0000446 if (num_matches > 0)
447 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000448 for (size_t i=0; i<num_matches; ++i)
Greg Claytone996fd32011-03-08 22:40:15 +0000449 {
450 matches.AppendString (process_infos.GetProcessNameAtIndex(i),
451 process_infos.GetProcessNameLengthAtIndex(i));
452 }
453 }
Jim Ingham5aee1622010-08-09 23:31:02 +0000454 }
455 }
456
457 return false;
458 }
459
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000460 // Options table: Required for subclasses of Options.
461
Greg Claytone0d378b2011-03-24 21:19:54 +0000462 static OptionDefinition g_option_table[];
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463
464 // Instance variables to hold the values for command options.
465
Greg Clayton144f3a92011-11-15 03:53:30 +0000466 ProcessAttachInfo attach_info;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000467 };
468
Greg Claytona7015092010-09-18 01:14:36 +0000469 CommandObjectProcessAttach (CommandInterpreter &interpreter) :
Jim Inghamdcb1d852013-03-29 00:56:30 +0000470 CommandObjectProcessLaunchOrAttach (interpreter,
471 "process attach",
472 "Attach to a process.",
473 "process attach <cmd-options>",
474 0,
475 "attach"),
Greg Claytoneb0103f2011-04-07 22:46:35 +0000476 m_options (interpreter)
Jim Ingham5aee1622010-08-09 23:31:02 +0000477 {
Jim Ingham5aee1622010-08-09 23:31:02 +0000478 }
479
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000480 ~CommandObjectProcessAttach () override
Jim Ingham5aee1622010-08-09 23:31:02 +0000481 {
482 }
483
Jim Ingham5a988412012-06-08 21:56:10 +0000484 Options *
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000485 GetOptions () override
Jim Ingham5a988412012-06-08 21:56:10 +0000486 {
487 return &m_options;
488 }
489
490protected:
Jim Ingham5aee1622010-08-09 23:31:02 +0000491 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000492 DoExecute (Args& command, CommandReturnObject &result) override
Jim Ingham5aee1622010-08-09 23:31:02 +0000493 {
Oleksiy Vyalov926af0c2015-02-03 00:04:35 +0000494 PlatformSP platform_sp (m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
495
Greg Claytona7015092010-09-18 01:14:36 +0000496 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Jim Ingham31412642011-09-15 01:08:57 +0000497 // N.B. The attach should be synchronous. It doesn't help much to get the prompt back between initiating the attach
498 // and the target actually stopping. So even if the interpreter is set to be asynchronous, we wait for the stop
499 // ourselves here.
Jim Inghambb3a2832011-01-29 01:49:25 +0000500
Greg Clayton71337622011-02-24 22:24:29 +0000501 StateType state = eStateInvalid;
Jim Inghamdcb1d852013-03-29 00:56:30 +0000502 Process *process = m_exe_ctx.GetProcessPtr();
503
504 if (!StopProcessIfNecessary (process, state, result))
505 return false;
506
Jim Ingham5aee1622010-08-09 23:31:02 +0000507 if (target == NULL)
508 {
509 // If there isn't a current target create one.
510 TargetSP new_target_sp;
Jim Ingham5aee1622010-08-09 23:31:02 +0000511 Error error;
512
Greg Claytona7015092010-09-18 01:14:36 +0000513 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
Greg Claytona0ca6602012-10-18 16:33:33 +0000514 NULL,
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000515 NULL,
Greg Claytona7015092010-09-18 01:14:36 +0000516 false,
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000517 NULL, // No platform options
Greg Claytona7015092010-09-18 01:14:36 +0000518 new_target_sp);
Jim Ingham5aee1622010-08-09 23:31:02 +0000519 target = new_target_sp.get();
520 if (target == NULL || error.Fail())
521 {
Greg Claytonb766a732011-02-04 01:58:07 +0000522 result.AppendError(error.AsCString("Error creating target"));
Jim Ingham5aee1622010-08-09 23:31:02 +0000523 return false;
524 }
Greg Claytona7015092010-09-18 01:14:36 +0000525 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target);
Jim Ingham5aee1622010-08-09 23:31:02 +0000526 }
527
528 // Record the old executable module, we want to issue a warning if the process of attaching changed the
529 // current executable (like somebody said "file foo" then attached to a PID whose executable was bar.)
530
531 ModuleSP old_exec_module_sp = target->GetExecutableModule();
532 ArchSpec old_arch_spec = target->GetArchitecture();
533
534 if (command.GetArgumentCount())
535 {
Jason Molendafd54b362011-09-20 21:44:10 +0000536 result.AppendErrorWithFormat("Invalid arguments for '%s'.\nUsage: %s\n", m_cmd_name.c_str(), m_cmd_syntax.c_str());
Jim Ingham5aee1622010-08-09 23:31:02 +0000537 result.SetStatus (eReturnStatusFailed);
Oleksiy Vyalov926af0c2015-02-03 00:04:35 +0000538 return false;
539 }
540
541 m_interpreter.UpdateExecutionContext(nullptr);
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000542 StreamString stream;
543 const auto error = target->Attach(m_options.attach_info, &stream);
Oleksiy Vyalov926af0c2015-02-03 00:04:35 +0000544 if (error.Success())
545 {
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000546 ProcessSP process_sp (target->GetProcessSP());
547 if (process_sp)
Oleksiy Vyalov926af0c2015-02-03 00:04:35 +0000548 {
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000549 if (stream.GetData())
550 result.AppendMessage(stream.GetData());
Oleksiy Vyalov926af0c2015-02-03 00:04:35 +0000551 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000552 result.SetDidChangeProcessState (true);
Oleksiy Vyalov926af0c2015-02-03 00:04:35 +0000553 }
554 else
555 {
Oleksiy Vyalov37386142015-02-10 22:49:57 +0000556 result.AppendError("no error returned from Target::Attach, and target has no process");
Oleksiy Vyalov926af0c2015-02-03 00:04:35 +0000557 result.SetStatus (eReturnStatusFailed);
558 }
Jim Ingham5aee1622010-08-09 23:31:02 +0000559 }
560 else
561 {
Oleksiy Vyalov926af0c2015-02-03 00:04:35 +0000562 result.AppendErrorWithFormat ("attach failed: %s\n", error.AsCString());
563 result.SetStatus (eReturnStatusFailed);
Jim Ingham5aee1622010-08-09 23:31:02 +0000564 }
Oleksiy Vyalov926af0c2015-02-03 00:04:35 +0000565
566 if (!result.Succeeded())
567 return false;
568
569 // Okay, we're done. Last step is to warn if the executable module has changed:
570 char new_path[PATH_MAX];
571 ModuleSP new_exec_module_sp (target->GetExecutableModule());
572 if (!old_exec_module_sp)
Jim Ingham5aee1622010-08-09 23:31:02 +0000573 {
Oleksiy Vyalov926af0c2015-02-03 00:04:35 +0000574 // We might not have a module if we attached to a raw pid...
575 if (new_exec_module_sp)
Jim Ingham5aee1622010-08-09 23:31:02 +0000576 {
Oleksiy Vyalov926af0c2015-02-03 00:04:35 +0000577 new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
578 result.AppendMessageWithFormat("Executable module set to \"%s\".\n", new_path);
Jim Ingham5aee1622010-08-09 23:31:02 +0000579 }
Jim Ingham5aee1622010-08-09 23:31:02 +0000580 }
Oleksiy Vyalov926af0c2015-02-03 00:04:35 +0000581 else if (old_exec_module_sp->GetFileSpec() != new_exec_module_sp->GetFileSpec())
582 {
583 char old_path[PATH_MAX];
584
585 old_exec_module_sp->GetFileSpec().GetPath (old_path, PATH_MAX);
586 new_exec_module_sp->GetFileSpec().GetPath (new_path, PATH_MAX);
587
588 result.AppendWarningWithFormat("Executable module changed from \"%s\" to \"%s\".\n",
589 old_path, new_path);
590 }
591
592 if (!old_arch_spec.IsValid())
593 {
594 result.AppendMessageWithFormat ("Architecture set to: %s.\n", target->GetArchitecture().GetTriple().getTriple().c_str());
595 }
596 else if (!old_arch_spec.IsExactMatch(target->GetArchitecture()))
597 {
598 result.AppendWarningWithFormat("Architecture changed from %s to %s.\n",
599 old_arch_spec.GetTriple().getTriple().c_str(),
600 target->GetArchitecture().GetTriple().getTriple().c_str());
601 }
602
603 // This supports the use-case scenario of immediately continuing the process once attached.
604 if (m_options.attach_info.GetContinueOnceAttached())
605 m_interpreter.HandleCommand("process continue", eLazyBoolNo, result);
606
Jim Ingham5aee1622010-08-09 23:31:02 +0000607 return result.Succeeded();
608 }
609
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000610 CommandOptions m_options;
611};
612
613
Greg Claytone0d378b2011-03-24 21:19:54 +0000614OptionDefinition
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000615CommandObjectProcessAttach::CommandOptions::g_option_table[] =
616{
Zachary Turnerd37221d2014-07-09 16:31:49 +0000617{ LLDB_OPT_SET_ALL, false, "continue",'c', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Immediately continue the process once attached."},
618{ LLDB_OPT_SET_ALL, false, "plugin", 'P', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
619{ LLDB_OPT_SET_1, false, "pid", 'p', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypePid, "The process ID of an existing process to attach to."},
620{ LLDB_OPT_SET_2, false, "name", 'n', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeProcessName, "The name of the process to attach to."},
621{ LLDB_OPT_SET_2, false, "include-existing", 'i', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Include existing processes when doing attach -w."},
622{ LLDB_OPT_SET_2, false, "waitfor", 'w', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Wait for the process with <process-name> to launch."},
623{ 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000624};
625
626//-------------------------------------------------------------------------
627// CommandObjectProcessContinue
628//-------------------------------------------------------------------------
Jim Inghambb9caf72010-12-09 18:58:16 +0000629#pragma mark CommandObjectProcessContinue
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000630
Jim Ingham5a988412012-06-08 21:56:10 +0000631class CommandObjectProcessContinue : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000632{
633public:
634
Greg Claytona7015092010-09-18 01:14:36 +0000635 CommandObjectProcessContinue (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000636 CommandObjectParsed (interpreter,
637 "process continue",
638 "Continue execution of all threads in the current process.",
639 "process continue",
Enrico Granatae87764f2015-05-27 05:04:35 +0000640 eCommandRequiresProcess |
641 eCommandTryTargetAPILock |
642 eCommandProcessMustBeLaunched |
643 eCommandProcessMustBePaused ),
Jim Ingham0e410842012-08-11 01:27:55 +0000644 m_options(interpreter)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000645 {
646 }
647
648
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000649 ~CommandObjectProcessContinue () override
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000650 {
651 }
652
Jim Ingham5a988412012-06-08 21:56:10 +0000653protected:
Jim Ingham0e410842012-08-11 01:27:55 +0000654
655 class CommandOptions : public Options
656 {
657 public:
658
659 CommandOptions (CommandInterpreter &interpreter) :
660 Options(interpreter)
661 {
662 // Keep default values of all options in one place: OptionParsingStarting ()
663 OptionParsingStarting ();
664 }
665
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000666 ~CommandOptions () override
Jim Ingham0e410842012-08-11 01:27:55 +0000667 {
668 }
669
670 Error
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000671 SetOptionValue (uint32_t option_idx, const char *option_arg) override
Jim Ingham0e410842012-08-11 01:27:55 +0000672 {
673 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000674 const int short_option = m_getopt_table[option_idx].val;
Jim Ingham0e410842012-08-11 01:27:55 +0000675 bool success = false;
676 switch (short_option)
677 {
678 case 'i':
Vince Harron5275aaa2015-01-15 20:08:35 +0000679 m_ignore = StringConvert::ToUInt32 (option_arg, 0, 0, &success);
Jim Ingham0e410842012-08-11 01:27:55 +0000680 if (!success)
681 error.SetErrorStringWithFormat ("invalid value for ignore option: \"%s\", should be a number.", option_arg);
682 break;
683
684 default:
685 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
686 break;
687 }
688 return error;
689 }
690
691 void
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000692 OptionParsingStarting () override
Jim Ingham0e410842012-08-11 01:27:55 +0000693 {
694 m_ignore = 0;
695 }
696
697 const OptionDefinition*
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000698 GetDefinitions () override
Jim Ingham0e410842012-08-11 01:27:55 +0000699 {
700 return g_option_table;
701 }
702
703 // Options table: Required for subclasses of Options.
704
705 static OptionDefinition g_option_table[];
706
707 uint32_t m_ignore;
708 };
709
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000710 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000711 DoExecute (Args& command, CommandReturnObject &result) override
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000712 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000713 Process *process = m_exe_ctx.GetProcessPtr();
Greg Claytona7015092010-09-18 01:14:36 +0000714 bool synchronous_execution = m_interpreter.GetSynchronous ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000715 StateType state = process->GetState();
716 if (state == eStateStopped)
717 {
718 if (command.GetArgumentCount() != 0)
719 {
720 result.AppendErrorWithFormat ("The '%s' command does not take any arguments.\n", m_cmd_name.c_str());
721 result.SetStatus (eReturnStatusFailed);
722 return false;
723 }
724
Jim Ingham0e410842012-08-11 01:27:55 +0000725 if (m_options.m_ignore > 0)
726 {
727 ThreadSP sel_thread_sp(process->GetThreadList().GetSelectedThread());
728 if (sel_thread_sp)
729 {
730 StopInfoSP stop_info_sp = sel_thread_sp->GetStopInfo();
731 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint)
732 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000733 lldb::break_id_t bp_site_id = (lldb::break_id_t)stop_info_sp->GetValue();
Jim Ingham0e410842012-08-11 01:27:55 +0000734 BreakpointSiteSP bp_site_sp(process->GetBreakpointSiteList().FindByID(bp_site_id));
735 if (bp_site_sp)
736 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000737 const size_t num_owners = bp_site_sp->GetNumberOfOwners();
738 for (size_t i = 0; i < num_owners; i++)
Jim Ingham0e410842012-08-11 01:27:55 +0000739 {
740 Breakpoint &bp_ref = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
741 if (!bp_ref.IsInternal())
742 {
743 bp_ref.SetIgnoreCount(m_options.m_ignore);
744 }
745 }
746 }
747 }
748 }
749 }
750
Jim Ingham41f2b942012-09-10 20:50:15 +0000751 { // Scope for thread list mutex:
752 Mutex::Locker locker (process->GetThreadList().GetMutex());
753 const uint32_t num_threads = process->GetThreadList().GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000754
Jim Ingham41f2b942012-09-10 20:50:15 +0000755 // Set the actions that the threads should each take when resuming
756 for (uint32_t idx=0; idx<num_threads; ++idx)
757 {
Jim Ingham6c9ed912014-04-03 01:26:14 +0000758 const bool override_suspend = false;
759 process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState (eStateRunning, override_suspend);
Jim Ingham41f2b942012-09-10 20:50:15 +0000760 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000761 }
Todd Fialaa3b89e22014-08-12 14:33:19 +0000762
Pavel Labath44464872015-05-27 12:40:32 +0000763 const uint32_t iohandler_id = process->GetIOHandlerID();
764
Greg Claytondc6224e2014-10-21 01:00:42 +0000765 StreamString stream;
766 Error error;
767 if (synchronous_execution)
768 error = process->ResumeSynchronous (&stream);
769 else
770 error = process->Resume ();
Todd Fialaa3b89e22014-08-12 14:33:19 +0000771
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000772 if (error.Success())
773 {
Todd Fialaa3b89e22014-08-12 14:33:19 +0000774 // There is a race condition where this thread will return up the call stack to the main command
Pavel Labath44464872015-05-27 12:40:32 +0000775 // handler and show an (lldb) prompt before HandlePrivateEvent (from PrivateStateThread) has
776 // a chance to call PushProcessIOHandler().
777 process->SyncIOHandler(iohandler_id, 2000);
Todd Fialaa3b89e22014-08-12 14:33:19 +0000778
Daniel Malead01b2952012-11-29 21:49:15 +0000779 result.AppendMessageWithFormat ("Process %" PRIu64 " resuming\n", process->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000780 if (synchronous_execution)
781 {
Greg Claytondc6224e2014-10-21 01:00:42 +0000782 // If any state changed events had anything to say, add that to the result
783 if (stream.GetData())
784 result.AppendMessage(stream.GetData());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000785
786 result.SetDidChangeProcessState (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000787 result.SetStatus (eReturnStatusSuccessFinishNoResult);
788 }
789 else
790 {
791 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
792 }
793 }
794 else
795 {
796 result.AppendErrorWithFormat("Failed to resume process: %s.\n", error.AsCString());
797 result.SetStatus (eReturnStatusFailed);
798 }
799 }
800 else
801 {
802 result.AppendErrorWithFormat ("Process cannot be continued from its current state (%s).\n",
803 StateAsCString(state));
804 result.SetStatus (eReturnStatusFailed);
805 }
806 return result.Succeeded();
807 }
Jim Ingham0e410842012-08-11 01:27:55 +0000808
809 Options *
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000810 GetOptions () override
Jim Ingham0e410842012-08-11 01:27:55 +0000811 {
812 return &m_options;
813 }
814
815 CommandOptions m_options;
816
817};
818
819OptionDefinition
820CommandObjectProcessContinue::CommandOptions::g_option_table[] =
821{
Zachary Turnerd37221d2014-07-09 16:31:49 +0000822{ LLDB_OPT_SET_ALL, false, "ignore-count",'i', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeUnsignedInteger,
Jim Ingham0e410842012-08-11 01:27:55 +0000823 "Ignore <N> crossings of the breakpoint (if it exists) for the currently selected thread."},
Zachary Turnerd37221d2014-07-09 16:31:49 +0000824{ 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000825};
826
827//-------------------------------------------------------------------------
828// CommandObjectProcessDetach
829//-------------------------------------------------------------------------
Jim Inghambb9caf72010-12-09 18:58:16 +0000830#pragma mark CommandObjectProcessDetach
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000831
Jim Ingham5a988412012-06-08 21:56:10 +0000832class CommandObjectProcessDetach : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000833{
834public:
Jim Inghamacff8952013-05-02 00:27:30 +0000835 class CommandOptions : public Options
836 {
837 public:
838
839 CommandOptions (CommandInterpreter &interpreter) :
840 Options (interpreter)
841 {
842 OptionParsingStarting ();
843 }
844
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000845 ~CommandOptions () override
Jim Inghamacff8952013-05-02 00:27:30 +0000846 {
847 }
848
849 Error
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000850 SetOptionValue (uint32_t option_idx, const char *option_arg) override
Jim Inghamacff8952013-05-02 00:27:30 +0000851 {
852 Error error;
853 const int short_option = m_getopt_table[option_idx].val;
854
855 switch (short_option)
856 {
857 case 's':
858 bool tmp_result;
859 bool success;
860 tmp_result = Args::StringToBoolean(option_arg, false, &success);
861 if (!success)
862 error.SetErrorStringWithFormat("invalid boolean option: \"%s\"", option_arg);
863 else
864 {
865 if (tmp_result)
866 m_keep_stopped = eLazyBoolYes;
867 else
868 m_keep_stopped = eLazyBoolNo;
869 }
870 break;
871 default:
872 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
873 break;
874 }
875 return error;
876 }
877
878 void
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000879 OptionParsingStarting () override
Jim Inghamacff8952013-05-02 00:27:30 +0000880 {
881 m_keep_stopped = eLazyBoolCalculate;
882 }
883
884 const OptionDefinition*
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000885 GetDefinitions () override
Jim Inghamacff8952013-05-02 00:27:30 +0000886 {
887 return g_option_table;
888 }
889
890 // Options table: Required for subclasses of Options.
891
892 static OptionDefinition g_option_table[];
893
894 // Instance variables to hold the values for command options.
895 LazyBool m_keep_stopped;
896 };
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000897
Greg Claytona7015092010-09-18 01:14:36 +0000898 CommandObjectProcessDetach (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000899 CommandObjectParsed (interpreter,
900 "process detach",
901 "Detach from the current process being debugged.",
902 "process detach",
Enrico Granatae87764f2015-05-27 05:04:35 +0000903 eCommandRequiresProcess |
904 eCommandTryTargetAPILock |
905 eCommandProcessMustBeLaunched),
Jim Inghamacff8952013-05-02 00:27:30 +0000906 m_options(interpreter)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000907 {
908 }
909
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000910 ~CommandObjectProcessDetach () override
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000911 {
912 }
913
Jim Inghamacff8952013-05-02 00:27:30 +0000914 Options *
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000915 GetOptions () override
Jim Inghamacff8952013-05-02 00:27:30 +0000916 {
917 return &m_options;
918 }
919
920
Jim Ingham5a988412012-06-08 21:56:10 +0000921protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000922 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000923 DoExecute (Args& command, CommandReturnObject &result) override
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000924 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000925 Process *process = m_exe_ctx.GetProcessPtr();
Jim Inghamacff8952013-05-02 00:27:30 +0000926 // FIXME: This will be a Command Option:
927 bool keep_stopped;
928 if (m_options.m_keep_stopped == eLazyBoolCalculate)
929 {
930 // Check the process default:
931 if (process->GetDetachKeepsStopped())
932 keep_stopped = true;
933 else
934 keep_stopped = false;
935 }
936 else if (m_options.m_keep_stopped == eLazyBoolYes)
937 keep_stopped = true;
938 else
939 keep_stopped = false;
940
941 Error error (process->Detach(keep_stopped));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000942 if (error.Success())
943 {
944 result.SetStatus (eReturnStatusSuccessFinishResult);
945 }
946 else
947 {
948 result.AppendErrorWithFormat ("Detach failed: %s\n", error.AsCString());
949 result.SetStatus (eReturnStatusFailed);
950 return false;
951 }
952 return result.Succeeded();
953 }
Jim Inghamacff8952013-05-02 00:27:30 +0000954
955 CommandOptions m_options;
956};
957
958OptionDefinition
959CommandObjectProcessDetach::CommandOptions::g_option_table[] =
960{
Zachary Turnerd37221d2014-07-09 16:31:49 +0000961{ LLDB_OPT_SET_1, false, "keep-stopped", 's', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeBoolean, "Whether or not the process should be kept stopped on detach (if possible)." },
962{ 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000963};
964
965//-------------------------------------------------------------------------
Greg Claytonb766a732011-02-04 01:58:07 +0000966// CommandObjectProcessConnect
967//-------------------------------------------------------------------------
968#pragma mark CommandObjectProcessConnect
969
Jim Ingham5a988412012-06-08 21:56:10 +0000970class CommandObjectProcessConnect : public CommandObjectParsed
Greg Claytonb766a732011-02-04 01:58:07 +0000971{
972public:
973
974 class CommandOptions : public Options
975 {
976 public:
977
Greg Claytoneb0103f2011-04-07 22:46:35 +0000978 CommandOptions (CommandInterpreter &interpreter) :
979 Options(interpreter)
Greg Claytonb766a732011-02-04 01:58:07 +0000980 {
Greg Claytonf6b8b582011-04-13 00:18:08 +0000981 // Keep default values of all options in one place: OptionParsingStarting ()
982 OptionParsingStarting ();
Greg Claytonb766a732011-02-04 01:58:07 +0000983 }
984
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000985 ~CommandOptions () override
Greg Claytonb766a732011-02-04 01:58:07 +0000986 {
987 }
988
989 Error
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000990 SetOptionValue (uint32_t option_idx, const char *option_arg) override
Greg Claytonb766a732011-02-04 01:58:07 +0000991 {
992 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000993 const int short_option = m_getopt_table[option_idx].val;
Greg Claytonb766a732011-02-04 01:58:07 +0000994
995 switch (short_option)
996 {
997 case 'p':
998 plugin_name.assign (option_arg);
999 break;
1000
1001 default:
Greg Clayton86edbf42011-10-26 00:56:27 +00001002 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Greg Claytonb766a732011-02-04 01:58:07 +00001003 break;
1004 }
1005 return error;
1006 }
1007
1008 void
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001009 OptionParsingStarting () override
Greg Claytonb766a732011-02-04 01:58:07 +00001010 {
Greg Claytonb766a732011-02-04 01:58:07 +00001011 plugin_name.clear();
1012 }
1013
Greg Claytone0d378b2011-03-24 21:19:54 +00001014 const OptionDefinition*
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001015 GetDefinitions () override
Greg Claytonb766a732011-02-04 01:58:07 +00001016 {
1017 return g_option_table;
1018 }
1019
1020 // Options table: Required for subclasses of Options.
1021
Greg Claytone0d378b2011-03-24 21:19:54 +00001022 static OptionDefinition g_option_table[];
Greg Claytonb766a732011-02-04 01:58:07 +00001023
1024 // Instance variables to hold the values for command options.
1025
1026 std::string plugin_name;
1027 };
1028
1029 CommandObjectProcessConnect (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001030 CommandObjectParsed (interpreter,
1031 "process connect",
1032 "Connect to a remote debug service.",
1033 "process connect <remote-url>",
1034 0),
Greg Claytoneb0103f2011-04-07 22:46:35 +00001035 m_options (interpreter)
Greg Claytonb766a732011-02-04 01:58:07 +00001036 {
1037 }
1038
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001039 ~CommandObjectProcessConnect () override
Greg Claytonb766a732011-02-04 01:58:07 +00001040 {
1041 }
1042
1043
Jim Ingham5a988412012-06-08 21:56:10 +00001044 Options *
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001045 GetOptions () override
Jim Ingham5a988412012-06-08 21:56:10 +00001046 {
1047 return &m_options;
1048 }
1049
1050protected:
Greg Claytonb766a732011-02-04 01:58:07 +00001051 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001052 DoExecute (Args& command, CommandReturnObject &result) override
Greg Claytonb766a732011-02-04 01:58:07 +00001053 {
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001054 if (command.GetArgumentCount() != 1)
Greg Claytonb766a732011-02-04 01:58:07 +00001055 {
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001056 result.AppendErrorWithFormat ("'%s' takes exactly one argument:\nUsage: %s\n",
Greg Claytonb766a732011-02-04 01:58:07 +00001057 m_cmd_name.c_str(),
1058 m_cmd_syntax.c_str());
1059 result.SetStatus (eReturnStatusFailed);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001060 return false;
Greg Claytonb766a732011-02-04 01:58:07 +00001061 }
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001062
1063
1064 Process *process = m_exe_ctx.GetProcessPtr();
1065 if (process && process->IsAlive())
1066 {
1067 result.AppendErrorWithFormat ("Process %" PRIu64 " is currently being debugged, kill the process before connecting.\n",
1068 process->GetID());
1069 result.SetStatus (eReturnStatusFailed);
1070 return false;
1071 }
1072
1073 const char *plugin_name = nullptr;
1074 if (!m_options.plugin_name.empty())
1075 plugin_name = m_options.plugin_name.c_str();
1076
1077 Error error;
1078 Debugger& debugger = m_interpreter.GetDebugger();
1079 PlatformSP platform_sp = m_interpreter.GetPlatform(true);
1080 ProcessSP process_sp = platform_sp->ConnectProcess(command.GetArgumentAtIndex(0),
1081 plugin_name,
1082 debugger,
1083 debugger.GetSelectedTarget().get(),
1084 error);
1085 if (error.Fail() || process_sp == nullptr)
1086 {
1087 result.AppendError(error.AsCString("Error connecting to the process"));
1088 result.SetStatus (eReturnStatusFailed);
1089 return false;
1090 }
1091 return true;
Greg Claytonb766a732011-02-04 01:58:07 +00001092 }
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001093
Greg Claytonb766a732011-02-04 01:58:07 +00001094 CommandOptions m_options;
1095};
1096
Greg Claytone0d378b2011-03-24 21:19:54 +00001097OptionDefinition
Greg Claytonb766a732011-02-04 01:58:07 +00001098CommandObjectProcessConnect::CommandOptions::g_option_table[] =
1099{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001100 { LLDB_OPT_SET_ALL, false, "plugin", 'p', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
1101 { 0, false, NULL, 0 , 0, NULL, NULL, 0, eArgTypeNone, NULL }
Greg Claytonb766a732011-02-04 01:58:07 +00001102};
1103
1104//-------------------------------------------------------------------------
Greg Clayton998255b2012-10-13 02:07:45 +00001105// CommandObjectProcessPlugin
1106//-------------------------------------------------------------------------
1107#pragma mark CommandObjectProcessPlugin
1108
1109class CommandObjectProcessPlugin : public CommandObjectProxy
1110{
1111public:
1112
1113 CommandObjectProcessPlugin (CommandInterpreter &interpreter) :
1114 CommandObjectProxy (interpreter,
1115 "process plugin",
1116 "Send a custom command to the current process plug-in.",
1117 "process plugin <args>",
1118 0)
1119 {
1120 }
1121
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001122 ~CommandObjectProcessPlugin () override
Greg Clayton998255b2012-10-13 02:07:45 +00001123 {
1124 }
1125
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001126 CommandObject *
1127 GetProxyCommandObject() override
Greg Clayton998255b2012-10-13 02:07:45 +00001128 {
Greg Claytone05b2ef2013-01-09 22:58:18 +00001129 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton998255b2012-10-13 02:07:45 +00001130 if (process)
1131 return process->GetPluginCommandObject();
1132 return NULL;
1133 }
1134};
1135
1136
1137//-------------------------------------------------------------------------
Greg Clayton8f343b02010-11-04 01:54:29 +00001138// CommandObjectProcessLoad
1139//-------------------------------------------------------------------------
Jim Inghambb9caf72010-12-09 18:58:16 +00001140#pragma mark CommandObjectProcessLoad
Greg Clayton8f343b02010-11-04 01:54:29 +00001141
Jim Ingham5a988412012-06-08 21:56:10 +00001142class CommandObjectProcessLoad : public CommandObjectParsed
Greg Clayton8f343b02010-11-04 01:54:29 +00001143{
1144public:
Tamas Berghammer4fbd67a2015-12-08 13:43:59 +00001145 class CommandOptions : public Options
1146 {
1147 public:
1148 CommandOptions (CommandInterpreter &interpreter) :
1149 Options(interpreter)
1150 {
1151 // Keep default values of all options in one place: OptionParsingStarting ()
1152 OptionParsingStarting ();
1153 }
1154
1155 ~CommandOptions () override = default;
1156
1157 Error
1158 SetOptionValue (uint32_t option_idx, const char *option_arg) override
1159 {
1160 Error error;
1161 const int short_option = m_getopt_table[option_idx].val;
1162 switch (short_option)
1163 {
1164 case 'i':
1165 do_install = true;
1166 if (option_arg && option_arg[0])
1167 install_path.SetFile(option_arg, false);
1168 break;
1169 default:
1170 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
1171 break;
1172 }
1173 return error;
1174 }
1175
1176 void
1177 OptionParsingStarting () override
1178 {
1179 do_install = false;
1180 install_path.Clear();
1181 }
1182
1183 const OptionDefinition*
1184 GetDefinitions () override
1185 {
1186 return g_option_table;
1187 }
1188
1189 // Options table: Required for subclasses of Options.
1190 static OptionDefinition g_option_table[];
1191
1192 // Instance variables to hold the values for command options.
1193 bool do_install;
1194 FileSpec install_path;
1195 };
Greg Clayton8f343b02010-11-04 01:54:29 +00001196
1197 CommandObjectProcessLoad (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001198 CommandObjectParsed (interpreter,
1199 "process load",
1200 "Load a shared library into the current process.",
1201 "process load <filename> [<filename> ...]",
Enrico Granatae87764f2015-05-27 05:04:35 +00001202 eCommandRequiresProcess |
1203 eCommandTryTargetAPILock |
1204 eCommandProcessMustBeLaunched |
Tamas Berghammer4fbd67a2015-12-08 13:43:59 +00001205 eCommandProcessMustBePaused ),
1206 m_options (interpreter)
Greg Clayton8f343b02010-11-04 01:54:29 +00001207 {
1208 }
1209
Tamas Berghammer4fbd67a2015-12-08 13:43:59 +00001210 ~CommandObjectProcessLoad () override = default;
1211
1212 Options *
1213 GetOptions () override
Greg Clayton8f343b02010-11-04 01:54:29 +00001214 {
Tamas Berghammer4fbd67a2015-12-08 13:43:59 +00001215 return &m_options;
Greg Clayton8f343b02010-11-04 01:54:29 +00001216 }
1217
Jim Ingham5a988412012-06-08 21:56:10 +00001218protected:
Greg Clayton8f343b02010-11-04 01:54:29 +00001219 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001220 DoExecute (Args& command, CommandReturnObject &result) override
Greg Clayton8f343b02010-11-04 01:54:29 +00001221 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00001222 Process *process = m_exe_ctx.GetProcessPtr();
Greg Clayton8f343b02010-11-04 01:54:29 +00001223
Greg Claytonc7bece562013-01-25 18:06:21 +00001224 const size_t argc = command.GetArgumentCount();
Greg Clayton8f343b02010-11-04 01:54:29 +00001225 for (uint32_t i=0; i<argc; ++i)
1226 {
1227 Error error;
Tamas Berghammer3cb132a2015-12-02 11:58:51 +00001228 PlatformSP platform = process->GetTarget().GetPlatform();
Tamas Berghammer4fbd67a2015-12-08 13:43:59 +00001229 const char *image_path = command.GetArgumentAtIndex(i);
1230 uint32_t image_token = LLDB_INVALID_IMAGE_TOKEN;
1231
1232 if (!m_options.do_install)
1233 {
1234 FileSpec image_spec (image_path, false);
1235 platform->ResolveRemotePath(image_spec, image_spec);
1236 image_token = platform->LoadImage(process, FileSpec(), image_spec, error);
1237 }
1238 else if (m_options.install_path)
1239 {
1240 FileSpec image_spec (image_path, true);
1241 platform->ResolveRemotePath(m_options.install_path, m_options.install_path);
1242 image_token = platform->LoadImage(process, image_spec, m_options.install_path, error);
1243 }
1244 else
1245 {
1246 FileSpec image_spec (image_path, true);
1247 image_token = platform->LoadImage(process, image_spec, FileSpec(), error);
1248 }
1249
Greg Clayton8f343b02010-11-04 01:54:29 +00001250 if (image_token != LLDB_INVALID_IMAGE_TOKEN)
1251 {
Tamas Berghammer4fbd67a2015-12-08 13:43:59 +00001252 result.AppendMessageWithFormat ("Loading \"%s\"...ok\nImage %u loaded.\n", image_path, image_token);
Greg Clayton8f343b02010-11-04 01:54:29 +00001253 result.SetStatus (eReturnStatusSuccessFinishResult);
1254 }
1255 else
1256 {
1257 result.AppendErrorWithFormat ("failed to load '%s': %s", image_path, error.AsCString());
1258 result.SetStatus (eReturnStatusFailed);
1259 }
1260 }
1261 return result.Succeeded();
1262 }
Tamas Berghammer4fbd67a2015-12-08 13:43:59 +00001263
1264 CommandOptions m_options;
Greg Clayton8f343b02010-11-04 01:54:29 +00001265};
1266
Tamas Berghammer4fbd67a2015-12-08 13:43:59 +00001267OptionDefinition
1268CommandObjectProcessLoad::CommandOptions::g_option_table[] =
1269{
1270 { 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."},
1271 { 0, false, nullptr, 0 , 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }
1272};
Greg Clayton8f343b02010-11-04 01:54:29 +00001273
1274//-------------------------------------------------------------------------
1275// CommandObjectProcessUnload
1276//-------------------------------------------------------------------------
Jim Inghambb9caf72010-12-09 18:58:16 +00001277#pragma mark CommandObjectProcessUnload
Greg Clayton8f343b02010-11-04 01:54:29 +00001278
Jim Ingham5a988412012-06-08 21:56:10 +00001279class CommandObjectProcessUnload : public CommandObjectParsed
Greg Clayton8f343b02010-11-04 01:54:29 +00001280{
1281public:
1282
1283 CommandObjectProcessUnload (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001284 CommandObjectParsed (interpreter,
1285 "process unload",
1286 "Unload a shared library from the current process using the index returned by a previous call to \"process load\".",
1287 "process unload <index>",
Enrico Granatae87764f2015-05-27 05:04:35 +00001288 eCommandRequiresProcess |
1289 eCommandTryTargetAPILock |
1290 eCommandProcessMustBeLaunched |
1291 eCommandProcessMustBePaused )
Greg Clayton8f343b02010-11-04 01:54:29 +00001292 {
1293 }
1294
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001295 ~CommandObjectProcessUnload () override
Greg Clayton8f343b02010-11-04 01:54:29 +00001296 {
1297 }
1298
Jim Ingham5a988412012-06-08 21:56:10 +00001299protected:
Greg Clayton8f343b02010-11-04 01:54:29 +00001300 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001301 DoExecute (Args& command, CommandReturnObject &result) override
Greg Clayton8f343b02010-11-04 01:54:29 +00001302 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00001303 Process *process = m_exe_ctx.GetProcessPtr();
Greg Clayton8f343b02010-11-04 01:54:29 +00001304
Greg Claytonc7bece562013-01-25 18:06:21 +00001305 const size_t argc = command.GetArgumentCount();
Greg Clayton8f343b02010-11-04 01:54:29 +00001306
1307 for (uint32_t i=0; i<argc; ++i)
1308 {
1309 const char *image_token_cstr = command.GetArgumentAtIndex(i);
Vince Harron5275aaa2015-01-15 20:08:35 +00001310 uint32_t image_token = StringConvert::ToUInt32(image_token_cstr, LLDB_INVALID_IMAGE_TOKEN, 0);
Greg Clayton8f343b02010-11-04 01:54:29 +00001311 if (image_token == LLDB_INVALID_IMAGE_TOKEN)
1312 {
1313 result.AppendErrorWithFormat ("invalid image index argument '%s'", image_token_cstr);
1314 result.SetStatus (eReturnStatusFailed);
1315 break;
1316 }
1317 else
1318 {
Tamas Berghammer3cb132a2015-12-02 11:58:51 +00001319 Error error (process->GetTarget().GetPlatform()->UnloadImage(process, image_token));
Greg Clayton8f343b02010-11-04 01:54:29 +00001320 if (error.Success())
1321 {
1322 result.AppendMessageWithFormat ("Unloading shared library with index %u...ok\n", image_token);
1323 result.SetStatus (eReturnStatusSuccessFinishResult);
1324 }
1325 else
1326 {
1327 result.AppendErrorWithFormat ("failed to unload image: %s", error.AsCString());
1328 result.SetStatus (eReturnStatusFailed);
1329 break;
1330 }
1331 }
1332 }
1333 return result.Succeeded();
1334 }
1335};
1336
1337//-------------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001338// CommandObjectProcessSignal
1339//-------------------------------------------------------------------------
Jim Inghambb9caf72010-12-09 18:58:16 +00001340#pragma mark CommandObjectProcessSignal
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001341
Jim Ingham5a988412012-06-08 21:56:10 +00001342class CommandObjectProcessSignal : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001343{
1344public:
1345
Greg Claytona7015092010-09-18 01:14:36 +00001346 CommandObjectProcessSignal (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001347 CommandObjectParsed (interpreter,
1348 "process signal",
1349 "Send a UNIX signal to the current process being debugged.",
Greg Claytonf9fc6092013-01-09 19:44:40 +00001350 NULL,
Enrico Granatae87764f2015-05-27 05:04:35 +00001351 eCommandRequiresProcess | eCommandTryTargetAPILock)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001352 {
Caroline Tice405fe672010-10-04 22:28:36 +00001353 CommandArgumentEntry arg;
1354 CommandArgumentData signal_arg;
1355
1356 // Define the first (and only) variant of this arg.
Caroline Ticec0dbdfb2010-10-18 22:56:57 +00001357 signal_arg.arg_type = eArgTypeUnixSignal;
Caroline Tice405fe672010-10-04 22:28:36 +00001358 signal_arg.arg_repetition = eArgRepeatPlain;
1359
1360 // There is only one variant this argument could be; put it into the argument entry.
1361 arg.push_back (signal_arg);
1362
1363 // Push the data for the first argument into the m_arguments vector.
1364 m_arguments.push_back (arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001365 }
1366
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001367 ~CommandObjectProcessSignal () override
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001368 {
1369 }
1370
Jim Ingham5a988412012-06-08 21:56:10 +00001371protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001372 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001373 DoExecute (Args& command, CommandReturnObject &result) override
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001374 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00001375 Process *process = m_exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001376
1377 if (command.GetArgumentCount() == 1)
1378 {
Greg Clayton237cd902010-10-09 01:40:57 +00001379 int signo = LLDB_INVALID_SIGNAL_NUMBER;
1380
1381 const char *signal_name = command.GetArgumentAtIndex(0);
1382 if (::isxdigit (signal_name[0]))
Vince Harron5275aaa2015-01-15 20:08:35 +00001383 signo = StringConvert::ToSInt32(signal_name, LLDB_INVALID_SIGNAL_NUMBER, 0);
Greg Clayton237cd902010-10-09 01:40:57 +00001384 else
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001385 signo = process->GetUnixSignals()->GetSignalNumberFromName(signal_name);
Greg Clayton237cd902010-10-09 01:40:57 +00001386
1387 if (signo == LLDB_INVALID_SIGNAL_NUMBER)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001388 {
1389 result.AppendErrorWithFormat ("Invalid signal argument '%s'.\n", command.GetArgumentAtIndex(0));
1390 result.SetStatus (eReturnStatusFailed);
1391 }
1392 else
1393 {
1394 Error error (process->Signal (signo));
1395 if (error.Success())
1396 {
1397 result.SetStatus (eReturnStatusSuccessFinishResult);
1398 }
1399 else
1400 {
1401 result.AppendErrorWithFormat ("Failed to send signal %i: %s\n", signo, error.AsCString());
1402 result.SetStatus (eReturnStatusFailed);
1403 }
1404 }
1405 }
1406 else
1407 {
Jason Molendafd54b362011-09-20 21:44:10 +00001408 result.AppendErrorWithFormat("'%s' takes exactly one signal number argument:\nUsage: %s\n", m_cmd_name.c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001409 m_cmd_syntax.c_str());
1410 result.SetStatus (eReturnStatusFailed);
1411 }
1412 return result.Succeeded();
1413 }
1414};
1415
1416
1417//-------------------------------------------------------------------------
1418// CommandObjectProcessInterrupt
1419//-------------------------------------------------------------------------
Jim Inghambb9caf72010-12-09 18:58:16 +00001420#pragma mark CommandObjectProcessInterrupt
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001421
Jim Ingham5a988412012-06-08 21:56:10 +00001422class CommandObjectProcessInterrupt : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001423{
1424public:
1425
1426
Greg Claytona7015092010-09-18 01:14:36 +00001427 CommandObjectProcessInterrupt (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001428 CommandObjectParsed (interpreter,
1429 "process interrupt",
1430 "Interrupt the current process being debugged.",
1431 "process interrupt",
Enrico Granatae87764f2015-05-27 05:04:35 +00001432 eCommandRequiresProcess |
1433 eCommandTryTargetAPILock |
1434 eCommandProcessMustBeLaunched)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001435 {
1436 }
1437
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001438 ~CommandObjectProcessInterrupt () override
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001439 {
1440 }
1441
Jim Ingham5a988412012-06-08 21:56:10 +00001442protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001443 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001444 DoExecute (Args& command, CommandReturnObject &result) override
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001445 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00001446 Process *process = m_exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001447 if (process == NULL)
1448 {
1449 result.AppendError ("no process to halt");
1450 result.SetStatus (eReturnStatusFailed);
1451 return false;
1452 }
1453
1454 if (command.GetArgumentCount() == 0)
1455 {
Greg Claytonf9b57b92013-05-10 23:48:10 +00001456 bool clear_thread_plans = true;
1457 Error error(process->Halt (clear_thread_plans));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001458 if (error.Success())
1459 {
1460 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001461 }
1462 else
1463 {
1464 result.AppendErrorWithFormat ("Failed to halt process: %s\n", error.AsCString());
1465 result.SetStatus (eReturnStatusFailed);
1466 }
1467 }
1468 else
1469 {
Jason Molendafd54b362011-09-20 21:44:10 +00001470 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001471 m_cmd_name.c_str(),
1472 m_cmd_syntax.c_str());
1473 result.SetStatus (eReturnStatusFailed);
1474 }
1475 return result.Succeeded();
1476 }
1477};
1478
1479//-------------------------------------------------------------------------
1480// CommandObjectProcessKill
1481//-------------------------------------------------------------------------
Jim Inghambb9caf72010-12-09 18:58:16 +00001482#pragma mark CommandObjectProcessKill
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001483
Jim Ingham5a988412012-06-08 21:56:10 +00001484class CommandObjectProcessKill : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001485{
1486public:
1487
Greg Claytona7015092010-09-18 01:14:36 +00001488 CommandObjectProcessKill (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001489 CommandObjectParsed (interpreter,
1490 "process kill",
1491 "Terminate the current process being debugged.",
1492 "process kill",
Enrico Granatae87764f2015-05-27 05:04:35 +00001493 eCommandRequiresProcess |
1494 eCommandTryTargetAPILock |
1495 eCommandProcessMustBeLaunched)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001496 {
1497 }
1498
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001499 ~CommandObjectProcessKill () override
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001500 {
1501 }
1502
Jim Ingham5a988412012-06-08 21:56:10 +00001503protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001504 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001505 DoExecute (Args& command, CommandReturnObject &result) override
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001506 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00001507 Process *process = m_exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001508 if (process == NULL)
1509 {
1510 result.AppendError ("no process to kill");
1511 result.SetStatus (eReturnStatusFailed);
1512 return false;
1513 }
1514
1515 if (command.GetArgumentCount() == 0)
1516 {
Jason Molenda8980e6b2015-05-01 23:39:48 +00001517 Error error (process->Destroy(true));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001518 if (error.Success())
1519 {
1520 result.SetStatus (eReturnStatusSuccessFinishResult);
1521 }
1522 else
1523 {
1524 result.AppendErrorWithFormat ("Failed to kill process: %s\n", error.AsCString());
1525 result.SetStatus (eReturnStatusFailed);
1526 }
1527 }
1528 else
1529 {
Jason Molendafd54b362011-09-20 21:44:10 +00001530 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001531 m_cmd_name.c_str(),
1532 m_cmd_syntax.c_str());
1533 result.SetStatus (eReturnStatusFailed);
1534 }
1535 return result.Succeeded();
1536 }
1537};
1538
1539//-------------------------------------------------------------------------
Greg Claytona2715cf2014-06-13 00:54:12 +00001540// CommandObjectProcessSaveCore
1541//-------------------------------------------------------------------------
1542#pragma mark CommandObjectProcessSaveCore
1543
1544class CommandObjectProcessSaveCore : public CommandObjectParsed
1545{
1546public:
1547
1548 CommandObjectProcessSaveCore (CommandInterpreter &interpreter) :
1549 CommandObjectParsed (interpreter,
1550 "process save-core",
1551 "Save the current process as a core file using an appropriate file type.",
1552 "process save-core FILE",
Enrico Granatae87764f2015-05-27 05:04:35 +00001553 eCommandRequiresProcess |
1554 eCommandTryTargetAPILock |
1555 eCommandProcessMustBeLaunched)
Greg Claytona2715cf2014-06-13 00:54:12 +00001556 {
1557 }
1558
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001559 ~CommandObjectProcessSaveCore () override
Greg Claytona2715cf2014-06-13 00:54:12 +00001560 {
1561 }
1562
1563protected:
1564 bool
1565 DoExecute (Args& command,
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001566 CommandReturnObject &result) override
Greg Claytona2715cf2014-06-13 00:54:12 +00001567 {
1568 ProcessSP process_sp = m_exe_ctx.GetProcessSP();
1569 if (process_sp)
1570 {
1571 if (command.GetArgumentCount() == 1)
1572 {
1573 FileSpec output_file(command.GetArgumentAtIndex(0), false);
1574 Error error = PluginManager::SaveCore(process_sp, output_file);
1575 if (error.Success())
1576 {
1577 result.SetStatus (eReturnStatusSuccessFinishResult);
1578 }
1579 else
1580 {
1581 result.AppendErrorWithFormat ("Failed to save core file for process: %s\n", error.AsCString());
1582 result.SetStatus (eReturnStatusFailed);
1583 }
1584 }
1585 else
1586 {
1587 result.AppendErrorWithFormat ("'%s' takes one arguments:\nUsage: %s\n",
1588 m_cmd_name.c_str(),
1589 m_cmd_syntax.c_str());
1590 result.SetStatus (eReturnStatusFailed);
1591 }
1592 }
1593 else
1594 {
1595 result.AppendError ("invalid process");
1596 result.SetStatus (eReturnStatusFailed);
1597 return false;
1598 }
1599
1600 return result.Succeeded();
1601 }
1602};
1603
1604//-------------------------------------------------------------------------
Jim Ingham4b9bea82010-06-18 01:23:09 +00001605// CommandObjectProcessStatus
1606//-------------------------------------------------------------------------
Jim Inghambb9caf72010-12-09 18:58:16 +00001607#pragma mark CommandObjectProcessStatus
1608
Jim Ingham5a988412012-06-08 21:56:10 +00001609class CommandObjectProcessStatus : public CommandObjectParsed
Jim Ingham4b9bea82010-06-18 01:23:09 +00001610{
1611public:
Greg Claytona7015092010-09-18 01:14:36 +00001612 CommandObjectProcessStatus (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001613 CommandObjectParsed (interpreter,
1614 "process status",
1615 "Show the current status and location of executing process.",
1616 "process status",
Enrico Granatae87764f2015-05-27 05:04:35 +00001617 eCommandRequiresProcess | eCommandTryTargetAPILock)
Jim Ingham4b9bea82010-06-18 01:23:09 +00001618 {
1619 }
1620
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001621 ~CommandObjectProcessStatus() override
Jim Ingham4b9bea82010-06-18 01:23:09 +00001622 {
1623 }
1624
1625
1626 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001627 DoExecute (Args& command, CommandReturnObject &result) override
Jim Ingham4b9bea82010-06-18 01:23:09 +00001628 {
Greg Clayton7260f622011-04-18 08:33:37 +00001629 Stream &strm = result.GetOutputStream();
Jim Ingham4b9bea82010-06-18 01:23:09 +00001630 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Enrico Granatae87764f2015-05-27 05:04:35 +00001631 // No need to check "process" for validity as eCommandRequiresProcess ensures it is valid
Greg Claytonf9fc6092013-01-09 19:44:40 +00001632 Process *process = m_exe_ctx.GetProcessPtr();
1633 const bool only_threads_with_stop_reason = true;
1634 const uint32_t start_frame = 0;
1635 const uint32_t num_frames = 1;
1636 const uint32_t num_frames_with_source = 1;
1637 process->GetStatus(strm);
1638 process->GetThreadStatus (strm,
1639 only_threads_with_stop_reason,
1640 start_frame,
1641 num_frames,
1642 num_frames_with_source);
Jim Ingham4b9bea82010-06-18 01:23:09 +00001643 return result.Succeeded();
1644 }
1645};
1646
1647//-------------------------------------------------------------------------
Caroline Tice35731352010-10-13 20:44:39 +00001648// CommandObjectProcessHandle
1649//-------------------------------------------------------------------------
Jim Inghambb9caf72010-12-09 18:58:16 +00001650#pragma mark CommandObjectProcessHandle
Caroline Tice35731352010-10-13 20:44:39 +00001651
Jim Ingham5a988412012-06-08 21:56:10 +00001652class CommandObjectProcessHandle : public CommandObjectParsed
Caroline Tice35731352010-10-13 20:44:39 +00001653{
1654public:
1655
1656 class CommandOptions : public Options
1657 {
1658 public:
1659
Greg Claytoneb0103f2011-04-07 22:46:35 +00001660 CommandOptions (CommandInterpreter &interpreter) :
1661 Options (interpreter)
Caroline Tice35731352010-10-13 20:44:39 +00001662 {
Greg Claytonf6b8b582011-04-13 00:18:08 +00001663 OptionParsingStarting ();
Caroline Tice35731352010-10-13 20:44:39 +00001664 }
1665
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001666 ~CommandOptions () override
Caroline Tice35731352010-10-13 20:44:39 +00001667 {
1668 }
1669
1670 Error
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001671 SetOptionValue (uint32_t option_idx, const char *option_arg) override
Caroline Tice35731352010-10-13 20:44:39 +00001672 {
1673 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +00001674 const int short_option = m_getopt_table[option_idx].val;
Caroline Tice35731352010-10-13 20:44:39 +00001675
1676 switch (short_option)
1677 {
1678 case 's':
1679 stop = option_arg;
1680 break;
1681 case 'n':
1682 notify = option_arg;
1683 break;
1684 case 'p':
1685 pass = option_arg;
1686 break;
1687 default:
Greg Clayton86edbf42011-10-26 00:56:27 +00001688 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Caroline Tice35731352010-10-13 20:44:39 +00001689 break;
1690 }
1691 return error;
1692 }
1693
1694 void
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001695 OptionParsingStarting () override
Caroline Tice35731352010-10-13 20:44:39 +00001696 {
Caroline Tice35731352010-10-13 20:44:39 +00001697 stop.clear();
1698 notify.clear();
1699 pass.clear();
1700 }
1701
Greg Claytone0d378b2011-03-24 21:19:54 +00001702 const OptionDefinition*
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001703 GetDefinitions () override
Caroline Tice35731352010-10-13 20:44:39 +00001704 {
1705 return g_option_table;
1706 }
1707
1708 // Options table: Required for subclasses of Options.
1709
Greg Claytone0d378b2011-03-24 21:19:54 +00001710 static OptionDefinition g_option_table[];
Caroline Tice35731352010-10-13 20:44:39 +00001711
1712 // Instance variables to hold the values for command options.
1713
1714 std::string stop;
1715 std::string notify;
1716 std::string pass;
1717 };
1718
1719
1720 CommandObjectProcessHandle (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001721 CommandObjectParsed (interpreter,
1722 "process handle",
1723 "Show or update what the process and debugger should do with various signals received from the OS.",
1724 NULL),
Greg Claytoneb0103f2011-04-07 22:46:35 +00001725 m_options (interpreter)
Caroline Tice35731352010-10-13 20:44:39 +00001726 {
Kate Stoneea671fb2015-07-14 05:48:36 +00001727 SetHelpLong ("\nIf no signals are specified, update them all. If no update "
1728 "option is specified, list the current values.");
Caroline Tice35731352010-10-13 20:44:39 +00001729 CommandArgumentEntry arg;
Caroline Ticec0dbdfb2010-10-18 22:56:57 +00001730 CommandArgumentData signal_arg;
Caroline Tice35731352010-10-13 20:44:39 +00001731
Caroline Ticec0dbdfb2010-10-18 22:56:57 +00001732 signal_arg.arg_type = eArgTypeUnixSignal;
1733 signal_arg.arg_repetition = eArgRepeatStar;
Caroline Tice35731352010-10-13 20:44:39 +00001734
Caroline Ticec0dbdfb2010-10-18 22:56:57 +00001735 arg.push_back (signal_arg);
Caroline Tice35731352010-10-13 20:44:39 +00001736
1737 m_arguments.push_back (arg);
1738 }
1739
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001740 ~CommandObjectProcessHandle () override
Caroline Tice35731352010-10-13 20:44:39 +00001741 {
1742 }
1743
1744 Options *
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001745 GetOptions () override
Caroline Tice35731352010-10-13 20:44:39 +00001746 {
1747 return &m_options;
1748 }
1749
1750 bool
Caroline Tice10ad7992010-10-14 21:31:13 +00001751 VerifyCommandOptionValue (const std::string &option, int &real_value)
Caroline Tice35731352010-10-13 20:44:39 +00001752 {
1753 bool okay = true;
1754
Caroline Tice10ad7992010-10-14 21:31:13 +00001755 bool success = false;
1756 bool tmp_value = Args::StringToBoolean (option.c_str(), false, &success);
1757
1758 if (success && tmp_value)
1759 real_value = 1;
1760 else if (success && !tmp_value)
1761 real_value = 0;
Caroline Tice35731352010-10-13 20:44:39 +00001762 else
1763 {
1764 // If the value isn't 'true' or 'false', it had better be 0 or 1.
Vince Harron5275aaa2015-01-15 20:08:35 +00001765 real_value = StringConvert::ToUInt32 (option.c_str(), 3);
Caroline Tice10ad7992010-10-14 21:31:13 +00001766 if (real_value != 0 && real_value != 1)
Caroline Tice35731352010-10-13 20:44:39 +00001767 okay = false;
1768 }
1769
1770 return okay;
1771 }
1772
Caroline Tice10ad7992010-10-14 21:31:13 +00001773 void
1774 PrintSignalHeader (Stream &str)
1775 {
Pavel Labathb84141a2015-05-22 08:46:18 +00001776 str.Printf ("NAME PASS STOP NOTIFY\n");
1777 str.Printf ("=========== ===== ===== ======\n");
Caroline Tice10ad7992010-10-14 21:31:13 +00001778 }
1779
1780 void
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001781 PrintSignal(Stream &str, int32_t signo, const char *sig_name, const UnixSignalsSP &signals_sp)
Caroline Tice10ad7992010-10-14 21:31:13 +00001782 {
1783 bool stop;
1784 bool suppress;
1785 bool notify;
1786
Pavel Labathb84141a2015-05-22 08:46:18 +00001787 str.Printf ("%-11s ", sig_name);
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001788 if (signals_sp->GetSignalInfo(signo, suppress, stop, notify))
Caroline Tice10ad7992010-10-14 21:31:13 +00001789 {
1790 bool pass = !suppress;
1791 str.Printf ("%s %s %s",
1792 (pass ? "true " : "false"),
1793 (stop ? "true " : "false"),
1794 (notify ? "true " : "false"));
1795 }
1796 str.Printf ("\n");
1797 }
1798
1799 void
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001800 PrintSignalInformation(Stream &str, Args &signal_args, int num_valid_signals, const UnixSignalsSP &signals_sp)
Caroline Tice10ad7992010-10-14 21:31:13 +00001801 {
1802 PrintSignalHeader (str);
1803
1804 if (num_valid_signals > 0)
1805 {
1806 size_t num_args = signal_args.GetArgumentCount();
1807 for (size_t i = 0; i < num_args; ++i)
1808 {
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001809 int32_t signo = signals_sp->GetSignalNumberFromName(signal_args.GetArgumentAtIndex(i));
Caroline Tice10ad7992010-10-14 21:31:13 +00001810 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001811 PrintSignal (str, signo, signal_args.GetArgumentAtIndex (i), signals_sp);
Caroline Tice10ad7992010-10-14 21:31:13 +00001812 }
1813 }
1814 else // Print info for ALL signals
1815 {
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001816 int32_t signo = signals_sp->GetFirstSignalNumber();
Caroline Tice10ad7992010-10-14 21:31:13 +00001817 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1818 {
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001819 PrintSignal(str, signo, signals_sp->GetSignalAsCString(signo), signals_sp);
1820 signo = signals_sp->GetNextSignalNumber(signo);
Caroline Tice10ad7992010-10-14 21:31:13 +00001821 }
1822 }
1823 }
1824
Jim Ingham5a988412012-06-08 21:56:10 +00001825protected:
Caroline Tice35731352010-10-13 20:44:39 +00001826 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001827 DoExecute (Args &signal_args, CommandReturnObject &result) override
Caroline Tice35731352010-10-13 20:44:39 +00001828 {
1829 TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
1830
1831 if (!target_sp)
1832 {
1833 result.AppendError ("No current target;"
1834 " cannot handle signals until you have a valid target and process.\n");
1835 result.SetStatus (eReturnStatusFailed);
1836 return false;
1837 }
1838
1839 ProcessSP process_sp = target_sp->GetProcessSP();
1840
1841 if (!process_sp)
1842 {
1843 result.AppendError ("No current process; cannot handle signals until you have a valid process.\n");
1844 result.SetStatus (eReturnStatusFailed);
1845 return false;
1846 }
1847
Caroline Tice35731352010-10-13 20:44:39 +00001848 int stop_action = -1; // -1 means leave the current setting alone
Caroline Tice10ad7992010-10-14 21:31:13 +00001849 int pass_action = -1; // -1 means leave the current setting alone
Caroline Tice35731352010-10-13 20:44:39 +00001850 int notify_action = -1; // -1 means leave the current setting alone
1851
1852 if (! m_options.stop.empty()
Caroline Tice10ad7992010-10-14 21:31:13 +00001853 && ! VerifyCommandOptionValue (m_options.stop, stop_action))
Caroline Tice35731352010-10-13 20:44:39 +00001854 {
1855 result.AppendError ("Invalid argument for command option --stop; must be true or false.\n");
1856 result.SetStatus (eReturnStatusFailed);
1857 return false;
1858 }
1859
1860 if (! m_options.notify.empty()
Caroline Tice10ad7992010-10-14 21:31:13 +00001861 && ! VerifyCommandOptionValue (m_options.notify, notify_action))
Caroline Tice35731352010-10-13 20:44:39 +00001862 {
1863 result.AppendError ("Invalid argument for command option --notify; must be true or false.\n");
1864 result.SetStatus (eReturnStatusFailed);
1865 return false;
1866 }
1867
1868 if (! m_options.pass.empty()
Caroline Tice10ad7992010-10-14 21:31:13 +00001869 && ! VerifyCommandOptionValue (m_options.pass, pass_action))
Caroline Tice35731352010-10-13 20:44:39 +00001870 {
1871 result.AppendError ("Invalid argument for command option --pass; must be true or false.\n");
1872 result.SetStatus (eReturnStatusFailed);
1873 return false;
1874 }
1875
1876 size_t num_args = signal_args.GetArgumentCount();
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001877 UnixSignalsSP signals_sp = process_sp->GetUnixSignals();
Caroline Tice35731352010-10-13 20:44:39 +00001878 int num_signals_set = 0;
1879
Caroline Tice10ad7992010-10-14 21:31:13 +00001880 if (num_args > 0)
Caroline Tice35731352010-10-13 20:44:39 +00001881 {
Caroline Tice10ad7992010-10-14 21:31:13 +00001882 for (size_t i = 0; i < num_args; ++i)
Caroline Tice35731352010-10-13 20:44:39 +00001883 {
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001884 int32_t signo = signals_sp->GetSignalNumberFromName(signal_args.GetArgumentAtIndex(i));
Caroline Tice10ad7992010-10-14 21:31:13 +00001885 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
Caroline Tice35731352010-10-13 20:44:39 +00001886 {
Caroline Tice10ad7992010-10-14 21:31:13 +00001887 // Casting the actions as bools here should be okay, because VerifyCommandOptionValue guarantees
1888 // the value is either 0 or 1.
1889 if (stop_action != -1)
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001890 signals_sp->SetShouldStop(signo, stop_action);
Caroline Tice10ad7992010-10-14 21:31:13 +00001891 if (pass_action != -1)
1892 {
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001893 bool suppress = !pass_action;
1894 signals_sp->SetShouldSuppress(signo, suppress);
Caroline Tice10ad7992010-10-14 21:31:13 +00001895 }
1896 if (notify_action != -1)
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001897 signals_sp->SetShouldNotify(signo, notify_action);
Caroline Tice10ad7992010-10-14 21:31:13 +00001898 ++num_signals_set;
Caroline Tice35731352010-10-13 20:44:39 +00001899 }
Caroline Tice10ad7992010-10-14 21:31:13 +00001900 else
1901 {
1902 result.AppendErrorWithFormat ("Invalid signal name '%s'\n", signal_args.GetArgumentAtIndex (i));
1903 }
Caroline Tice35731352010-10-13 20:44:39 +00001904 }
1905 }
Caroline Tice10ad7992010-10-14 21:31:13 +00001906 else
1907 {
1908 // No signal specified, if any command options were specified, update ALL signals.
1909 if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1))
1910 {
1911 if (m_interpreter.Confirm ("Do you really want to update all the signals?", false))
1912 {
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001913 int32_t signo = signals_sp->GetFirstSignalNumber();
Caroline Tice10ad7992010-10-14 21:31:13 +00001914 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1915 {
1916 if (notify_action != -1)
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001917 signals_sp->SetShouldNotify(signo, notify_action);
Caroline Tice10ad7992010-10-14 21:31:13 +00001918 if (stop_action != -1)
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001919 signals_sp->SetShouldStop(signo, stop_action);
Caroline Tice10ad7992010-10-14 21:31:13 +00001920 if (pass_action != -1)
1921 {
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001922 bool suppress = !pass_action;
1923 signals_sp->SetShouldSuppress(signo, suppress);
Caroline Tice10ad7992010-10-14 21:31:13 +00001924 }
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001925 signo = signals_sp->GetNextSignalNumber(signo);
Caroline Tice10ad7992010-10-14 21:31:13 +00001926 }
1927 }
1928 }
1929 }
1930
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001931 PrintSignalInformation (result.GetOutputStream(), signal_args, num_signals_set, signals_sp);
Caroline Tice35731352010-10-13 20:44:39 +00001932
1933 if (num_signals_set > 0)
1934 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1935 else
1936 result.SetStatus (eReturnStatusFailed);
1937
1938 return result.Succeeded();
1939 }
1940
Caroline Tice35731352010-10-13 20:44:39 +00001941 CommandOptions m_options;
1942};
1943
Greg Claytone0d378b2011-03-24 21:19:54 +00001944OptionDefinition
Caroline Tice35731352010-10-13 20:44:39 +00001945CommandObjectProcessHandle::CommandOptions::g_option_table[] =
1946{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001947{ LLDB_OPT_SET_1, false, "stop", 's', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeBoolean, "Whether or not the process should be stopped if the signal is received." },
1948{ LLDB_OPT_SET_1, false, "notify", 'n', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeBoolean, "Whether or not the debugger should notify the user if the signal is received." },
1949{ LLDB_OPT_SET_1, false, "pass", 'p', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeBoolean, "Whether or not the signal should be passed to the process." },
1950{ 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Caroline Tice35731352010-10-13 20:44:39 +00001951};
1952
1953//-------------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001954// CommandObjectMultiwordProcess
1955//-------------------------------------------------------------------------
1956
Greg Clayton66111032010-06-23 01:19:29 +00001957CommandObjectMultiwordProcess::CommandObjectMultiwordProcess (CommandInterpreter &interpreter) :
Greg Claytona7015092010-09-18 01:14:36 +00001958 CommandObjectMultiword (interpreter,
1959 "process",
1960 "A set of commands for operating on a process.",
1961 "process <subcommand> [<subcommand-options>]")
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001962{
Greg Clayton197bacf2011-07-02 21:07:54 +00001963 LoadSubCommand ("attach", CommandObjectSP (new CommandObjectProcessAttach (interpreter)));
1964 LoadSubCommand ("launch", CommandObjectSP (new CommandObjectProcessLaunch (interpreter)));
1965 LoadSubCommand ("continue", CommandObjectSP (new CommandObjectProcessContinue (interpreter)));
1966 LoadSubCommand ("connect", CommandObjectSP (new CommandObjectProcessConnect (interpreter)));
1967 LoadSubCommand ("detach", CommandObjectSP (new CommandObjectProcessDetach (interpreter)));
1968 LoadSubCommand ("load", CommandObjectSP (new CommandObjectProcessLoad (interpreter)));
1969 LoadSubCommand ("unload", CommandObjectSP (new CommandObjectProcessUnload (interpreter)));
1970 LoadSubCommand ("signal", CommandObjectSP (new CommandObjectProcessSignal (interpreter)));
1971 LoadSubCommand ("handle", CommandObjectSP (new CommandObjectProcessHandle (interpreter)));
1972 LoadSubCommand ("status", CommandObjectSP (new CommandObjectProcessStatus (interpreter)));
Greg Claytona7015092010-09-18 01:14:36 +00001973 LoadSubCommand ("interrupt", CommandObjectSP (new CommandObjectProcessInterrupt (interpreter)));
Greg Clayton197bacf2011-07-02 21:07:54 +00001974 LoadSubCommand ("kill", CommandObjectSP (new CommandObjectProcessKill (interpreter)));
Greg Clayton998255b2012-10-13 02:07:45 +00001975 LoadSubCommand ("plugin", CommandObjectSP (new CommandObjectProcessPlugin (interpreter)));
Greg Claytona2715cf2014-06-13 00:54:12 +00001976 LoadSubCommand ("save-core", CommandObjectSP (new CommandObjectProcessSaveCore (interpreter)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001977}
1978
1979CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess ()
1980{
1981}
1982