blob: 4c8fe20483ed6cb9afa81bc0847dbc2ff82e517c [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 {
1054
1055 TargetSP target_sp (m_interpreter.GetDebugger().GetSelectedTarget());
1056 Error error;
Greg Claytonf9fc6092013-01-09 19:44:40 +00001057 Process *process = m_exe_ctx.GetProcessPtr();
Greg Claytonb766a732011-02-04 01:58:07 +00001058 if (process)
1059 {
1060 if (process->IsAlive())
1061 {
Daniel Malead01b2952012-11-29 21:49:15 +00001062 result.AppendErrorWithFormat ("Process %" PRIu64 " is currently being debugged, kill the process before connecting.\n",
Greg Claytonb766a732011-02-04 01:58:07 +00001063 process->GetID());
1064 result.SetStatus (eReturnStatusFailed);
1065 return false;
1066 }
1067 }
1068
1069 if (!target_sp)
1070 {
1071 // If there isn't a current target create one.
Greg Claytonb766a732011-02-04 01:58:07 +00001072
1073 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
Greg Claytona0ca6602012-10-18 16:33:33 +00001074 NULL,
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001075 NULL,
Greg Claytonb766a732011-02-04 01:58:07 +00001076 false,
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001077 NULL, // No platform options
Greg Claytonb766a732011-02-04 01:58:07 +00001078 target_sp);
1079 if (!target_sp || error.Fail())
1080 {
1081 result.AppendError(error.AsCString("Error creating target"));
1082 result.SetStatus (eReturnStatusFailed);
1083 return false;
1084 }
1085 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target_sp.get());
1086 }
1087
1088 if (command.GetArgumentCount() == 1)
1089 {
1090 const char *plugin_name = NULL;
1091 if (!m_options.plugin_name.empty())
1092 plugin_name = m_options.plugin_name.c_str();
1093
1094 const char *remote_url = command.GetArgumentAtIndex(0);
Greg Claytonc3776bf2012-02-09 06:16:32 +00001095 process = target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name, NULL).get();
Greg Claytonb766a732011-02-04 01:58:07 +00001096
1097 if (process)
1098 {
Greg Clayton44d93782014-01-27 23:43:24 +00001099 error = process->ConnectRemote (process->GetTarget().GetDebugger().GetOutputFile().get(), remote_url);
Greg Claytonb766a732011-02-04 01:58:07 +00001100
1101 if (error.Fail())
1102 {
1103 result.AppendError(error.AsCString("Remote connect failed"));
1104 result.SetStatus (eReturnStatusFailed);
Greg Clayton1517dd32012-03-31 00:10:30 +00001105 target_sp->DeleteCurrentProcess();
Greg Claytonb766a732011-02-04 01:58:07 +00001106 return false;
1107 }
1108 }
1109 else
1110 {
Jason Molendafd54b362011-09-20 21:44:10 +00001111 result.AppendErrorWithFormat ("Unable to find process plug-in for remote URL '%s'.\nPlease specify a process plug-in name with the --plugin option, or specify an object file using the \"file\" command.\n",
Daniel Maleaf00b7512012-12-18 20:00:40 +00001112 remote_url);
Greg Claytonb766a732011-02-04 01:58:07 +00001113 result.SetStatus (eReturnStatusFailed);
1114 }
1115 }
1116 else
1117 {
Jason Molendafd54b362011-09-20 21:44:10 +00001118 result.AppendErrorWithFormat ("'%s' takes exactly one argument:\nUsage: %s\n",
Greg Claytonb766a732011-02-04 01:58:07 +00001119 m_cmd_name.c_str(),
1120 m_cmd_syntax.c_str());
1121 result.SetStatus (eReturnStatusFailed);
1122 }
1123 return result.Succeeded();
1124 }
Greg Claytonb766a732011-02-04 01:58:07 +00001125
1126 CommandOptions m_options;
1127};
1128
Greg Claytone0d378b2011-03-24 21:19:54 +00001129OptionDefinition
Greg Claytonb766a732011-02-04 01:58:07 +00001130CommandObjectProcessConnect::CommandOptions::g_option_table[] =
1131{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001132 { LLDB_OPT_SET_ALL, false, "plugin", 'p', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
1133 { 0, false, NULL, 0 , 0, NULL, NULL, 0, eArgTypeNone, NULL }
Greg Claytonb766a732011-02-04 01:58:07 +00001134};
1135
1136//-------------------------------------------------------------------------
Greg Clayton998255b2012-10-13 02:07:45 +00001137// CommandObjectProcessPlugin
1138//-------------------------------------------------------------------------
1139#pragma mark CommandObjectProcessPlugin
1140
1141class CommandObjectProcessPlugin : public CommandObjectProxy
1142{
1143public:
1144
1145 CommandObjectProcessPlugin (CommandInterpreter &interpreter) :
1146 CommandObjectProxy (interpreter,
1147 "process plugin",
1148 "Send a custom command to the current process plug-in.",
1149 "process plugin <args>",
1150 0)
1151 {
1152 }
1153
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001154 ~CommandObjectProcessPlugin () override
Greg Clayton998255b2012-10-13 02:07:45 +00001155 {
1156 }
1157
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001158 CommandObject *
1159 GetProxyCommandObject() override
Greg Clayton998255b2012-10-13 02:07:45 +00001160 {
Greg Claytone05b2ef2013-01-09 22:58:18 +00001161 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton998255b2012-10-13 02:07:45 +00001162 if (process)
1163 return process->GetPluginCommandObject();
1164 return NULL;
1165 }
1166};
1167
1168
1169//-------------------------------------------------------------------------
Greg Clayton8f343b02010-11-04 01:54:29 +00001170// CommandObjectProcessLoad
1171//-------------------------------------------------------------------------
Jim Inghambb9caf72010-12-09 18:58:16 +00001172#pragma mark CommandObjectProcessLoad
Greg Clayton8f343b02010-11-04 01:54:29 +00001173
Jim Ingham5a988412012-06-08 21:56:10 +00001174class CommandObjectProcessLoad : public CommandObjectParsed
Greg Clayton8f343b02010-11-04 01:54:29 +00001175{
1176public:
1177
1178 CommandObjectProcessLoad (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001179 CommandObjectParsed (interpreter,
1180 "process load",
1181 "Load a shared library into the current process.",
1182 "process load <filename> [<filename> ...]",
Enrico Granatae87764f2015-05-27 05:04:35 +00001183 eCommandRequiresProcess |
1184 eCommandTryTargetAPILock |
1185 eCommandProcessMustBeLaunched |
1186 eCommandProcessMustBePaused )
Greg Clayton8f343b02010-11-04 01:54:29 +00001187 {
1188 }
1189
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001190 ~CommandObjectProcessLoad () override
Greg Clayton8f343b02010-11-04 01:54:29 +00001191 {
1192 }
1193
Jim Ingham5a988412012-06-08 21:56:10 +00001194protected:
Greg Clayton8f343b02010-11-04 01:54:29 +00001195 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001196 DoExecute (Args& command, CommandReturnObject &result) override
Greg Clayton8f343b02010-11-04 01:54:29 +00001197 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00001198 Process *process = m_exe_ctx.GetProcessPtr();
Greg Clayton8f343b02010-11-04 01:54:29 +00001199
Greg Claytonc7bece562013-01-25 18:06:21 +00001200 const size_t argc = command.GetArgumentCount();
Greg Clayton8f343b02010-11-04 01:54:29 +00001201
1202 for (uint32_t i=0; i<argc; ++i)
1203 {
1204 Error error;
1205 const char *image_path = command.GetArgumentAtIndex(i);
1206 FileSpec image_spec (image_path, false);
Tamas Berghammer3cb132a2015-12-02 11:58:51 +00001207 PlatformSP platform = process->GetTarget().GetPlatform();
1208 platform->ResolveRemotePath(image_spec, image_spec);
1209 uint32_t image_token = platform->LoadImage(process, image_spec, error);
Greg Clayton8f343b02010-11-04 01:54:29 +00001210 if (image_token != LLDB_INVALID_IMAGE_TOKEN)
1211 {
1212 result.AppendMessageWithFormat ("Loading \"%s\"...ok\nImage %u loaded.\n", image_path, image_token);
1213 result.SetStatus (eReturnStatusSuccessFinishResult);
1214 }
1215 else
1216 {
1217 result.AppendErrorWithFormat ("failed to load '%s': %s", image_path, error.AsCString());
1218 result.SetStatus (eReturnStatusFailed);
1219 }
1220 }
1221 return result.Succeeded();
1222 }
1223};
1224
1225
1226//-------------------------------------------------------------------------
1227// CommandObjectProcessUnload
1228//-------------------------------------------------------------------------
Jim Inghambb9caf72010-12-09 18:58:16 +00001229#pragma mark CommandObjectProcessUnload
Greg Clayton8f343b02010-11-04 01:54:29 +00001230
Jim Ingham5a988412012-06-08 21:56:10 +00001231class CommandObjectProcessUnload : public CommandObjectParsed
Greg Clayton8f343b02010-11-04 01:54:29 +00001232{
1233public:
1234
1235 CommandObjectProcessUnload (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001236 CommandObjectParsed (interpreter,
1237 "process unload",
1238 "Unload a shared library from the current process using the index returned by a previous call to \"process load\".",
1239 "process unload <index>",
Enrico Granatae87764f2015-05-27 05:04:35 +00001240 eCommandRequiresProcess |
1241 eCommandTryTargetAPILock |
1242 eCommandProcessMustBeLaunched |
1243 eCommandProcessMustBePaused )
Greg Clayton8f343b02010-11-04 01:54:29 +00001244 {
1245 }
1246
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001247 ~CommandObjectProcessUnload () override
Greg Clayton8f343b02010-11-04 01:54:29 +00001248 {
1249 }
1250
Jim Ingham5a988412012-06-08 21:56:10 +00001251protected:
Greg Clayton8f343b02010-11-04 01:54:29 +00001252 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001253 DoExecute (Args& command, CommandReturnObject &result) override
Greg Clayton8f343b02010-11-04 01:54:29 +00001254 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00001255 Process *process = m_exe_ctx.GetProcessPtr();
Greg Clayton8f343b02010-11-04 01:54:29 +00001256
Greg Claytonc7bece562013-01-25 18:06:21 +00001257 const size_t argc = command.GetArgumentCount();
Greg Clayton8f343b02010-11-04 01:54:29 +00001258
1259 for (uint32_t i=0; i<argc; ++i)
1260 {
1261 const char *image_token_cstr = command.GetArgumentAtIndex(i);
Vince Harron5275aaa2015-01-15 20:08:35 +00001262 uint32_t image_token = StringConvert::ToUInt32(image_token_cstr, LLDB_INVALID_IMAGE_TOKEN, 0);
Greg Clayton8f343b02010-11-04 01:54:29 +00001263 if (image_token == LLDB_INVALID_IMAGE_TOKEN)
1264 {
1265 result.AppendErrorWithFormat ("invalid image index argument '%s'", image_token_cstr);
1266 result.SetStatus (eReturnStatusFailed);
1267 break;
1268 }
1269 else
1270 {
Tamas Berghammer3cb132a2015-12-02 11:58:51 +00001271 Error error (process->GetTarget().GetPlatform()->UnloadImage(process, image_token));
Greg Clayton8f343b02010-11-04 01:54:29 +00001272 if (error.Success())
1273 {
1274 result.AppendMessageWithFormat ("Unloading shared library with index %u...ok\n", image_token);
1275 result.SetStatus (eReturnStatusSuccessFinishResult);
1276 }
1277 else
1278 {
1279 result.AppendErrorWithFormat ("failed to unload image: %s", error.AsCString());
1280 result.SetStatus (eReturnStatusFailed);
1281 break;
1282 }
1283 }
1284 }
1285 return result.Succeeded();
1286 }
1287};
1288
1289//-------------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001290// CommandObjectProcessSignal
1291//-------------------------------------------------------------------------
Jim Inghambb9caf72010-12-09 18:58:16 +00001292#pragma mark CommandObjectProcessSignal
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001293
Jim Ingham5a988412012-06-08 21:56:10 +00001294class CommandObjectProcessSignal : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001295{
1296public:
1297
Greg Claytona7015092010-09-18 01:14:36 +00001298 CommandObjectProcessSignal (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001299 CommandObjectParsed (interpreter,
1300 "process signal",
1301 "Send a UNIX signal to the current process being debugged.",
Greg Claytonf9fc6092013-01-09 19:44:40 +00001302 NULL,
Enrico Granatae87764f2015-05-27 05:04:35 +00001303 eCommandRequiresProcess | eCommandTryTargetAPILock)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001304 {
Caroline Tice405fe672010-10-04 22:28:36 +00001305 CommandArgumentEntry arg;
1306 CommandArgumentData signal_arg;
1307
1308 // Define the first (and only) variant of this arg.
Caroline Ticec0dbdfb2010-10-18 22:56:57 +00001309 signal_arg.arg_type = eArgTypeUnixSignal;
Caroline Tice405fe672010-10-04 22:28:36 +00001310 signal_arg.arg_repetition = eArgRepeatPlain;
1311
1312 // There is only one variant this argument could be; put it into the argument entry.
1313 arg.push_back (signal_arg);
1314
1315 // Push the data for the first argument into the m_arguments vector.
1316 m_arguments.push_back (arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001317 }
1318
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001319 ~CommandObjectProcessSignal () override
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001320 {
1321 }
1322
Jim Ingham5a988412012-06-08 21:56:10 +00001323protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001324 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001325 DoExecute (Args& command, CommandReturnObject &result) override
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001326 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00001327 Process *process = m_exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001328
1329 if (command.GetArgumentCount() == 1)
1330 {
Greg Clayton237cd902010-10-09 01:40:57 +00001331 int signo = LLDB_INVALID_SIGNAL_NUMBER;
1332
1333 const char *signal_name = command.GetArgumentAtIndex(0);
1334 if (::isxdigit (signal_name[0]))
Vince Harron5275aaa2015-01-15 20:08:35 +00001335 signo = StringConvert::ToSInt32(signal_name, LLDB_INVALID_SIGNAL_NUMBER, 0);
Greg Clayton237cd902010-10-09 01:40:57 +00001336 else
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001337 signo = process->GetUnixSignals()->GetSignalNumberFromName(signal_name);
Greg Clayton237cd902010-10-09 01:40:57 +00001338
1339 if (signo == LLDB_INVALID_SIGNAL_NUMBER)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001340 {
1341 result.AppendErrorWithFormat ("Invalid signal argument '%s'.\n", command.GetArgumentAtIndex(0));
1342 result.SetStatus (eReturnStatusFailed);
1343 }
1344 else
1345 {
1346 Error error (process->Signal (signo));
1347 if (error.Success())
1348 {
1349 result.SetStatus (eReturnStatusSuccessFinishResult);
1350 }
1351 else
1352 {
1353 result.AppendErrorWithFormat ("Failed to send signal %i: %s\n", signo, error.AsCString());
1354 result.SetStatus (eReturnStatusFailed);
1355 }
1356 }
1357 }
1358 else
1359 {
Jason Molendafd54b362011-09-20 21:44:10 +00001360 result.AppendErrorWithFormat("'%s' takes exactly one signal number argument:\nUsage: %s\n", m_cmd_name.c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001361 m_cmd_syntax.c_str());
1362 result.SetStatus (eReturnStatusFailed);
1363 }
1364 return result.Succeeded();
1365 }
1366};
1367
1368
1369//-------------------------------------------------------------------------
1370// CommandObjectProcessInterrupt
1371//-------------------------------------------------------------------------
Jim Inghambb9caf72010-12-09 18:58:16 +00001372#pragma mark CommandObjectProcessInterrupt
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001373
Jim Ingham5a988412012-06-08 21:56:10 +00001374class CommandObjectProcessInterrupt : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001375{
1376public:
1377
1378
Greg Claytona7015092010-09-18 01:14:36 +00001379 CommandObjectProcessInterrupt (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001380 CommandObjectParsed (interpreter,
1381 "process interrupt",
1382 "Interrupt the current process being debugged.",
1383 "process interrupt",
Enrico Granatae87764f2015-05-27 05:04:35 +00001384 eCommandRequiresProcess |
1385 eCommandTryTargetAPILock |
1386 eCommandProcessMustBeLaunched)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001387 {
1388 }
1389
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001390 ~CommandObjectProcessInterrupt () override
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001391 {
1392 }
1393
Jim Ingham5a988412012-06-08 21:56:10 +00001394protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001395 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001396 DoExecute (Args& command, CommandReturnObject &result) override
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001397 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00001398 Process *process = m_exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001399 if (process == NULL)
1400 {
1401 result.AppendError ("no process to halt");
1402 result.SetStatus (eReturnStatusFailed);
1403 return false;
1404 }
1405
1406 if (command.GetArgumentCount() == 0)
1407 {
Greg Claytonf9b57b92013-05-10 23:48:10 +00001408 bool clear_thread_plans = true;
1409 Error error(process->Halt (clear_thread_plans));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001410 if (error.Success())
1411 {
1412 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001413 }
1414 else
1415 {
1416 result.AppendErrorWithFormat ("Failed to halt process: %s\n", error.AsCString());
1417 result.SetStatus (eReturnStatusFailed);
1418 }
1419 }
1420 else
1421 {
Jason Molendafd54b362011-09-20 21:44:10 +00001422 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001423 m_cmd_name.c_str(),
1424 m_cmd_syntax.c_str());
1425 result.SetStatus (eReturnStatusFailed);
1426 }
1427 return result.Succeeded();
1428 }
1429};
1430
1431//-------------------------------------------------------------------------
1432// CommandObjectProcessKill
1433//-------------------------------------------------------------------------
Jim Inghambb9caf72010-12-09 18:58:16 +00001434#pragma mark CommandObjectProcessKill
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001435
Jim Ingham5a988412012-06-08 21:56:10 +00001436class CommandObjectProcessKill : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001437{
1438public:
1439
Greg Claytona7015092010-09-18 01:14:36 +00001440 CommandObjectProcessKill (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001441 CommandObjectParsed (interpreter,
1442 "process kill",
1443 "Terminate the current process being debugged.",
1444 "process kill",
Enrico Granatae87764f2015-05-27 05:04:35 +00001445 eCommandRequiresProcess |
1446 eCommandTryTargetAPILock |
1447 eCommandProcessMustBeLaunched)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001448 {
1449 }
1450
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001451 ~CommandObjectProcessKill () override
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001452 {
1453 }
1454
Jim Ingham5a988412012-06-08 21:56:10 +00001455protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001456 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001457 DoExecute (Args& command, CommandReturnObject &result) override
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001458 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00001459 Process *process = m_exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001460 if (process == NULL)
1461 {
1462 result.AppendError ("no process to kill");
1463 result.SetStatus (eReturnStatusFailed);
1464 return false;
1465 }
1466
1467 if (command.GetArgumentCount() == 0)
1468 {
Jason Molenda8980e6b2015-05-01 23:39:48 +00001469 Error error (process->Destroy(true));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001470 if (error.Success())
1471 {
1472 result.SetStatus (eReturnStatusSuccessFinishResult);
1473 }
1474 else
1475 {
1476 result.AppendErrorWithFormat ("Failed to kill process: %s\n", error.AsCString());
1477 result.SetStatus (eReturnStatusFailed);
1478 }
1479 }
1480 else
1481 {
Jason Molendafd54b362011-09-20 21:44:10 +00001482 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001483 m_cmd_name.c_str(),
1484 m_cmd_syntax.c_str());
1485 result.SetStatus (eReturnStatusFailed);
1486 }
1487 return result.Succeeded();
1488 }
1489};
1490
1491//-------------------------------------------------------------------------
Greg Claytona2715cf2014-06-13 00:54:12 +00001492// CommandObjectProcessSaveCore
1493//-------------------------------------------------------------------------
1494#pragma mark CommandObjectProcessSaveCore
1495
1496class CommandObjectProcessSaveCore : public CommandObjectParsed
1497{
1498public:
1499
1500 CommandObjectProcessSaveCore (CommandInterpreter &interpreter) :
1501 CommandObjectParsed (interpreter,
1502 "process save-core",
1503 "Save the current process as a core file using an appropriate file type.",
1504 "process save-core FILE",
Enrico Granatae87764f2015-05-27 05:04:35 +00001505 eCommandRequiresProcess |
1506 eCommandTryTargetAPILock |
1507 eCommandProcessMustBeLaunched)
Greg Claytona2715cf2014-06-13 00:54:12 +00001508 {
1509 }
1510
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001511 ~CommandObjectProcessSaveCore () override
Greg Claytona2715cf2014-06-13 00:54:12 +00001512 {
1513 }
1514
1515protected:
1516 bool
1517 DoExecute (Args& command,
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001518 CommandReturnObject &result) override
Greg Claytona2715cf2014-06-13 00:54:12 +00001519 {
1520 ProcessSP process_sp = m_exe_ctx.GetProcessSP();
1521 if (process_sp)
1522 {
1523 if (command.GetArgumentCount() == 1)
1524 {
1525 FileSpec output_file(command.GetArgumentAtIndex(0), false);
1526 Error error = PluginManager::SaveCore(process_sp, output_file);
1527 if (error.Success())
1528 {
1529 result.SetStatus (eReturnStatusSuccessFinishResult);
1530 }
1531 else
1532 {
1533 result.AppendErrorWithFormat ("Failed to save core file for process: %s\n", error.AsCString());
1534 result.SetStatus (eReturnStatusFailed);
1535 }
1536 }
1537 else
1538 {
1539 result.AppendErrorWithFormat ("'%s' takes one arguments:\nUsage: %s\n",
1540 m_cmd_name.c_str(),
1541 m_cmd_syntax.c_str());
1542 result.SetStatus (eReturnStatusFailed);
1543 }
1544 }
1545 else
1546 {
1547 result.AppendError ("invalid process");
1548 result.SetStatus (eReturnStatusFailed);
1549 return false;
1550 }
1551
1552 return result.Succeeded();
1553 }
1554};
1555
1556//-------------------------------------------------------------------------
Jim Ingham4b9bea82010-06-18 01:23:09 +00001557// CommandObjectProcessStatus
1558//-------------------------------------------------------------------------
Jim Inghambb9caf72010-12-09 18:58:16 +00001559#pragma mark CommandObjectProcessStatus
1560
Jim Ingham5a988412012-06-08 21:56:10 +00001561class CommandObjectProcessStatus : public CommandObjectParsed
Jim Ingham4b9bea82010-06-18 01:23:09 +00001562{
1563public:
Greg Claytona7015092010-09-18 01:14:36 +00001564 CommandObjectProcessStatus (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001565 CommandObjectParsed (interpreter,
1566 "process status",
1567 "Show the current status and location of executing process.",
1568 "process status",
Enrico Granatae87764f2015-05-27 05:04:35 +00001569 eCommandRequiresProcess | eCommandTryTargetAPILock)
Jim Ingham4b9bea82010-06-18 01:23:09 +00001570 {
1571 }
1572
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001573 ~CommandObjectProcessStatus() override
Jim Ingham4b9bea82010-06-18 01:23:09 +00001574 {
1575 }
1576
1577
1578 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001579 DoExecute (Args& command, CommandReturnObject &result) override
Jim Ingham4b9bea82010-06-18 01:23:09 +00001580 {
Greg Clayton7260f622011-04-18 08:33:37 +00001581 Stream &strm = result.GetOutputStream();
Jim Ingham4b9bea82010-06-18 01:23:09 +00001582 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Enrico Granatae87764f2015-05-27 05:04:35 +00001583 // No need to check "process" for validity as eCommandRequiresProcess ensures it is valid
Greg Claytonf9fc6092013-01-09 19:44:40 +00001584 Process *process = m_exe_ctx.GetProcessPtr();
1585 const bool only_threads_with_stop_reason = true;
1586 const uint32_t start_frame = 0;
1587 const uint32_t num_frames = 1;
1588 const uint32_t num_frames_with_source = 1;
1589 process->GetStatus(strm);
1590 process->GetThreadStatus (strm,
1591 only_threads_with_stop_reason,
1592 start_frame,
1593 num_frames,
1594 num_frames_with_source);
Jim Ingham4b9bea82010-06-18 01:23:09 +00001595 return result.Succeeded();
1596 }
1597};
1598
1599//-------------------------------------------------------------------------
Caroline Tice35731352010-10-13 20:44:39 +00001600// CommandObjectProcessHandle
1601//-------------------------------------------------------------------------
Jim Inghambb9caf72010-12-09 18:58:16 +00001602#pragma mark CommandObjectProcessHandle
Caroline Tice35731352010-10-13 20:44:39 +00001603
Jim Ingham5a988412012-06-08 21:56:10 +00001604class CommandObjectProcessHandle : public CommandObjectParsed
Caroline Tice35731352010-10-13 20:44:39 +00001605{
1606public:
1607
1608 class CommandOptions : public Options
1609 {
1610 public:
1611
Greg Claytoneb0103f2011-04-07 22:46:35 +00001612 CommandOptions (CommandInterpreter &interpreter) :
1613 Options (interpreter)
Caroline Tice35731352010-10-13 20:44:39 +00001614 {
Greg Claytonf6b8b582011-04-13 00:18:08 +00001615 OptionParsingStarting ();
Caroline Tice35731352010-10-13 20:44:39 +00001616 }
1617
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001618 ~CommandOptions () override
Caroline Tice35731352010-10-13 20:44:39 +00001619 {
1620 }
1621
1622 Error
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001623 SetOptionValue (uint32_t option_idx, const char *option_arg) override
Caroline Tice35731352010-10-13 20:44:39 +00001624 {
1625 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +00001626 const int short_option = m_getopt_table[option_idx].val;
Caroline Tice35731352010-10-13 20:44:39 +00001627
1628 switch (short_option)
1629 {
1630 case 's':
1631 stop = option_arg;
1632 break;
1633 case 'n':
1634 notify = option_arg;
1635 break;
1636 case 'p':
1637 pass = option_arg;
1638 break;
1639 default:
Greg Clayton86edbf42011-10-26 00:56:27 +00001640 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Caroline Tice35731352010-10-13 20:44:39 +00001641 break;
1642 }
1643 return error;
1644 }
1645
1646 void
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001647 OptionParsingStarting () override
Caroline Tice35731352010-10-13 20:44:39 +00001648 {
Caroline Tice35731352010-10-13 20:44:39 +00001649 stop.clear();
1650 notify.clear();
1651 pass.clear();
1652 }
1653
Greg Claytone0d378b2011-03-24 21:19:54 +00001654 const OptionDefinition*
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001655 GetDefinitions () override
Caroline Tice35731352010-10-13 20:44:39 +00001656 {
1657 return g_option_table;
1658 }
1659
1660 // Options table: Required for subclasses of Options.
1661
Greg Claytone0d378b2011-03-24 21:19:54 +00001662 static OptionDefinition g_option_table[];
Caroline Tice35731352010-10-13 20:44:39 +00001663
1664 // Instance variables to hold the values for command options.
1665
1666 std::string stop;
1667 std::string notify;
1668 std::string pass;
1669 };
1670
1671
1672 CommandObjectProcessHandle (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001673 CommandObjectParsed (interpreter,
1674 "process handle",
1675 "Show or update what the process and debugger should do with various signals received from the OS.",
1676 NULL),
Greg Claytoneb0103f2011-04-07 22:46:35 +00001677 m_options (interpreter)
Caroline Tice35731352010-10-13 20:44:39 +00001678 {
Kate Stoneea671fb2015-07-14 05:48:36 +00001679 SetHelpLong ("\nIf no signals are specified, update them all. If no update "
1680 "option is specified, list the current values.");
Caroline Tice35731352010-10-13 20:44:39 +00001681 CommandArgumentEntry arg;
Caroline Ticec0dbdfb2010-10-18 22:56:57 +00001682 CommandArgumentData signal_arg;
Caroline Tice35731352010-10-13 20:44:39 +00001683
Caroline Ticec0dbdfb2010-10-18 22:56:57 +00001684 signal_arg.arg_type = eArgTypeUnixSignal;
1685 signal_arg.arg_repetition = eArgRepeatStar;
Caroline Tice35731352010-10-13 20:44:39 +00001686
Caroline Ticec0dbdfb2010-10-18 22:56:57 +00001687 arg.push_back (signal_arg);
Caroline Tice35731352010-10-13 20:44:39 +00001688
1689 m_arguments.push_back (arg);
1690 }
1691
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001692 ~CommandObjectProcessHandle () override
Caroline Tice35731352010-10-13 20:44:39 +00001693 {
1694 }
1695
1696 Options *
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001697 GetOptions () override
Caroline Tice35731352010-10-13 20:44:39 +00001698 {
1699 return &m_options;
1700 }
1701
1702 bool
Caroline Tice10ad7992010-10-14 21:31:13 +00001703 VerifyCommandOptionValue (const std::string &option, int &real_value)
Caroline Tice35731352010-10-13 20:44:39 +00001704 {
1705 bool okay = true;
1706
Caroline Tice10ad7992010-10-14 21:31:13 +00001707 bool success = false;
1708 bool tmp_value = Args::StringToBoolean (option.c_str(), false, &success);
1709
1710 if (success && tmp_value)
1711 real_value = 1;
1712 else if (success && !tmp_value)
1713 real_value = 0;
Caroline Tice35731352010-10-13 20:44:39 +00001714 else
1715 {
1716 // If the value isn't 'true' or 'false', it had better be 0 or 1.
Vince Harron5275aaa2015-01-15 20:08:35 +00001717 real_value = StringConvert::ToUInt32 (option.c_str(), 3);
Caroline Tice10ad7992010-10-14 21:31:13 +00001718 if (real_value != 0 && real_value != 1)
Caroline Tice35731352010-10-13 20:44:39 +00001719 okay = false;
1720 }
1721
1722 return okay;
1723 }
1724
Caroline Tice10ad7992010-10-14 21:31:13 +00001725 void
1726 PrintSignalHeader (Stream &str)
1727 {
Pavel Labathb84141a2015-05-22 08:46:18 +00001728 str.Printf ("NAME PASS STOP NOTIFY\n");
1729 str.Printf ("=========== ===== ===== ======\n");
Caroline Tice10ad7992010-10-14 21:31:13 +00001730 }
1731
1732 void
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001733 PrintSignal(Stream &str, int32_t signo, const char *sig_name, const UnixSignalsSP &signals_sp)
Caroline Tice10ad7992010-10-14 21:31:13 +00001734 {
1735 bool stop;
1736 bool suppress;
1737 bool notify;
1738
Pavel Labathb84141a2015-05-22 08:46:18 +00001739 str.Printf ("%-11s ", sig_name);
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001740 if (signals_sp->GetSignalInfo(signo, suppress, stop, notify))
Caroline Tice10ad7992010-10-14 21:31:13 +00001741 {
1742 bool pass = !suppress;
1743 str.Printf ("%s %s %s",
1744 (pass ? "true " : "false"),
1745 (stop ? "true " : "false"),
1746 (notify ? "true " : "false"));
1747 }
1748 str.Printf ("\n");
1749 }
1750
1751 void
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001752 PrintSignalInformation(Stream &str, Args &signal_args, int num_valid_signals, const UnixSignalsSP &signals_sp)
Caroline Tice10ad7992010-10-14 21:31:13 +00001753 {
1754 PrintSignalHeader (str);
1755
1756 if (num_valid_signals > 0)
1757 {
1758 size_t num_args = signal_args.GetArgumentCount();
1759 for (size_t i = 0; i < num_args; ++i)
1760 {
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001761 int32_t signo = signals_sp->GetSignalNumberFromName(signal_args.GetArgumentAtIndex(i));
Caroline Tice10ad7992010-10-14 21:31:13 +00001762 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001763 PrintSignal (str, signo, signal_args.GetArgumentAtIndex (i), signals_sp);
Caroline Tice10ad7992010-10-14 21:31:13 +00001764 }
1765 }
1766 else // Print info for ALL signals
1767 {
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001768 int32_t signo = signals_sp->GetFirstSignalNumber();
Caroline Tice10ad7992010-10-14 21:31:13 +00001769 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1770 {
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001771 PrintSignal(str, signo, signals_sp->GetSignalAsCString(signo), signals_sp);
1772 signo = signals_sp->GetNextSignalNumber(signo);
Caroline Tice10ad7992010-10-14 21:31:13 +00001773 }
1774 }
1775 }
1776
Jim Ingham5a988412012-06-08 21:56:10 +00001777protected:
Caroline Tice35731352010-10-13 20:44:39 +00001778 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001779 DoExecute (Args &signal_args, CommandReturnObject &result) override
Caroline Tice35731352010-10-13 20:44:39 +00001780 {
1781 TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
1782
1783 if (!target_sp)
1784 {
1785 result.AppendError ("No current target;"
1786 " cannot handle signals until you have a valid target and process.\n");
1787 result.SetStatus (eReturnStatusFailed);
1788 return false;
1789 }
1790
1791 ProcessSP process_sp = target_sp->GetProcessSP();
1792
1793 if (!process_sp)
1794 {
1795 result.AppendError ("No current process; cannot handle signals until you have a valid process.\n");
1796 result.SetStatus (eReturnStatusFailed);
1797 return false;
1798 }
1799
Caroline Tice35731352010-10-13 20:44:39 +00001800 int stop_action = -1; // -1 means leave the current setting alone
Caroline Tice10ad7992010-10-14 21:31:13 +00001801 int pass_action = -1; // -1 means leave the current setting alone
Caroline Tice35731352010-10-13 20:44:39 +00001802 int notify_action = -1; // -1 means leave the current setting alone
1803
1804 if (! m_options.stop.empty()
Caroline Tice10ad7992010-10-14 21:31:13 +00001805 && ! VerifyCommandOptionValue (m_options.stop, stop_action))
Caroline Tice35731352010-10-13 20:44:39 +00001806 {
1807 result.AppendError ("Invalid argument for command option --stop; must be true or false.\n");
1808 result.SetStatus (eReturnStatusFailed);
1809 return false;
1810 }
1811
1812 if (! m_options.notify.empty()
Caroline Tice10ad7992010-10-14 21:31:13 +00001813 && ! VerifyCommandOptionValue (m_options.notify, notify_action))
Caroline Tice35731352010-10-13 20:44:39 +00001814 {
1815 result.AppendError ("Invalid argument for command option --notify; must be true or false.\n");
1816 result.SetStatus (eReturnStatusFailed);
1817 return false;
1818 }
1819
1820 if (! m_options.pass.empty()
Caroline Tice10ad7992010-10-14 21:31:13 +00001821 && ! VerifyCommandOptionValue (m_options.pass, pass_action))
Caroline Tice35731352010-10-13 20:44:39 +00001822 {
1823 result.AppendError ("Invalid argument for command option --pass; must be true or false.\n");
1824 result.SetStatus (eReturnStatusFailed);
1825 return false;
1826 }
1827
1828 size_t num_args = signal_args.GetArgumentCount();
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001829 UnixSignalsSP signals_sp = process_sp->GetUnixSignals();
Caroline Tice35731352010-10-13 20:44:39 +00001830 int num_signals_set = 0;
1831
Caroline Tice10ad7992010-10-14 21:31:13 +00001832 if (num_args > 0)
Caroline Tice35731352010-10-13 20:44:39 +00001833 {
Caroline Tice10ad7992010-10-14 21:31:13 +00001834 for (size_t i = 0; i < num_args; ++i)
Caroline Tice35731352010-10-13 20:44:39 +00001835 {
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001836 int32_t signo = signals_sp->GetSignalNumberFromName(signal_args.GetArgumentAtIndex(i));
Caroline Tice10ad7992010-10-14 21:31:13 +00001837 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
Caroline Tice35731352010-10-13 20:44:39 +00001838 {
Caroline Tice10ad7992010-10-14 21:31:13 +00001839 // Casting the actions as bools here should be okay, because VerifyCommandOptionValue guarantees
1840 // the value is either 0 or 1.
1841 if (stop_action != -1)
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001842 signals_sp->SetShouldStop(signo, stop_action);
Caroline Tice10ad7992010-10-14 21:31:13 +00001843 if (pass_action != -1)
1844 {
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001845 bool suppress = !pass_action;
1846 signals_sp->SetShouldSuppress(signo, suppress);
Caroline Tice10ad7992010-10-14 21:31:13 +00001847 }
1848 if (notify_action != -1)
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001849 signals_sp->SetShouldNotify(signo, notify_action);
Caroline Tice10ad7992010-10-14 21:31:13 +00001850 ++num_signals_set;
Caroline Tice35731352010-10-13 20:44:39 +00001851 }
Caroline Tice10ad7992010-10-14 21:31:13 +00001852 else
1853 {
1854 result.AppendErrorWithFormat ("Invalid signal name '%s'\n", signal_args.GetArgumentAtIndex (i));
1855 }
Caroline Tice35731352010-10-13 20:44:39 +00001856 }
1857 }
Caroline Tice10ad7992010-10-14 21:31:13 +00001858 else
1859 {
1860 // No signal specified, if any command options were specified, update ALL signals.
1861 if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1))
1862 {
1863 if (m_interpreter.Confirm ("Do you really want to update all the signals?", false))
1864 {
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001865 int32_t signo = signals_sp->GetFirstSignalNumber();
Caroline Tice10ad7992010-10-14 21:31:13 +00001866 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1867 {
1868 if (notify_action != -1)
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001869 signals_sp->SetShouldNotify(signo, notify_action);
Caroline Tice10ad7992010-10-14 21:31:13 +00001870 if (stop_action != -1)
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001871 signals_sp->SetShouldStop(signo, stop_action);
Caroline Tice10ad7992010-10-14 21:31:13 +00001872 if (pass_action != -1)
1873 {
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001874 bool suppress = !pass_action;
1875 signals_sp->SetShouldSuppress(signo, suppress);
Caroline Tice10ad7992010-10-14 21:31:13 +00001876 }
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001877 signo = signals_sp->GetNextSignalNumber(signo);
Caroline Tice10ad7992010-10-14 21:31:13 +00001878 }
1879 }
1880 }
1881 }
1882
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001883 PrintSignalInformation (result.GetOutputStream(), signal_args, num_signals_set, signals_sp);
Caroline Tice35731352010-10-13 20:44:39 +00001884
1885 if (num_signals_set > 0)
1886 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1887 else
1888 result.SetStatus (eReturnStatusFailed);
1889
1890 return result.Succeeded();
1891 }
1892
Caroline Tice35731352010-10-13 20:44:39 +00001893 CommandOptions m_options;
1894};
1895
Greg Claytone0d378b2011-03-24 21:19:54 +00001896OptionDefinition
Caroline Tice35731352010-10-13 20:44:39 +00001897CommandObjectProcessHandle::CommandOptions::g_option_table[] =
1898{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001899{ 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." },
1900{ 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." },
1901{ LLDB_OPT_SET_1, false, "pass", 'p', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeBoolean, "Whether or not the signal should be passed to the process." },
1902{ 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Caroline Tice35731352010-10-13 20:44:39 +00001903};
1904
1905//-------------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001906// CommandObjectMultiwordProcess
1907//-------------------------------------------------------------------------
1908
Greg Clayton66111032010-06-23 01:19:29 +00001909CommandObjectMultiwordProcess::CommandObjectMultiwordProcess (CommandInterpreter &interpreter) :
Greg Claytona7015092010-09-18 01:14:36 +00001910 CommandObjectMultiword (interpreter,
1911 "process",
1912 "A set of commands for operating on a process.",
1913 "process <subcommand> [<subcommand-options>]")
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001914{
Greg Clayton197bacf2011-07-02 21:07:54 +00001915 LoadSubCommand ("attach", CommandObjectSP (new CommandObjectProcessAttach (interpreter)));
1916 LoadSubCommand ("launch", CommandObjectSP (new CommandObjectProcessLaunch (interpreter)));
1917 LoadSubCommand ("continue", CommandObjectSP (new CommandObjectProcessContinue (interpreter)));
1918 LoadSubCommand ("connect", CommandObjectSP (new CommandObjectProcessConnect (interpreter)));
1919 LoadSubCommand ("detach", CommandObjectSP (new CommandObjectProcessDetach (interpreter)));
1920 LoadSubCommand ("load", CommandObjectSP (new CommandObjectProcessLoad (interpreter)));
1921 LoadSubCommand ("unload", CommandObjectSP (new CommandObjectProcessUnload (interpreter)));
1922 LoadSubCommand ("signal", CommandObjectSP (new CommandObjectProcessSignal (interpreter)));
1923 LoadSubCommand ("handle", CommandObjectSP (new CommandObjectProcessHandle (interpreter)));
1924 LoadSubCommand ("status", CommandObjectSP (new CommandObjectProcessStatus (interpreter)));
Greg Claytona7015092010-09-18 01:14:36 +00001925 LoadSubCommand ("interrupt", CommandObjectSP (new CommandObjectProcessInterrupt (interpreter)));
Greg Clayton197bacf2011-07-02 21:07:54 +00001926 LoadSubCommand ("kill", CommandObjectSP (new CommandObjectProcessKill (interpreter)));
Greg Clayton998255b2012-10-13 02:07:45 +00001927 LoadSubCommand ("plugin", CommandObjectSP (new CommandObjectProcessPlugin (interpreter)));
Greg Claytona2715cf2014-06-13 00:54:12 +00001928 LoadSubCommand ("save-core", CommandObjectSP (new CommandObjectProcessSaveCore (interpreter)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001929}
1930
1931CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess ()
1932{
1933}
1934