blob: f39c3227a07d865d70ae9881a0bc95f523efd540 [file] [log] [blame]
Chris Lattner24943d22010-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
Daniel Malead891f9b2012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner24943d22010-06-08 16:52:24 +000012#include "CommandObjectProcess.h"
13
14// C Includes
15// C++ Includes
16// Other libraries and framework includes
17// Project includes
Jim Ingham124e6902012-08-11 01:27:55 +000018#include "lldb/Breakpoint/Breakpoint.h"
19#include "lldb/Breakpoint/BreakpointLocation.h"
20#include "lldb/Breakpoint/BreakpointSite.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Core/State.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000022#include "lldb/Core/Module.h"
Greg Claytonabe0fed2011-04-18 08:33:37 +000023#include "lldb/Host/Host.h"
Jim Ingham124e6902012-08-11 01:27:55 +000024#include "lldb/Interpreter/Args.h"
25#include "lldb/Interpreter/Options.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026#include "lldb/Interpreter/CommandInterpreter.h"
27#include "lldb/Interpreter/CommandReturnObject.h"
Greg Claytone4b9c1f2011-03-08 22:40:15 +000028#include "lldb/Target/Platform.h"
Chris Lattner24943d22010-06-08 16:52:24 +000029#include "lldb/Target/Process.h"
Jim Ingham124e6902012-08-11 01:27:55 +000030#include "lldb/Target/StopInfo.h"
Chris Lattner24943d22010-06-08 16:52:24 +000031#include "lldb/Target/Target.h"
32#include "lldb/Target/Thread.h"
33
34using namespace lldb;
35using namespace lldb_private;
36
37//-------------------------------------------------------------------------
38// CommandObjectProcessLaunch
39//-------------------------------------------------------------------------
Jim Ingham5a15e692012-02-16 06:50:00 +000040#pragma mark CommandObjectProcessLaunch
Jim Inghamda26bd22012-06-08 21:56:10 +000041class CommandObjectProcessLaunch : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +000042{
43public:
44
Greg Clayton238c0a12010-09-18 01:14:36 +000045 CommandObjectProcessLaunch (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +000046 CommandObjectParsed (interpreter,
47 "process launch",
48 "Launch the executable in the debugger.",
Greg Claytonea0bb4d2013-01-09 19:44:40 +000049 NULL,
50 eFlagRequiresTarget),
Greg Claytonf15996e2011-04-07 22:46:35 +000051 m_options (interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +000052 {
Caroline Tice43b014a2010-10-04 22:28:36 +000053 CommandArgumentEntry arg;
54 CommandArgumentData run_args_arg;
55
56 // Define the first (and only) variant of this arg.
57 run_args_arg.arg_type = eArgTypeRunArgs;
58 run_args_arg.arg_repetition = eArgRepeatOptional;
59
60 // There is only one variant this argument could be; put it into the argument entry.
61 arg.push_back (run_args_arg);
62
63 // Push the data for the first argument into the m_arguments vector.
64 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +000065 }
66
67
68 ~CommandObjectProcessLaunch ()
69 {
70 }
71
Greg Clayton36da2aa2013-01-25 18:06:21 +000072 virtual int
Jim Ingham392d9e02012-08-10 21:48:41 +000073 HandleArgumentCompletion (Args &input,
74 int &cursor_index,
75 int &cursor_char_position,
76 OptionElementVector &opt_element_vector,
77 int match_start_point,
78 int max_return_elements,
79 bool &word_complete,
80 StringList &matches)
81 {
82 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
83 completion_str.erase (cursor_char_position);
84
85 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
86 CommandCompletions::eDiskFileCompletion,
87 completion_str.c_str(),
88 match_start_point,
89 max_return_elements,
90 NULL,
91 word_complete,
92 matches);
93 return matches.GetSize();
94 }
95
Chris Lattner24943d22010-06-08 16:52:24 +000096 Options *
97 GetOptions ()
98 {
99 return &m_options;
100 }
101
Jim Inghamda26bd22012-06-08 21:56:10 +0000102 virtual const char *GetRepeatCommand (Args &current_command_args, uint32_t index)
103 {
104 // No repeat for "process launch"...
105 return "";
106 }
107
108protected:
Chris Lattner24943d22010-06-08 16:52:24 +0000109 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000110 DoExecute (Args& launch_args, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +0000111 {
Greg Claytonabb33022011-11-08 02:43:13 +0000112 Debugger &debugger = m_interpreter.GetDebugger();
113 Target *target = debugger.GetSelectedTarget().get();
114 Error error;
Chris Lattner24943d22010-06-08 16:52:24 +0000115 // If our listener is NULL, users aren't allows to launch
Chris Lattner24943d22010-06-08 16:52:24 +0000116 char filename[PATH_MAX];
Greg Clayton5beb99d2011-08-11 02:48:45 +0000117 const Module *exe_module = target->GetExecutableModulePointer();
Greg Claytona2f74232011-02-24 22:24:29 +0000118
119 if (exe_module == NULL)
120 {
Greg Claytone1f50b92011-05-03 22:09:39 +0000121 result.AppendError ("no file in target, create a debug target using the 'target create' command");
Greg Claytona2f74232011-02-24 22:24:29 +0000122 result.SetStatus (eReturnStatusFailed);
123 return false;
124 }
125
Greg Claytona2f74232011-02-24 22:24:29 +0000126 StateType state = eStateInvalid;
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000127 Process *process = m_exe_ctx.GetProcessPtr();
Greg Claytona2f74232011-02-24 22:24:29 +0000128 if (process)
129 {
130 state = process->GetState();
131
132 if (process->IsAlive() && state != eStateConnected)
133 {
134 char message[1024];
135 if (process->GetState() == eStateAttaching)
136 ::strncpy (message, "There is a pending attach, abort it and launch a new process?", sizeof(message));
137 else
138 ::strncpy (message, "There is a running process, kill it and restart?", sizeof(message));
139
140 if (!m_interpreter.Confirm (message, true))
Jim Ingham22dc9722010-12-09 18:58:16 +0000141 {
Greg Claytona2f74232011-02-24 22:24:29 +0000142 result.SetStatus (eReturnStatusFailed);
143 return false;
Jim Ingham22dc9722010-12-09 18:58:16 +0000144 }
145 else
146 {
Greg Claytonabb33022011-11-08 02:43:13 +0000147 Error destroy_error (process->Destroy());
148 if (destroy_error.Success())
Greg Claytona2f74232011-02-24 22:24:29 +0000149 {
150 result.SetStatus (eReturnStatusSuccessFinishResult);
151 }
152 else
153 {
Greg Claytonabb33022011-11-08 02:43:13 +0000154 result.AppendErrorWithFormat ("Failed to kill process: %s\n", destroy_error.AsCString());
Greg Claytona2f74232011-02-24 22:24:29 +0000155 result.SetStatus (eReturnStatusFailed);
156 }
Jim Ingham22dc9722010-12-09 18:58:16 +0000157 }
158 }
Chris Lattner24943d22010-06-08 16:52:24 +0000159 }
Jim Ingham22dc9722010-12-09 18:58:16 +0000160
Greg Clayton0c8446c2012-10-17 22:57:12 +0000161 const char *target_settings_argv0 = target->GetArg0();
162
163 exe_module->GetFileSpec().GetPath (filename, sizeof(filename));
164
165 if (target_settings_argv0)
166 {
167 m_options.launch_info.GetArguments().AppendArgument (target_settings_argv0);
168 m_options.launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), false);
169 }
170 else
171 {
172 m_options.launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
173 }
174
Greg Clayton527154d2011-11-15 03:53:30 +0000175 if (launch_args.GetArgumentCount() == 0)
176 {
Greg Clayton73844aa2012-08-22 17:17:09 +0000177 Args target_setting_args;
Greg Clayton0c8446c2012-10-17 22:57:12 +0000178 if (target->GetRunArguments(target_setting_args))
Greg Clayton73844aa2012-08-22 17:17:09 +0000179 m_options.launch_info.GetArguments().AppendArguments (target_setting_args);
Greg Clayton527154d2011-11-15 03:53:30 +0000180 }
181 else
Greg Claytonabb33022011-11-08 02:43:13 +0000182 {
Greg Clayton0c8446c2012-10-17 22:57:12 +0000183 m_options.launch_info.GetArguments().AppendArguments (launch_args);
184
Greg Clayton3e6f2cc2011-11-21 21:51:18 +0000185 // Save the arguments for subsequent runs in the current target.
186 target->SetRunArguments (launch_args);
Greg Claytonabb33022011-11-08 02:43:13 +0000187 }
Greg Claytonabb33022011-11-08 02:43:13 +0000188
Greg Clayton527154d2011-11-15 03:53:30 +0000189 if (target->GetDisableASLR())
190 m_options.launch_info.GetFlags().Set (eLaunchFlagDisableASLR);
191
192 if (target->GetDisableSTDIO())
193 m_options.launch_info.GetFlags().Set (eLaunchFlagDisableSTDIO);
194
195 m_options.launch_info.GetFlags().Set (eLaunchFlagDebug);
196
197 Args environment;
198 target->GetEnvironmentAsArgs (environment);
199 if (environment.GetArgumentCount() > 0)
200 m_options.launch_info.GetEnvironmentEntries ().AppendArguments (environment);
201
Jim Ingham89e248f2013-02-09 01:29:05 +0000202 // Get the value of synchronous execution here. If you wait till after you have started to
203 // run, then you could have hit a breakpoint, whose command might switch the value, and
204 // then you'll pick up that incorrect value.
205 bool synchronous_execution = m_interpreter.GetSynchronous ();
206
Greg Clayton464c6162011-11-17 22:14:31 +0000207 // Finalize the file actions, and if none were given, default to opening
208 // up a pseudo terminal
209 const bool default_to_use_pty = true;
210 m_options.launch_info.FinalizeFileActions (target, default_to_use_pty);
Greg Clayton527154d2011-11-15 03:53:30 +0000211
Greg Claytonabb33022011-11-08 02:43:13 +0000212 if (state == eStateConnected)
213 {
214 if (m_options.launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
215 {
216 result.AppendWarning("can't launch in tty when launching through a remote connection");
217 m_options.launch_info.GetFlags().Clear (eLaunchFlagLaunchInTTY);
218 }
219 }
220 else
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000221 {
Greg Clayton527154d2011-11-15 03:53:30 +0000222 if (!m_options.launch_info.GetArchitecture().IsValid())
Greg Clayton2d9adb72011-11-12 02:10:56 +0000223 m_options.launch_info.GetArchitecture() = target->GetArchitecture();
224
Greg Clayton75d8c252011-11-28 01:45:00 +0000225 PlatformSP platform_sp (target->GetPlatform());
226
227 if (platform_sp && platform_sp->CanDebugProcess ())
228 {
229 process = target->GetPlatform()->DebugProcess (m_options.launch_info,
230 debugger,
231 target,
232 debugger.GetListener(),
233 error).get();
234 }
235 else
236 {
237 const char *plugin_name = m_options.launch_info.GetProcessPluginName();
Greg Clayton46c9a352012-02-09 06:16:32 +0000238 process = target->CreateProcess (debugger.GetListener(), plugin_name, NULL).get();
Greg Clayton75d8c252011-11-28 01:45:00 +0000239 if (process)
240 error = process->Launch (m_options.launch_info);
241 }
Greg Claytonabb33022011-11-08 02:43:13 +0000242
Greg Claytona2f74232011-02-24 22:24:29 +0000243 if (process == NULL)
244 {
Greg Clayton527154d2011-11-15 03:53:30 +0000245 result.SetError (error, "failed to launch or debug process");
Greg Claytona2f74232011-02-24 22:24:29 +0000246 return false;
247 }
Chris Lattner24943d22010-06-08 16:52:24 +0000248 }
Greg Claytonabb33022011-11-08 02:43:13 +0000249
Greg Clayton238c0a12010-09-18 01:14:36 +0000250 if (error.Success())
251 {
Greg Clayton940b1032011-02-23 00:35:02 +0000252 const char *archname = exe_module->GetArchitecture().GetArchitectureName();
Greg Claytonc1d37752010-10-18 01:45:30 +0000253
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000254 result.AppendMessageWithFormat ("Process %" PRIu64 " launched: '%s' (%s)\n", process->GetID(), filename, archname);
Greg Claytond8c62532010-10-07 04:19:01 +0000255 result.SetDidChangeProcessState (true);
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000256 if (m_options.launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
Greg Clayton238c0a12010-09-18 01:14:36 +0000257 {
Greg Claytond8c62532010-10-07 04:19:01 +0000258 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
Greg Clayton238c0a12010-09-18 01:14:36 +0000259 StateType state = process->WaitForProcessToStop (NULL);
260
261 if (state == eStateStopped)
262 {
Greg Claytond8c62532010-10-07 04:19:01 +0000263 error = process->Resume();
264 if (error.Success())
265 {
Greg Claytond8c62532010-10-07 04:19:01 +0000266 if (synchronous_execution)
267 {
268 state = process->WaitForProcessToStop (NULL);
Greg Clayton20206082011-11-17 01:23:07 +0000269 const bool must_be_alive = true;
270 if (!StateIsStoppedState(state, must_be_alive))
Greg Clayton395fc332011-02-15 21:59:32 +0000271 {
Greg Clayton527154d2011-11-15 03:53:30 +0000272 result.AppendErrorWithFormat ("process isn't stopped: %s", StateAsCString(state));
Greg Clayton395fc332011-02-15 21:59:32 +0000273 }
Greg Claytond8c62532010-10-07 04:19:01 +0000274 result.SetDidChangeProcessState (true);
275 result.SetStatus (eReturnStatusSuccessFinishResult);
276 }
277 else
278 {
279 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
280 }
281 }
Greg Clayton395fc332011-02-15 21:59:32 +0000282 else
283 {
Greg Clayton527154d2011-11-15 03:53:30 +0000284 result.AppendErrorWithFormat ("process resume at entry point failed: %s", error.AsCString());
Greg Clayton395fc332011-02-15 21:59:32 +0000285 result.SetStatus (eReturnStatusFailed);
286 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000287 }
Greg Clayton395fc332011-02-15 21:59:32 +0000288 else
289 {
Greg Clayton527154d2011-11-15 03:53:30 +0000290 result.AppendErrorWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
Greg Clayton395fc332011-02-15 21:59:32 +0000291 result.SetStatus (eReturnStatusFailed);
292 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000293 }
294 }
Greg Clayton395fc332011-02-15 21:59:32 +0000295 else
296 {
Greg Claytona9eb8272011-07-02 21:07:54 +0000297 result.AppendErrorWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton395fc332011-02-15 21:59:32 +0000298 result.SetStatus (eReturnStatusFailed);
299 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000300
Chris Lattner24943d22010-06-08 16:52:24 +0000301 return result.Succeeded();
302 }
303
304protected:
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000305 ProcessLaunchCommandOptions m_options;
Chris Lattner24943d22010-06-08 16:52:24 +0000306};
307
308
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000309//#define SET1 LLDB_OPT_SET_1
310//#define SET2 LLDB_OPT_SET_2
311//#define SET3 LLDB_OPT_SET_3
312//
313//OptionDefinition
314//CommandObjectProcessLaunch::CommandOptions::g_option_table[] =
315//{
316//{ SET1 | SET2 | SET3, false, "stop-at-entry", 's', no_argument, NULL, 0, eArgTypeNone, "Stop at the entry point of the program when launching a process."},
Sean Callanan9a91ef62012-10-24 01:12:14 +0000317//{ SET1 , false, "stdin", 'i', required_argument, NULL, 0, eArgTypeDirectoryName, "Redirect stdin for the process to <path>."},
318//{ SET1 , false, "stdout", 'o', required_argument, NULL, 0, eArgTypeDirectoryName, "Redirect stdout for the process to <path>."},
319//{ SET1 , false, "stderr", 'e', required_argument, NULL, 0, eArgTypeDirectoryName, "Redirect stderr for the process to <path>."},
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000320//{ SET1 | SET2 | SET3, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
Sean Callanan9a91ef62012-10-24 01:12:14 +0000321//{ SET2 , false, "tty", 't', optional_argument, 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."},
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000322//{ SET3, false, "no-stdio", 'n', no_argument, NULL, 0, eArgTypeNone, "Do not set up for terminal I/O to go to running process."},
Sean Callanan9a91ef62012-10-24 01:12:14 +0000323//{ SET1 | SET2 | SET3, false, "working-dir", 'w', required_argument, NULL, 0, eArgTypeDirectoryName, "Set the current working directory to <path> when running the inferior."},
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000324//{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
325//};
326//
327//#undef SET1
328//#undef SET2
329//#undef SET3
Chris Lattner24943d22010-06-08 16:52:24 +0000330
331//-------------------------------------------------------------------------
332// CommandObjectProcessAttach
333//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000334#pragma mark CommandObjectProcessAttach
Jim Inghamda26bd22012-06-08 21:56:10 +0000335class CommandObjectProcessAttach : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000336{
337public:
338
Chris Lattner24943d22010-06-08 16:52:24 +0000339 class CommandOptions : public Options
340 {
341 public:
342
Greg Claytonf15996e2011-04-07 22:46:35 +0000343 CommandOptions (CommandInterpreter &interpreter) :
344 Options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000345 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000346 // Keep default values of all options in one place: OptionParsingStarting ()
347 OptionParsingStarting ();
Chris Lattner24943d22010-06-08 16:52:24 +0000348 }
349
350 ~CommandOptions ()
351 {
352 }
353
354 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000355 SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +0000356 {
357 Error error;
Greg Clayton6475c422012-12-04 00:32:51 +0000358 const int short_option = m_getopt_table[option_idx].val;
Chris Lattner24943d22010-06-08 16:52:24 +0000359 bool success = false;
360 switch (short_option)
361 {
Johnny Chen7c099972012-05-24 00:43:00 +0000362 case 'c':
363 attach_info.SetContinueOnceAttached(true);
364 break;
365
Chris Lattner24943d22010-06-08 16:52:24 +0000366 case 'p':
Chris Lattner24943d22010-06-08 16:52:24 +0000367 {
Greg Clayton527154d2011-11-15 03:53:30 +0000368 lldb::pid_t pid = Args::StringToUInt32 (option_arg, LLDB_INVALID_PROCESS_ID, 0, &success);
369 if (!success || pid == LLDB_INVALID_PROCESS_ID)
370 {
371 error.SetErrorStringWithFormat("invalid process ID '%s'", option_arg);
372 }
373 else
374 {
375 attach_info.SetProcessID (pid);
376 }
Chris Lattner24943d22010-06-08 16:52:24 +0000377 }
378 break;
379
380 case 'P':
Greg Clayton527154d2011-11-15 03:53:30 +0000381 attach_info.SetProcessPluginName (option_arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000382 break;
383
384 case 'n':
Greg Clayton527154d2011-11-15 03:53:30 +0000385 attach_info.GetExecutableFile().SetFile(option_arg, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000386 break;
387
388 case 'w':
Greg Clayton527154d2011-11-15 03:53:30 +0000389 attach_info.SetWaitForLaunch(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000390 break;
Jim Ingham3a458eb2012-07-20 21:37:13 +0000391
392 case 'i':
393 attach_info.SetIgnoreExisting(false);
394 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000395
396 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000397 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner24943d22010-06-08 16:52:24 +0000398 break;
399 }
400 return error;
401 }
402
403 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000404 OptionParsingStarting ()
Chris Lattner24943d22010-06-08 16:52:24 +0000405 {
Greg Clayton527154d2011-11-15 03:53:30 +0000406 attach_info.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000407 }
408
Greg Claytonb3448432011-03-24 21:19:54 +0000409 const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +0000410 GetDefinitions ()
411 {
412 return g_option_table;
413 }
414
Jim Ingham7508e732010-08-09 23:31:02 +0000415 virtual bool
Greg Claytonf15996e2011-04-07 22:46:35 +0000416 HandleOptionArgumentCompletion (Args &input,
Jim Ingham7508e732010-08-09 23:31:02 +0000417 int cursor_index,
418 int char_pos,
419 OptionElementVector &opt_element_vector,
420 int opt_element_index,
421 int match_start_point,
422 int max_return_elements,
423 bool &word_complete,
424 StringList &matches)
425 {
426 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
427 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
428
429 // We are only completing the name option for now...
430
Greg Claytonb3448432011-03-24 21:19:54 +0000431 const OptionDefinition *opt_defs = GetDefinitions();
Jim Ingham7508e732010-08-09 23:31:02 +0000432 if (opt_defs[opt_defs_index].short_option == 'n')
433 {
434 // Are we in the name?
435
436 // Look to see if there is a -P argument provided, and if so use that plugin, otherwise
437 // use the default plugin.
Jim Ingham7508e732010-08-09 23:31:02 +0000438
439 const char *partial_name = NULL;
440 partial_name = input.GetArgumentAtIndex(opt_arg_pos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000441
Greg Claytonb72d0f02011-04-12 05:54:46 +0000442 PlatformSP platform_sp (m_interpreter.GetPlatform (true));
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000443 if (platform_sp)
Jim Ingham7508e732010-08-09 23:31:02 +0000444 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000445 ProcessInstanceInfoList process_infos;
446 ProcessInstanceInfoMatch match_info;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000447 if (partial_name)
448 {
Greg Clayton527154d2011-11-15 03:53:30 +0000449 match_info.GetProcessInfo().GetExecutableFile().SetFile(partial_name, false);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000450 match_info.SetNameMatchType(eNameMatchStartsWith);
451 }
452 platform_sp->FindProcesses (match_info, process_infos);
Greg Clayton36da2aa2013-01-25 18:06:21 +0000453 const size_t num_matches = process_infos.GetSize();
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000454 if (num_matches > 0)
455 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000456 for (size_t i=0; i<num_matches; ++i)
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000457 {
458 matches.AppendString (process_infos.GetProcessNameAtIndex(i),
459 process_infos.GetProcessNameLengthAtIndex(i));
460 }
461 }
Jim Ingham7508e732010-08-09 23:31:02 +0000462 }
463 }
464
465 return false;
466 }
467
Chris Lattner24943d22010-06-08 16:52:24 +0000468 // Options table: Required for subclasses of Options.
469
Greg Claytonb3448432011-03-24 21:19:54 +0000470 static OptionDefinition g_option_table[];
Chris Lattner24943d22010-06-08 16:52:24 +0000471
472 // Instance variables to hold the values for command options.
473
Greg Clayton527154d2011-11-15 03:53:30 +0000474 ProcessAttachInfo attach_info;
Chris Lattner24943d22010-06-08 16:52:24 +0000475 };
476
Greg Clayton238c0a12010-09-18 01:14:36 +0000477 CommandObjectProcessAttach (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000478 CommandObjectParsed (interpreter,
479 "process attach",
480 "Attach to a process.",
481 "process attach <cmd-options>"),
Greg Claytonf15996e2011-04-07 22:46:35 +0000482 m_options (interpreter)
Jim Ingham7508e732010-08-09 23:31:02 +0000483 {
Jim Ingham7508e732010-08-09 23:31:02 +0000484 }
485
486 ~CommandObjectProcessAttach ()
487 {
488 }
489
Jim Inghamda26bd22012-06-08 21:56:10 +0000490 Options *
491 GetOptions ()
492 {
493 return &m_options;
494 }
495
496protected:
Jim Ingham7508e732010-08-09 23:31:02 +0000497 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000498 DoExecute (Args& command,
Jim Ingham7508e732010-08-09 23:31:02 +0000499 CommandReturnObject &result)
500 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000501 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Jim Inghamee940e22011-09-15 01:08:57 +0000502 // N.B. The attach should be synchronous. It doesn't help much to get the prompt back between initiating the attach
503 // and the target actually stopping. So even if the interpreter is set to be asynchronous, we wait for the stop
504 // ourselves here.
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000505
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000506 Process *process = m_exe_ctx.GetProcessPtr();
Greg Claytona2f74232011-02-24 22:24:29 +0000507 StateType state = eStateInvalid;
Jim Ingham7508e732010-08-09 23:31:02 +0000508 if (process)
509 {
Greg Claytona2f74232011-02-24 22:24:29 +0000510 state = process->GetState();
511 if (process->IsAlive() && state != eStateConnected)
Jim Ingham7508e732010-08-09 23:31:02 +0000512 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000513 result.AppendErrorWithFormat ("Process %" PRIu64 " is currently being debugged, kill the process before attaching.\n",
Jim Ingham7508e732010-08-09 23:31:02 +0000514 process->GetID());
515 result.SetStatus (eReturnStatusFailed);
516 return false;
517 }
518 }
519
520 if (target == NULL)
521 {
522 // If there isn't a current target create one.
523 TargetSP new_target_sp;
Jim Ingham7508e732010-08-09 23:31:02 +0000524 Error error;
525
Greg Clayton238c0a12010-09-18 01:14:36 +0000526 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
Greg Claytoned0a0fb2012-10-18 16:33:33 +0000527 NULL,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000528 NULL,
Greg Clayton238c0a12010-09-18 01:14:36 +0000529 false,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000530 NULL, // No platform options
Greg Clayton238c0a12010-09-18 01:14:36 +0000531 new_target_sp);
Jim Ingham7508e732010-08-09 23:31:02 +0000532 target = new_target_sp.get();
533 if (target == NULL || error.Fail())
534 {
Greg Claytone71e2582011-02-04 01:58:07 +0000535 result.AppendError(error.AsCString("Error creating target"));
Jim Ingham7508e732010-08-09 23:31:02 +0000536 return false;
537 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000538 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target);
Jim Ingham7508e732010-08-09 23:31:02 +0000539 }
540
541 // Record the old executable module, we want to issue a warning if the process of attaching changed the
542 // current executable (like somebody said "file foo" then attached to a PID whose executable was bar.)
543
544 ModuleSP old_exec_module_sp = target->GetExecutableModule();
545 ArchSpec old_arch_spec = target->GetArchitecture();
546
547 if (command.GetArgumentCount())
548 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000549 result.AppendErrorWithFormat("Invalid arguments for '%s'.\nUsage: %s\n", m_cmd_name.c_str(), m_cmd_syntax.c_str());
Jim Ingham7508e732010-08-09 23:31:02 +0000550 result.SetStatus (eReturnStatusFailed);
551 }
552 else
553 {
Greg Claytona2f74232011-02-24 22:24:29 +0000554 if (state != eStateConnected)
555 {
Greg Clayton527154d2011-11-15 03:53:30 +0000556 const char *plugin_name = m_options.attach_info.GetProcessPluginName();
Greg Clayton46c9a352012-02-09 06:16:32 +0000557 process = target->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name, NULL).get();
Greg Claytona2f74232011-02-24 22:24:29 +0000558 }
Jim Ingham7508e732010-08-09 23:31:02 +0000559
560 if (process)
561 {
562 Error error;
Greg Clayton527154d2011-11-15 03:53:30 +0000563 // If no process info was specified, then use the target executable
564 // name as the process to attach to by default
565 if (!m_options.attach_info.ProcessInfoSpecified ())
Jim Ingham4805a1c2010-09-15 01:34:14 +0000566 {
567 if (old_exec_module_sp)
Greg Clayton1d1f39e2011-11-29 04:03:30 +0000568 m_options.attach_info.GetExecutableFile().GetFilename() = old_exec_module_sp->GetPlatformFileSpec().GetFilename();
Jim Ingham4805a1c2010-09-15 01:34:14 +0000569
Greg Clayton527154d2011-11-15 03:53:30 +0000570 if (!m_options.attach_info.ProcessInfoSpecified ())
571 {
572 error.SetErrorString ("no process specified, create a target with a file, or specify the --pid or --name command option");
573 }
574 }
575
576 if (error.Success())
577 {
578 error = process->Attach (m_options.attach_info);
579
Jim Ingham4805a1c2010-09-15 01:34:14 +0000580 if (error.Success())
581 {
582 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
583 }
Jim Ingham7508e732010-08-09 23:31:02 +0000584 else
585 {
Greg Clayton527154d2011-11-15 03:53:30 +0000586 result.AppendErrorWithFormat ("attach failed: %s\n", error.AsCString());
Jim Ingham4805a1c2010-09-15 01:34:14 +0000587 result.SetStatus (eReturnStatusFailed);
588 return false;
Jim Ingham7508e732010-08-09 23:31:02 +0000589 }
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000590 // If we're synchronous, wait for the stopped event and report that.
591 // Otherwise just return.
592 // FIXME: in the async case it will now be possible to get to the command
593 // interpreter with a state eStateAttaching. Make sure we handle that correctly.
Jim Inghamee940e22011-09-15 01:08:57 +0000594 StateType state = process->WaitForProcessToStop (NULL);
Greg Clayton527154d2011-11-15 03:53:30 +0000595
Jim Inghamee940e22011-09-15 01:08:57 +0000596 result.SetDidChangeProcessState (true);
Johnny Chen9986a3b2012-05-18 00:51:36 +0000597
598 if (state == eStateStopped)
599 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000600 result.AppendMessageWithFormat ("Process %" PRIu64 " %s\n", process->GetID(), StateAsCString (state));
Johnny Chen9986a3b2012-05-18 00:51:36 +0000601 result.SetStatus (eReturnStatusSuccessFinishNoResult);
602 }
603 else
604 {
605 result.AppendError ("attach failed: process did not stop (no such process or permission problem?)");
Jim Ingham5d90ade2012-07-27 23:57:19 +0000606 process->Destroy();
Johnny Chen9986a3b2012-05-18 00:51:36 +0000607 result.SetStatus (eReturnStatusFailed);
608 return false;
609 }
Jim Ingham7508e732010-08-09 23:31:02 +0000610 }
Jim Ingham7508e732010-08-09 23:31:02 +0000611 }
612 }
613
614 if (result.Succeeded())
615 {
616 // Okay, we're done. Last step is to warn if the executable module has changed:
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000617 char new_path[PATH_MAX];
Greg Clayton5beb99d2011-08-11 02:48:45 +0000618 ModuleSP new_exec_module_sp (target->GetExecutableModule());
Jim Ingham7508e732010-08-09 23:31:02 +0000619 if (!old_exec_module_sp)
620 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000621 // We might not have a module if we attached to a raw pid...
Greg Clayton5beb99d2011-08-11 02:48:45 +0000622 if (new_exec_module_sp)
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000623 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000624 new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000625 result.AppendMessageWithFormat("Executable module set to \"%s\".\n", new_path);
626 }
Jim Ingham7508e732010-08-09 23:31:02 +0000627 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000628 else if (old_exec_module_sp->GetFileSpec() != new_exec_module_sp->GetFileSpec())
Jim Ingham7508e732010-08-09 23:31:02 +0000629 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000630 char old_path[PATH_MAX];
Jim Ingham7508e732010-08-09 23:31:02 +0000631
Greg Clayton5beb99d2011-08-11 02:48:45 +0000632 old_exec_module_sp->GetFileSpec().GetPath (old_path, PATH_MAX);
633 new_exec_module_sp->GetFileSpec().GetPath (new_path, PATH_MAX);
Jim Ingham7508e732010-08-09 23:31:02 +0000634
635 result.AppendWarningWithFormat("Executable module changed from \"%s\" to \"%s\".\n",
636 old_path, new_path);
637 }
638
639 if (!old_arch_spec.IsValid())
640 {
Greg Clayton92cb9a92012-09-14 02:41:36 +0000641 result.AppendMessageWithFormat ("Architecture set to: %s.\n", target->GetArchitecture().GetTriple().getTriple().c_str());
Jim Ingham7508e732010-08-09 23:31:02 +0000642 }
Sean Callanan40e278c2012-12-13 22:07:14 +0000643 else if (!old_arch_spec.IsExactMatch(target->GetArchitecture()))
Jim Ingham7508e732010-08-09 23:31:02 +0000644 {
645 result.AppendWarningWithFormat("Architecture changed from %s to %s.\n",
Greg Clayton92cb9a92012-09-14 02:41:36 +0000646 old_arch_spec.GetTriple().getTriple().c_str(),
647 target->GetArchitecture().GetTriple().getTriple().c_str());
Jim Ingham7508e732010-08-09 23:31:02 +0000648 }
Johnny Chen7c099972012-05-24 00:43:00 +0000649
650 // This supports the use-case scenario of immediately continuing the process once attached.
651 if (m_options.attach_info.GetContinueOnceAttached())
Sean Callanan4336d932012-05-31 01:30:08 +0000652 m_interpreter.HandleCommand("process continue", eLazyBoolNo, result);
Jim Ingham7508e732010-08-09 23:31:02 +0000653 }
654 return result.Succeeded();
655 }
656
Chris Lattner24943d22010-06-08 16:52:24 +0000657 CommandOptions m_options;
658};
659
660
Greg Claytonb3448432011-03-24 21:19:54 +0000661OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000662CommandObjectProcessAttach::CommandOptions::g_option_table[] =
663{
Jim Ingham3a458eb2012-07-20 21:37:13 +0000664{ LLDB_OPT_SET_ALL, false, "continue",'c', no_argument, NULL, 0, eArgTypeNone, "Immediately continue the process once attached."},
665{ LLDB_OPT_SET_ALL, false, "plugin", 'P', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
666{ LLDB_OPT_SET_1, false, "pid", 'p', required_argument, NULL, 0, eArgTypePid, "The process ID of an existing process to attach to."},
667{ LLDB_OPT_SET_2, false, "name", 'n', required_argument, NULL, 0, eArgTypeProcessName, "The name of the process to attach to."},
668{ LLDB_OPT_SET_2, false, "include-existing", 'i', no_argument, NULL, 0, eArgTypeNone, "Include existing processes when doing attach -w."},
669{ LLDB_OPT_SET_2, false, "waitfor", 'w', no_argument, NULL, 0, eArgTypeNone, "Wait for the process with <process-name> to launch."},
Caroline Tice4d6675c2010-10-01 19:59:14 +0000670{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000671};
672
673//-------------------------------------------------------------------------
674// CommandObjectProcessContinue
675//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000676#pragma mark CommandObjectProcessContinue
Chris Lattner24943d22010-06-08 16:52:24 +0000677
Jim Inghamda26bd22012-06-08 21:56:10 +0000678class CommandObjectProcessContinue : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000679{
680public:
681
Greg Clayton238c0a12010-09-18 01:14:36 +0000682 CommandObjectProcessContinue (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000683 CommandObjectParsed (interpreter,
684 "process continue",
685 "Continue execution of all threads in the current process.",
686 "process continue",
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000687 eFlagRequiresProcess |
688 eFlagTryTargetAPILock |
689 eFlagProcessMustBeLaunched |
690 eFlagProcessMustBePaused ),
Jim Ingham124e6902012-08-11 01:27:55 +0000691 m_options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000692 {
693 }
694
695
696 ~CommandObjectProcessContinue ()
697 {
698 }
699
Jim Inghamda26bd22012-06-08 21:56:10 +0000700protected:
Jim Ingham124e6902012-08-11 01:27:55 +0000701
702 class CommandOptions : public Options
703 {
704 public:
705
706 CommandOptions (CommandInterpreter &interpreter) :
707 Options(interpreter)
708 {
709 // Keep default values of all options in one place: OptionParsingStarting ()
710 OptionParsingStarting ();
711 }
712
713 ~CommandOptions ()
714 {
715 }
716
717 Error
718 SetOptionValue (uint32_t option_idx, const char *option_arg)
719 {
720 Error error;
Greg Clayton6475c422012-12-04 00:32:51 +0000721 const int short_option = m_getopt_table[option_idx].val;
Jim Ingham124e6902012-08-11 01:27:55 +0000722 bool success = false;
723 switch (short_option)
724 {
725 case 'i':
726 m_ignore = Args::StringToUInt32 (option_arg, 0, 0, &success);
727 if (!success)
728 error.SetErrorStringWithFormat ("invalid value for ignore option: \"%s\", should be a number.", option_arg);
729 break;
730
731 default:
732 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
733 break;
734 }
735 return error;
736 }
737
738 void
739 OptionParsingStarting ()
740 {
741 m_ignore = 0;
742 }
743
744 const OptionDefinition*
745 GetDefinitions ()
746 {
747 return g_option_table;
748 }
749
750 // Options table: Required for subclasses of Options.
751
752 static OptionDefinition g_option_table[];
753
754 uint32_t m_ignore;
755 };
756
Chris Lattner24943d22010-06-08 16:52:24 +0000757 bool
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000758 DoExecute (Args& command, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +0000759 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000760 Process *process = m_exe_ctx.GetProcessPtr();
Greg Clayton238c0a12010-09-18 01:14:36 +0000761 bool synchronous_execution = m_interpreter.GetSynchronous ();
Chris Lattner24943d22010-06-08 16:52:24 +0000762 StateType state = process->GetState();
763 if (state == eStateStopped)
764 {
765 if (command.GetArgumentCount() != 0)
766 {
767 result.AppendErrorWithFormat ("The '%s' command does not take any arguments.\n", m_cmd_name.c_str());
768 result.SetStatus (eReturnStatusFailed);
769 return false;
770 }
771
Jim Ingham124e6902012-08-11 01:27:55 +0000772 if (m_options.m_ignore > 0)
773 {
774 ThreadSP sel_thread_sp(process->GetThreadList().GetSelectedThread());
775 if (sel_thread_sp)
776 {
777 StopInfoSP stop_info_sp = sel_thread_sp->GetStopInfo();
778 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint)
779 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000780 lldb::break_id_t bp_site_id = (lldb::break_id_t)stop_info_sp->GetValue();
Jim Ingham124e6902012-08-11 01:27:55 +0000781 BreakpointSiteSP bp_site_sp(process->GetBreakpointSiteList().FindByID(bp_site_id));
782 if (bp_site_sp)
783 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000784 const size_t num_owners = bp_site_sp->GetNumberOfOwners();
785 for (size_t i = 0; i < num_owners; i++)
Jim Ingham124e6902012-08-11 01:27:55 +0000786 {
787 Breakpoint &bp_ref = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
788 if (!bp_ref.IsInternal())
789 {
790 bp_ref.SetIgnoreCount(m_options.m_ignore);
791 }
792 }
793 }
794 }
795 }
796 }
797
Jim Inghamb9950592012-09-10 20:50:15 +0000798 { // Scope for thread list mutex:
799 Mutex::Locker locker (process->GetThreadList().GetMutex());
800 const uint32_t num_threads = process->GetThreadList().GetSize();
Chris Lattner24943d22010-06-08 16:52:24 +0000801
Jim Inghamb9950592012-09-10 20:50:15 +0000802 // Set the actions that the threads should each take when resuming
803 for (uint32_t idx=0; idx<num_threads; ++idx)
804 {
805 process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState (eStateRunning);
806 }
Chris Lattner24943d22010-06-08 16:52:24 +0000807 }
Jim Inghamb9950592012-09-10 20:50:15 +0000808
Chris Lattner24943d22010-06-08 16:52:24 +0000809 Error error(process->Resume());
810 if (error.Success())
811 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000812 result.AppendMessageWithFormat ("Process %" PRIu64 " resuming\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000813 if (synchronous_execution)
814 {
Greg Claytonbef15832010-07-14 00:18:15 +0000815 state = process->WaitForProcessToStop (NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000816
817 result.SetDidChangeProcessState (true);
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000818 result.AppendMessageWithFormat ("Process %" PRIu64 " %s\n", process->GetID(), StateAsCString (state));
Chris Lattner24943d22010-06-08 16:52:24 +0000819 result.SetStatus (eReturnStatusSuccessFinishNoResult);
820 }
821 else
822 {
823 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
824 }
825 }
826 else
827 {
828 result.AppendErrorWithFormat("Failed to resume process: %s.\n", error.AsCString());
829 result.SetStatus (eReturnStatusFailed);
830 }
831 }
832 else
833 {
834 result.AppendErrorWithFormat ("Process cannot be continued from its current state (%s).\n",
835 StateAsCString(state));
836 result.SetStatus (eReturnStatusFailed);
837 }
838 return result.Succeeded();
839 }
Jim Ingham124e6902012-08-11 01:27:55 +0000840
841 Options *
842 GetOptions ()
843 {
844 return &m_options;
845 }
846
847 CommandOptions m_options;
848
849};
850
851OptionDefinition
852CommandObjectProcessContinue::CommandOptions::g_option_table[] =
853{
854{ LLDB_OPT_SET_ALL, false, "ignore-count",'i', required_argument, NULL, 0, eArgTypeUnsignedInteger,
855 "Ignore <N> crossings of the breakpoint (if it exists) for the currently selected thread."},
856{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000857};
858
859//-------------------------------------------------------------------------
860// CommandObjectProcessDetach
861//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000862#pragma mark CommandObjectProcessDetach
Chris Lattner24943d22010-06-08 16:52:24 +0000863
Jim Inghamda26bd22012-06-08 21:56:10 +0000864class CommandObjectProcessDetach : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000865{
866public:
867
Greg Clayton238c0a12010-09-18 01:14:36 +0000868 CommandObjectProcessDetach (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000869 CommandObjectParsed (interpreter,
870 "process detach",
871 "Detach from the current process being debugged.",
872 "process detach",
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000873 eFlagRequiresProcess |
874 eFlagTryTargetAPILock |
Jim Inghamda26bd22012-06-08 21:56:10 +0000875 eFlagProcessMustBeLaunched)
Chris Lattner24943d22010-06-08 16:52:24 +0000876 {
877 }
878
879 ~CommandObjectProcessDetach ()
880 {
881 }
882
Jim Inghamda26bd22012-06-08 21:56:10 +0000883protected:
Chris Lattner24943d22010-06-08 16:52:24 +0000884 bool
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000885 DoExecute (Args& command, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +0000886 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000887 Process *process = m_exe_ctx.GetProcessPtr();
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000888 result.AppendMessageWithFormat ("Detaching from process %" PRIu64 "\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000889 Error error (process->Detach());
890 if (error.Success())
891 {
892 result.SetStatus (eReturnStatusSuccessFinishResult);
893 }
894 else
895 {
896 result.AppendErrorWithFormat ("Detach failed: %s\n", error.AsCString());
897 result.SetStatus (eReturnStatusFailed);
898 return false;
899 }
900 return result.Succeeded();
901 }
902};
903
904//-------------------------------------------------------------------------
Greg Claytone71e2582011-02-04 01:58:07 +0000905// CommandObjectProcessConnect
906//-------------------------------------------------------------------------
907#pragma mark CommandObjectProcessConnect
908
Jim Inghamda26bd22012-06-08 21:56:10 +0000909class CommandObjectProcessConnect : public CommandObjectParsed
Greg Claytone71e2582011-02-04 01:58:07 +0000910{
911public:
912
913 class CommandOptions : public Options
914 {
915 public:
916
Greg Claytonf15996e2011-04-07 22:46:35 +0000917 CommandOptions (CommandInterpreter &interpreter) :
918 Options(interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000919 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000920 // Keep default values of all options in one place: OptionParsingStarting ()
921 OptionParsingStarting ();
Greg Claytone71e2582011-02-04 01:58:07 +0000922 }
923
924 ~CommandOptions ()
925 {
926 }
927
928 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000929 SetOptionValue (uint32_t option_idx, const char *option_arg)
Greg Claytone71e2582011-02-04 01:58:07 +0000930 {
931 Error error;
Greg Clayton6475c422012-12-04 00:32:51 +0000932 const int short_option = m_getopt_table[option_idx].val;
Greg Claytone71e2582011-02-04 01:58:07 +0000933
934 switch (short_option)
935 {
936 case 'p':
937 plugin_name.assign (option_arg);
938 break;
939
940 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000941 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Greg Claytone71e2582011-02-04 01:58:07 +0000942 break;
943 }
944 return error;
945 }
946
947 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000948 OptionParsingStarting ()
Greg Claytone71e2582011-02-04 01:58:07 +0000949 {
Greg Claytone71e2582011-02-04 01:58:07 +0000950 plugin_name.clear();
951 }
952
Greg Claytonb3448432011-03-24 21:19:54 +0000953 const OptionDefinition*
Greg Claytone71e2582011-02-04 01:58:07 +0000954 GetDefinitions ()
955 {
956 return g_option_table;
957 }
958
959 // Options table: Required for subclasses of Options.
960
Greg Claytonb3448432011-03-24 21:19:54 +0000961 static OptionDefinition g_option_table[];
Greg Claytone71e2582011-02-04 01:58:07 +0000962
963 // Instance variables to hold the values for command options.
964
965 std::string plugin_name;
966 };
967
968 CommandObjectProcessConnect (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000969 CommandObjectParsed (interpreter,
970 "process connect",
971 "Connect to a remote debug service.",
972 "process connect <remote-url>",
973 0),
Greg Claytonf15996e2011-04-07 22:46:35 +0000974 m_options (interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000975 {
976 }
977
978 ~CommandObjectProcessConnect ()
979 {
980 }
981
982
Jim Inghamda26bd22012-06-08 21:56:10 +0000983 Options *
984 GetOptions ()
985 {
986 return &m_options;
987 }
988
989protected:
Greg Claytone71e2582011-02-04 01:58:07 +0000990 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000991 DoExecute (Args& command,
Greg Claytone71e2582011-02-04 01:58:07 +0000992 CommandReturnObject &result)
993 {
994
995 TargetSP target_sp (m_interpreter.GetDebugger().GetSelectedTarget());
996 Error error;
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000997 Process *process = m_exe_ctx.GetProcessPtr();
Greg Claytone71e2582011-02-04 01:58:07 +0000998 if (process)
999 {
1000 if (process->IsAlive())
1001 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001002 result.AppendErrorWithFormat ("Process %" PRIu64 " is currently being debugged, kill the process before connecting.\n",
Greg Claytone71e2582011-02-04 01:58:07 +00001003 process->GetID());
1004 result.SetStatus (eReturnStatusFailed);
1005 return false;
1006 }
1007 }
1008
1009 if (!target_sp)
1010 {
1011 // If there isn't a current target create one.
Greg Claytone71e2582011-02-04 01:58:07 +00001012
1013 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
Greg Claytoned0a0fb2012-10-18 16:33:33 +00001014 NULL,
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001015 NULL,
Greg Claytone71e2582011-02-04 01:58:07 +00001016 false,
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001017 NULL, // No platform options
Greg Claytone71e2582011-02-04 01:58:07 +00001018 target_sp);
1019 if (!target_sp || error.Fail())
1020 {
1021 result.AppendError(error.AsCString("Error creating target"));
1022 result.SetStatus (eReturnStatusFailed);
1023 return false;
1024 }
1025 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target_sp.get());
1026 }
1027
1028 if (command.GetArgumentCount() == 1)
1029 {
1030 const char *plugin_name = NULL;
1031 if (!m_options.plugin_name.empty())
1032 plugin_name = m_options.plugin_name.c_str();
1033
1034 const char *remote_url = command.GetArgumentAtIndex(0);
Greg Clayton46c9a352012-02-09 06:16:32 +00001035 process = target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name, NULL).get();
Greg Claytone71e2582011-02-04 01:58:07 +00001036
1037 if (process)
1038 {
Jason Molendafac2e622012-09-29 04:02:01 +00001039 error = process->ConnectRemote (&process->GetTarget().GetDebugger().GetOutputStream(), remote_url);
Greg Claytone71e2582011-02-04 01:58:07 +00001040
1041 if (error.Fail())
1042 {
1043 result.AppendError(error.AsCString("Remote connect failed"));
1044 result.SetStatus (eReturnStatusFailed);
Greg Clayton0cbb93b2012-03-31 00:10:30 +00001045 target_sp->DeleteCurrentProcess();
Greg Claytone71e2582011-02-04 01:58:07 +00001046 return false;
1047 }
1048 }
1049 else
1050 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001051 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 Maleadd068882012-12-18 20:00:40 +00001052 remote_url);
Greg Claytone71e2582011-02-04 01:58:07 +00001053 result.SetStatus (eReturnStatusFailed);
1054 }
1055 }
1056 else
1057 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001058 result.AppendErrorWithFormat ("'%s' takes exactly one argument:\nUsage: %s\n",
Greg Claytone71e2582011-02-04 01:58:07 +00001059 m_cmd_name.c_str(),
1060 m_cmd_syntax.c_str());
1061 result.SetStatus (eReturnStatusFailed);
1062 }
1063 return result.Succeeded();
1064 }
Greg Claytone71e2582011-02-04 01:58:07 +00001065
1066 CommandOptions m_options;
1067};
1068
Greg Claytonb3448432011-03-24 21:19:54 +00001069OptionDefinition
Greg Claytone71e2582011-02-04 01:58:07 +00001070CommandObjectProcessConnect::CommandOptions::g_option_table[] =
1071{
1072 { LLDB_OPT_SET_ALL, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
1073 { 0, false, NULL, 0 , 0, NULL, 0, eArgTypeNone, NULL }
1074};
1075
1076//-------------------------------------------------------------------------
Greg Clayton13193d52012-10-13 02:07:45 +00001077// CommandObjectProcessPlugin
1078//-------------------------------------------------------------------------
1079#pragma mark CommandObjectProcessPlugin
1080
1081class CommandObjectProcessPlugin : public CommandObjectProxy
1082{
1083public:
1084
1085 CommandObjectProcessPlugin (CommandInterpreter &interpreter) :
1086 CommandObjectProxy (interpreter,
1087 "process plugin",
1088 "Send a custom command to the current process plug-in.",
1089 "process plugin <args>",
1090 0)
1091 {
1092 }
1093
1094 ~CommandObjectProcessPlugin ()
1095 {
1096 }
1097
1098 virtual CommandObject *
1099 GetProxyCommandObject()
1100 {
Greg Clayton5ebdb032013-01-09 22:58:18 +00001101 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton13193d52012-10-13 02:07:45 +00001102 if (process)
1103 return process->GetPluginCommandObject();
1104 return NULL;
1105 }
1106};
1107
1108
1109//-------------------------------------------------------------------------
Greg Clayton0baa3942010-11-04 01:54:29 +00001110// CommandObjectProcessLoad
1111//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001112#pragma mark CommandObjectProcessLoad
Greg Clayton0baa3942010-11-04 01:54:29 +00001113
Jim Inghamda26bd22012-06-08 21:56:10 +00001114class CommandObjectProcessLoad : public CommandObjectParsed
Greg Clayton0baa3942010-11-04 01:54:29 +00001115{
1116public:
1117
1118 CommandObjectProcessLoad (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001119 CommandObjectParsed (interpreter,
1120 "process load",
1121 "Load a shared library into the current process.",
1122 "process load <filename> [<filename> ...]",
Greg Claytonea0bb4d2013-01-09 19:44:40 +00001123 eFlagRequiresProcess |
1124 eFlagTryTargetAPILock |
1125 eFlagProcessMustBeLaunched |
1126 eFlagProcessMustBePaused )
Greg Clayton0baa3942010-11-04 01:54:29 +00001127 {
1128 }
1129
1130 ~CommandObjectProcessLoad ()
1131 {
1132 }
1133
Jim Inghamda26bd22012-06-08 21:56:10 +00001134protected:
Greg Clayton0baa3942010-11-04 01:54:29 +00001135 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001136 DoExecute (Args& command,
Greg Clayton0baa3942010-11-04 01:54:29 +00001137 CommandReturnObject &result)
1138 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00001139 Process *process = m_exe_ctx.GetProcessPtr();
Greg Clayton0baa3942010-11-04 01:54:29 +00001140
Greg Clayton36da2aa2013-01-25 18:06:21 +00001141 const size_t argc = command.GetArgumentCount();
Greg Clayton0baa3942010-11-04 01:54:29 +00001142
1143 for (uint32_t i=0; i<argc; ++i)
1144 {
1145 Error error;
1146 const char *image_path = command.GetArgumentAtIndex(i);
1147 FileSpec image_spec (image_path, false);
Greg Claytonf2bf8702011-08-11 16:25:18 +00001148 process->GetTarget().GetPlatform()->ResolveRemotePath(image_spec, image_spec);
Greg Clayton0baa3942010-11-04 01:54:29 +00001149 uint32_t image_token = process->LoadImage(image_spec, error);
1150 if (image_token != LLDB_INVALID_IMAGE_TOKEN)
1151 {
1152 result.AppendMessageWithFormat ("Loading \"%s\"...ok\nImage %u loaded.\n", image_path, image_token);
1153 result.SetStatus (eReturnStatusSuccessFinishResult);
1154 }
1155 else
1156 {
1157 result.AppendErrorWithFormat ("failed to load '%s': %s", image_path, error.AsCString());
1158 result.SetStatus (eReturnStatusFailed);
1159 }
1160 }
1161 return result.Succeeded();
1162 }
1163};
1164
1165
1166//-------------------------------------------------------------------------
1167// CommandObjectProcessUnload
1168//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001169#pragma mark CommandObjectProcessUnload
Greg Clayton0baa3942010-11-04 01:54:29 +00001170
Jim Inghamda26bd22012-06-08 21:56:10 +00001171class CommandObjectProcessUnload : public CommandObjectParsed
Greg Clayton0baa3942010-11-04 01:54:29 +00001172{
1173public:
1174
1175 CommandObjectProcessUnload (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001176 CommandObjectParsed (interpreter,
1177 "process unload",
1178 "Unload a shared library from the current process using the index returned by a previous call to \"process load\".",
1179 "process unload <index>",
Greg Claytonea0bb4d2013-01-09 19:44:40 +00001180 eFlagRequiresProcess |
1181 eFlagTryTargetAPILock |
1182 eFlagProcessMustBeLaunched |
1183 eFlagProcessMustBePaused )
Greg Clayton0baa3942010-11-04 01:54:29 +00001184 {
1185 }
1186
1187 ~CommandObjectProcessUnload ()
1188 {
1189 }
1190
Jim Inghamda26bd22012-06-08 21:56:10 +00001191protected:
Greg Clayton0baa3942010-11-04 01:54:29 +00001192 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001193 DoExecute (Args& command,
Greg Clayton0baa3942010-11-04 01:54:29 +00001194 CommandReturnObject &result)
1195 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00001196 Process *process = m_exe_ctx.GetProcessPtr();
Greg Clayton0baa3942010-11-04 01:54:29 +00001197
Greg Clayton36da2aa2013-01-25 18:06:21 +00001198 const size_t argc = command.GetArgumentCount();
Greg Clayton0baa3942010-11-04 01:54:29 +00001199
1200 for (uint32_t i=0; i<argc; ++i)
1201 {
1202 const char *image_token_cstr = command.GetArgumentAtIndex(i);
1203 uint32_t image_token = Args::StringToUInt32(image_token_cstr, LLDB_INVALID_IMAGE_TOKEN, 0);
1204 if (image_token == LLDB_INVALID_IMAGE_TOKEN)
1205 {
1206 result.AppendErrorWithFormat ("invalid image index argument '%s'", image_token_cstr);
1207 result.SetStatus (eReturnStatusFailed);
1208 break;
1209 }
1210 else
1211 {
1212 Error error (process->UnloadImage(image_token));
1213 if (error.Success())
1214 {
1215 result.AppendMessageWithFormat ("Unloading shared library with index %u...ok\n", image_token);
1216 result.SetStatus (eReturnStatusSuccessFinishResult);
1217 }
1218 else
1219 {
1220 result.AppendErrorWithFormat ("failed to unload image: %s", error.AsCString());
1221 result.SetStatus (eReturnStatusFailed);
1222 break;
1223 }
1224 }
1225 }
1226 return result.Succeeded();
1227 }
1228};
1229
1230//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001231// CommandObjectProcessSignal
1232//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001233#pragma mark CommandObjectProcessSignal
Chris Lattner24943d22010-06-08 16:52:24 +00001234
Jim Inghamda26bd22012-06-08 21:56:10 +00001235class CommandObjectProcessSignal : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001236{
1237public:
1238
Greg Clayton238c0a12010-09-18 01:14:36 +00001239 CommandObjectProcessSignal (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001240 CommandObjectParsed (interpreter,
1241 "process signal",
1242 "Send a UNIX signal to the current process being debugged.",
Greg Claytonea0bb4d2013-01-09 19:44:40 +00001243 NULL,
1244 eFlagRequiresProcess | eFlagTryTargetAPILock)
Chris Lattner24943d22010-06-08 16:52:24 +00001245 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001246 CommandArgumentEntry arg;
1247 CommandArgumentData signal_arg;
1248
1249 // Define the first (and only) variant of this arg.
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001250 signal_arg.arg_type = eArgTypeUnixSignal;
Caroline Tice43b014a2010-10-04 22:28:36 +00001251 signal_arg.arg_repetition = eArgRepeatPlain;
1252
1253 // There is only one variant this argument could be; put it into the argument entry.
1254 arg.push_back (signal_arg);
1255
1256 // Push the data for the first argument into the m_arguments vector.
1257 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001258 }
1259
1260 ~CommandObjectProcessSignal ()
1261 {
1262 }
1263
Jim Inghamda26bd22012-06-08 21:56:10 +00001264protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001265 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001266 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001267 CommandReturnObject &result)
1268 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00001269 Process *process = m_exe_ctx.GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001270
1271 if (command.GetArgumentCount() == 1)
1272 {
Greg Clayton8f6be2a2010-10-09 01:40:57 +00001273 int signo = LLDB_INVALID_SIGNAL_NUMBER;
1274
1275 const char *signal_name = command.GetArgumentAtIndex(0);
1276 if (::isxdigit (signal_name[0]))
1277 signo = Args::StringToSInt32(signal_name, LLDB_INVALID_SIGNAL_NUMBER, 0);
1278 else
1279 signo = process->GetUnixSignals().GetSignalNumberFromName (signal_name);
1280
1281 if (signo == LLDB_INVALID_SIGNAL_NUMBER)
Chris Lattner24943d22010-06-08 16:52:24 +00001282 {
1283 result.AppendErrorWithFormat ("Invalid signal argument '%s'.\n", command.GetArgumentAtIndex(0));
1284 result.SetStatus (eReturnStatusFailed);
1285 }
1286 else
1287 {
1288 Error error (process->Signal (signo));
1289 if (error.Success())
1290 {
1291 result.SetStatus (eReturnStatusSuccessFinishResult);
1292 }
1293 else
1294 {
1295 result.AppendErrorWithFormat ("Failed to send signal %i: %s\n", signo, error.AsCString());
1296 result.SetStatus (eReturnStatusFailed);
1297 }
1298 }
1299 }
1300 else
1301 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001302 result.AppendErrorWithFormat("'%s' takes exactly one signal number argument:\nUsage: %s\n", m_cmd_name.c_str(),
Chris Lattner24943d22010-06-08 16:52:24 +00001303 m_cmd_syntax.c_str());
1304 result.SetStatus (eReturnStatusFailed);
1305 }
1306 return result.Succeeded();
1307 }
1308};
1309
1310
1311//-------------------------------------------------------------------------
1312// CommandObjectProcessInterrupt
1313//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001314#pragma mark CommandObjectProcessInterrupt
Chris Lattner24943d22010-06-08 16:52:24 +00001315
Jim Inghamda26bd22012-06-08 21:56:10 +00001316class CommandObjectProcessInterrupt : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001317{
1318public:
1319
1320
Greg Clayton238c0a12010-09-18 01:14:36 +00001321 CommandObjectProcessInterrupt (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001322 CommandObjectParsed (interpreter,
1323 "process interrupt",
1324 "Interrupt the current process being debugged.",
1325 "process interrupt",
Greg Claytonea0bb4d2013-01-09 19:44:40 +00001326 eFlagRequiresProcess |
1327 eFlagTryTargetAPILock |
Jim Inghamda26bd22012-06-08 21:56:10 +00001328 eFlagProcessMustBeLaunched)
Chris Lattner24943d22010-06-08 16:52:24 +00001329 {
1330 }
1331
1332 ~CommandObjectProcessInterrupt ()
1333 {
1334 }
1335
Jim Inghamda26bd22012-06-08 21:56:10 +00001336protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001337 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001338 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001339 CommandReturnObject &result)
1340 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00001341 Process *process = m_exe_ctx.GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001342 if (process == NULL)
1343 {
1344 result.AppendError ("no process to halt");
1345 result.SetStatus (eReturnStatusFailed);
1346 return false;
1347 }
1348
1349 if (command.GetArgumentCount() == 0)
1350 {
1351 Error error(process->Halt ());
1352 if (error.Success())
1353 {
1354 result.SetStatus (eReturnStatusSuccessFinishResult);
1355
1356 // Maybe we should add a "SuspendThreadPlans so we
1357 // can halt, and keep in place all the current thread plans.
1358 process->GetThreadList().DiscardThreadPlans();
1359 }
1360 else
1361 {
1362 result.AppendErrorWithFormat ("Failed to halt process: %s\n", error.AsCString());
1363 result.SetStatus (eReturnStatusFailed);
1364 }
1365 }
1366 else
1367 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001368 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner24943d22010-06-08 16:52:24 +00001369 m_cmd_name.c_str(),
1370 m_cmd_syntax.c_str());
1371 result.SetStatus (eReturnStatusFailed);
1372 }
1373 return result.Succeeded();
1374 }
1375};
1376
1377//-------------------------------------------------------------------------
1378// CommandObjectProcessKill
1379//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001380#pragma mark CommandObjectProcessKill
Chris Lattner24943d22010-06-08 16:52:24 +00001381
Jim Inghamda26bd22012-06-08 21:56:10 +00001382class CommandObjectProcessKill : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001383{
1384public:
1385
Greg Clayton238c0a12010-09-18 01:14:36 +00001386 CommandObjectProcessKill (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001387 CommandObjectParsed (interpreter,
1388 "process kill",
1389 "Terminate the current process being debugged.",
1390 "process kill",
Greg Claytonea0bb4d2013-01-09 19:44:40 +00001391 eFlagRequiresProcess |
1392 eFlagTryTargetAPILock |
Jim Inghamda26bd22012-06-08 21:56:10 +00001393 eFlagProcessMustBeLaunched)
Chris Lattner24943d22010-06-08 16:52:24 +00001394 {
1395 }
1396
1397 ~CommandObjectProcessKill ()
1398 {
1399 }
1400
Jim Inghamda26bd22012-06-08 21:56:10 +00001401protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001402 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001403 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001404 CommandReturnObject &result)
1405 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00001406 Process *process = m_exe_ctx.GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001407 if (process == NULL)
1408 {
1409 result.AppendError ("no process to kill");
1410 result.SetStatus (eReturnStatusFailed);
1411 return false;
1412 }
1413
1414 if (command.GetArgumentCount() == 0)
1415 {
1416 Error error (process->Destroy());
1417 if (error.Success())
1418 {
1419 result.SetStatus (eReturnStatusSuccessFinishResult);
1420 }
1421 else
1422 {
1423 result.AppendErrorWithFormat ("Failed to kill process: %s\n", error.AsCString());
1424 result.SetStatus (eReturnStatusFailed);
1425 }
1426 }
1427 else
1428 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001429 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner24943d22010-06-08 16:52:24 +00001430 m_cmd_name.c_str(),
1431 m_cmd_syntax.c_str());
1432 result.SetStatus (eReturnStatusFailed);
1433 }
1434 return result.Succeeded();
1435 }
1436};
1437
1438//-------------------------------------------------------------------------
Jim Ingham41313fc2010-06-18 01:23:09 +00001439// CommandObjectProcessStatus
1440//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001441#pragma mark CommandObjectProcessStatus
1442
Jim Inghamda26bd22012-06-08 21:56:10 +00001443class CommandObjectProcessStatus : public CommandObjectParsed
Jim Ingham41313fc2010-06-18 01:23:09 +00001444{
1445public:
Greg Clayton238c0a12010-09-18 01:14:36 +00001446 CommandObjectProcessStatus (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001447 CommandObjectParsed (interpreter,
1448 "process status",
1449 "Show the current status and location of executing process.",
1450 "process status",
Greg Claytonea0bb4d2013-01-09 19:44:40 +00001451 eFlagRequiresProcess | eFlagTryTargetAPILock)
Jim Ingham41313fc2010-06-18 01:23:09 +00001452 {
1453 }
1454
1455 ~CommandObjectProcessStatus()
1456 {
1457 }
1458
1459
1460 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001461 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham41313fc2010-06-18 01:23:09 +00001462 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001463 Stream &strm = result.GetOutputStream();
Jim Ingham41313fc2010-06-18 01:23:09 +00001464 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Claytonea0bb4d2013-01-09 19:44:40 +00001465 // No need to check "process" for validity as eFlagRequiresProcess ensures it is valid
1466 Process *process = m_exe_ctx.GetProcessPtr();
1467 const bool only_threads_with_stop_reason = true;
1468 const uint32_t start_frame = 0;
1469 const uint32_t num_frames = 1;
1470 const uint32_t num_frames_with_source = 1;
1471 process->GetStatus(strm);
1472 process->GetThreadStatus (strm,
1473 only_threads_with_stop_reason,
1474 start_frame,
1475 num_frames,
1476 num_frames_with_source);
Jim Ingham41313fc2010-06-18 01:23:09 +00001477 return result.Succeeded();
1478 }
1479};
1480
1481//-------------------------------------------------------------------------
Caroline Tice23d6f272010-10-13 20:44:39 +00001482// CommandObjectProcessHandle
1483//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001484#pragma mark CommandObjectProcessHandle
Caroline Tice23d6f272010-10-13 20:44:39 +00001485
Jim Inghamda26bd22012-06-08 21:56:10 +00001486class CommandObjectProcessHandle : public CommandObjectParsed
Caroline Tice23d6f272010-10-13 20:44:39 +00001487{
1488public:
1489
1490 class CommandOptions : public Options
1491 {
1492 public:
1493
Greg Claytonf15996e2011-04-07 22:46:35 +00001494 CommandOptions (CommandInterpreter &interpreter) :
1495 Options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001496 {
Greg Clayton143fcc32011-04-13 00:18:08 +00001497 OptionParsingStarting ();
Caroline Tice23d6f272010-10-13 20:44:39 +00001498 }
1499
1500 ~CommandOptions ()
1501 {
1502 }
1503
1504 Error
Greg Clayton143fcc32011-04-13 00:18:08 +00001505 SetOptionValue (uint32_t option_idx, const char *option_arg)
Caroline Tice23d6f272010-10-13 20:44:39 +00001506 {
1507 Error error;
Greg Clayton6475c422012-12-04 00:32:51 +00001508 const int short_option = m_getopt_table[option_idx].val;
Caroline Tice23d6f272010-10-13 20:44:39 +00001509
1510 switch (short_option)
1511 {
1512 case 's':
1513 stop = option_arg;
1514 break;
1515 case 'n':
1516 notify = option_arg;
1517 break;
1518 case 'p':
1519 pass = option_arg;
1520 break;
1521 default:
Greg Clayton9c236732011-10-26 00:56:27 +00001522 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Caroline Tice23d6f272010-10-13 20:44:39 +00001523 break;
1524 }
1525 return error;
1526 }
1527
1528 void
Greg Clayton143fcc32011-04-13 00:18:08 +00001529 OptionParsingStarting ()
Caroline Tice23d6f272010-10-13 20:44:39 +00001530 {
Caroline Tice23d6f272010-10-13 20:44:39 +00001531 stop.clear();
1532 notify.clear();
1533 pass.clear();
1534 }
1535
Greg Claytonb3448432011-03-24 21:19:54 +00001536 const OptionDefinition*
Caroline Tice23d6f272010-10-13 20:44:39 +00001537 GetDefinitions ()
1538 {
1539 return g_option_table;
1540 }
1541
1542 // Options table: Required for subclasses of Options.
1543
Greg Claytonb3448432011-03-24 21:19:54 +00001544 static OptionDefinition g_option_table[];
Caroline Tice23d6f272010-10-13 20:44:39 +00001545
1546 // Instance variables to hold the values for command options.
1547
1548 std::string stop;
1549 std::string notify;
1550 std::string pass;
1551 };
1552
1553
1554 CommandObjectProcessHandle (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001555 CommandObjectParsed (interpreter,
1556 "process handle",
1557 "Show or update what the process and debugger should do with various signals received from the OS.",
1558 NULL),
Greg Claytonf15996e2011-04-07 22:46:35 +00001559 m_options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001560 {
Caroline Ticee7471982010-10-14 21:31:13 +00001561 SetHelpLong ("If no signals are specified, update them all. If no update option is specified, list the current values.\n");
Caroline Tice23d6f272010-10-13 20:44:39 +00001562 CommandArgumentEntry arg;
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001563 CommandArgumentData signal_arg;
Caroline Tice23d6f272010-10-13 20:44:39 +00001564
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001565 signal_arg.arg_type = eArgTypeUnixSignal;
1566 signal_arg.arg_repetition = eArgRepeatStar;
Caroline Tice23d6f272010-10-13 20:44:39 +00001567
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001568 arg.push_back (signal_arg);
Caroline Tice23d6f272010-10-13 20:44:39 +00001569
1570 m_arguments.push_back (arg);
1571 }
1572
1573 ~CommandObjectProcessHandle ()
1574 {
1575 }
1576
1577 Options *
1578 GetOptions ()
1579 {
1580 return &m_options;
1581 }
1582
1583 bool
Caroline Ticee7471982010-10-14 21:31:13 +00001584 VerifyCommandOptionValue (const std::string &option, int &real_value)
Caroline Tice23d6f272010-10-13 20:44:39 +00001585 {
1586 bool okay = true;
1587
Caroline Ticee7471982010-10-14 21:31:13 +00001588 bool success = false;
1589 bool tmp_value = Args::StringToBoolean (option.c_str(), false, &success);
1590
1591 if (success && tmp_value)
1592 real_value = 1;
1593 else if (success && !tmp_value)
1594 real_value = 0;
Caroline Tice23d6f272010-10-13 20:44:39 +00001595 else
1596 {
1597 // If the value isn't 'true' or 'false', it had better be 0 or 1.
Caroline Ticee7471982010-10-14 21:31:13 +00001598 real_value = Args::StringToUInt32 (option.c_str(), 3);
1599 if (real_value != 0 && real_value != 1)
Caroline Tice23d6f272010-10-13 20:44:39 +00001600 okay = false;
1601 }
1602
1603 return okay;
1604 }
1605
Caroline Ticee7471982010-10-14 21:31:13 +00001606 void
1607 PrintSignalHeader (Stream &str)
1608 {
1609 str.Printf ("NAME PASS STOP NOTIFY\n");
1610 str.Printf ("========== ===== ===== ======\n");
1611 }
1612
1613 void
1614 PrintSignal (Stream &str, int32_t signo, const char *sig_name, UnixSignals &signals)
1615 {
1616 bool stop;
1617 bool suppress;
1618 bool notify;
1619
1620 str.Printf ("%-10s ", sig_name);
1621 if (signals.GetSignalInfo (signo, suppress, stop, notify))
1622 {
1623 bool pass = !suppress;
1624 str.Printf ("%s %s %s",
1625 (pass ? "true " : "false"),
1626 (stop ? "true " : "false"),
1627 (notify ? "true " : "false"));
1628 }
1629 str.Printf ("\n");
1630 }
1631
1632 void
1633 PrintSignalInformation (Stream &str, Args &signal_args, int num_valid_signals, UnixSignals &signals)
1634 {
1635 PrintSignalHeader (str);
1636
1637 if (num_valid_signals > 0)
1638 {
1639 size_t num_args = signal_args.GetArgumentCount();
1640 for (size_t i = 0; i < num_args; ++i)
1641 {
1642 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1643 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
1644 PrintSignal (str, signo, signal_args.GetArgumentAtIndex (i), signals);
1645 }
1646 }
1647 else // Print info for ALL signals
1648 {
1649 int32_t signo = signals.GetFirstSignalNumber();
1650 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1651 {
1652 PrintSignal (str, signo, signals.GetSignalAsCString (signo), signals);
1653 signo = signals.GetNextSignalNumber (signo);
1654 }
1655 }
1656 }
1657
Jim Inghamda26bd22012-06-08 21:56:10 +00001658protected:
Caroline Tice23d6f272010-10-13 20:44:39 +00001659 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001660 DoExecute (Args &signal_args, CommandReturnObject &result)
Caroline Tice23d6f272010-10-13 20:44:39 +00001661 {
1662 TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
1663
1664 if (!target_sp)
1665 {
1666 result.AppendError ("No current target;"
1667 " cannot handle signals until you have a valid target and process.\n");
1668 result.SetStatus (eReturnStatusFailed);
1669 return false;
1670 }
1671
1672 ProcessSP process_sp = target_sp->GetProcessSP();
1673
1674 if (!process_sp)
1675 {
1676 result.AppendError ("No current process; cannot handle signals until you have a valid process.\n");
1677 result.SetStatus (eReturnStatusFailed);
1678 return false;
1679 }
1680
Caroline Tice23d6f272010-10-13 20:44:39 +00001681 int stop_action = -1; // -1 means leave the current setting alone
Caroline Ticee7471982010-10-14 21:31:13 +00001682 int pass_action = -1; // -1 means leave the current setting alone
Caroline Tice23d6f272010-10-13 20:44:39 +00001683 int notify_action = -1; // -1 means leave the current setting alone
1684
1685 if (! m_options.stop.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001686 && ! VerifyCommandOptionValue (m_options.stop, stop_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001687 {
1688 result.AppendError ("Invalid argument for command option --stop; must be true or false.\n");
1689 result.SetStatus (eReturnStatusFailed);
1690 return false;
1691 }
1692
1693 if (! m_options.notify.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001694 && ! VerifyCommandOptionValue (m_options.notify, notify_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001695 {
1696 result.AppendError ("Invalid argument for command option --notify; must be true or false.\n");
1697 result.SetStatus (eReturnStatusFailed);
1698 return false;
1699 }
1700
1701 if (! m_options.pass.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001702 && ! VerifyCommandOptionValue (m_options.pass, pass_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001703 {
1704 result.AppendError ("Invalid argument for command option --pass; must be true or false.\n");
1705 result.SetStatus (eReturnStatusFailed);
1706 return false;
1707 }
1708
1709 size_t num_args = signal_args.GetArgumentCount();
1710 UnixSignals &signals = process_sp->GetUnixSignals();
1711 int num_signals_set = 0;
1712
Caroline Ticee7471982010-10-14 21:31:13 +00001713 if (num_args > 0)
Caroline Tice23d6f272010-10-13 20:44:39 +00001714 {
Caroline Ticee7471982010-10-14 21:31:13 +00001715 for (size_t i = 0; i < num_args; ++i)
Caroline Tice23d6f272010-10-13 20:44:39 +00001716 {
Caroline Ticee7471982010-10-14 21:31:13 +00001717 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1718 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
Caroline Tice23d6f272010-10-13 20:44:39 +00001719 {
Caroline Ticee7471982010-10-14 21:31:13 +00001720 // Casting the actions as bools here should be okay, because VerifyCommandOptionValue guarantees
1721 // the value is either 0 or 1.
1722 if (stop_action != -1)
1723 signals.SetShouldStop (signo, (bool) stop_action);
1724 if (pass_action != -1)
1725 {
1726 bool suppress = ! ((bool) pass_action);
1727 signals.SetShouldSuppress (signo, suppress);
1728 }
1729 if (notify_action != -1)
1730 signals.SetShouldNotify (signo, (bool) notify_action);
1731 ++num_signals_set;
Caroline Tice23d6f272010-10-13 20:44:39 +00001732 }
Caroline Ticee7471982010-10-14 21:31:13 +00001733 else
1734 {
1735 result.AppendErrorWithFormat ("Invalid signal name '%s'\n", signal_args.GetArgumentAtIndex (i));
1736 }
Caroline Tice23d6f272010-10-13 20:44:39 +00001737 }
1738 }
Caroline Ticee7471982010-10-14 21:31:13 +00001739 else
1740 {
1741 // No signal specified, if any command options were specified, update ALL signals.
1742 if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1))
1743 {
1744 if (m_interpreter.Confirm ("Do you really want to update all the signals?", false))
1745 {
1746 int32_t signo = signals.GetFirstSignalNumber();
1747 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1748 {
1749 if (notify_action != -1)
1750 signals.SetShouldNotify (signo, (bool) notify_action);
1751 if (stop_action != -1)
1752 signals.SetShouldStop (signo, (bool) stop_action);
1753 if (pass_action != -1)
1754 {
1755 bool suppress = ! ((bool) pass_action);
1756 signals.SetShouldSuppress (signo, suppress);
1757 }
1758 signo = signals.GetNextSignalNumber (signo);
1759 }
1760 }
1761 }
1762 }
1763
1764 PrintSignalInformation (result.GetOutputStream(), signal_args, num_signals_set, signals);
Caroline Tice23d6f272010-10-13 20:44:39 +00001765
1766 if (num_signals_set > 0)
1767 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1768 else
1769 result.SetStatus (eReturnStatusFailed);
1770
1771 return result.Succeeded();
1772 }
1773
Caroline Tice23d6f272010-10-13 20:44:39 +00001774 CommandOptions m_options;
1775};
1776
Greg Claytonb3448432011-03-24 21:19:54 +00001777OptionDefinition
Caroline Tice23d6f272010-10-13 20:44:39 +00001778CommandObjectProcessHandle::CommandOptions::g_option_table[] =
1779{
1780{ LLDB_OPT_SET_1, false, "stop", 's', required_argument, NULL, 0, eArgTypeBoolean, "Whether or not the process should be stopped if the signal is received." },
1781{ LLDB_OPT_SET_1, false, "notify", 'n', required_argument, NULL, 0, eArgTypeBoolean, "Whether or not the debugger should notify the user if the signal is received." },
1782{ LLDB_OPT_SET_1, false, "pass", 'p', required_argument, NULL, 0, eArgTypeBoolean, "Whether or not the signal should be passed to the process." },
1783{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1784};
1785
1786//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001787// CommandObjectMultiwordProcess
1788//-------------------------------------------------------------------------
1789
Greg Clayton63094e02010-06-23 01:19:29 +00001790CommandObjectMultiwordProcess::CommandObjectMultiwordProcess (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00001791 CommandObjectMultiword (interpreter,
1792 "process",
1793 "A set of commands for operating on a process.",
1794 "process <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00001795{
Greg Claytona9eb8272011-07-02 21:07:54 +00001796 LoadSubCommand ("attach", CommandObjectSP (new CommandObjectProcessAttach (interpreter)));
1797 LoadSubCommand ("launch", CommandObjectSP (new CommandObjectProcessLaunch (interpreter)));
1798 LoadSubCommand ("continue", CommandObjectSP (new CommandObjectProcessContinue (interpreter)));
1799 LoadSubCommand ("connect", CommandObjectSP (new CommandObjectProcessConnect (interpreter)));
1800 LoadSubCommand ("detach", CommandObjectSP (new CommandObjectProcessDetach (interpreter)));
1801 LoadSubCommand ("load", CommandObjectSP (new CommandObjectProcessLoad (interpreter)));
1802 LoadSubCommand ("unload", CommandObjectSP (new CommandObjectProcessUnload (interpreter)));
1803 LoadSubCommand ("signal", CommandObjectSP (new CommandObjectProcessSignal (interpreter)));
1804 LoadSubCommand ("handle", CommandObjectSP (new CommandObjectProcessHandle (interpreter)));
1805 LoadSubCommand ("status", CommandObjectSP (new CommandObjectProcessStatus (interpreter)));
Greg Clayton238c0a12010-09-18 01:14:36 +00001806 LoadSubCommand ("interrupt", CommandObjectSP (new CommandObjectProcessInterrupt (interpreter)));
Greg Claytona9eb8272011-07-02 21:07:54 +00001807 LoadSubCommand ("kill", CommandObjectSP (new CommandObjectProcessKill (interpreter)));
Greg Clayton13193d52012-10-13 02:07:45 +00001808 LoadSubCommand ("plugin", CommandObjectSP (new CommandObjectProcessPlugin (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00001809}
1810
1811CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess ()
1812{
1813}
1814