blob: 1996cee57b0aad340c06039315e9f0322ced92c6 [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.",
49 NULL),
Greg Claytonf15996e2011-04-07 22:46:35 +000050 m_options (interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +000051 {
Caroline Tice43b014a2010-10-04 22:28:36 +000052 CommandArgumentEntry arg;
53 CommandArgumentData run_args_arg;
54
55 // Define the first (and only) variant of this arg.
56 run_args_arg.arg_type = eArgTypeRunArgs;
57 run_args_arg.arg_repetition = eArgRepeatOptional;
58
59 // There is only one variant this argument could be; put it into the argument entry.
60 arg.push_back (run_args_arg);
61
62 // Push the data for the first argument into the m_arguments vector.
63 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +000064 }
65
66
67 ~CommandObjectProcessLaunch ()
68 {
69 }
70
Jim Ingham392d9e02012-08-10 21:48:41 +000071 int
72 HandleArgumentCompletion (Args &input,
73 int &cursor_index,
74 int &cursor_char_position,
75 OptionElementVector &opt_element_vector,
76 int match_start_point,
77 int max_return_elements,
78 bool &word_complete,
79 StringList &matches)
80 {
81 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
82 completion_str.erase (cursor_char_position);
83
84 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
85 CommandCompletions::eDiskFileCompletion,
86 completion_str.c_str(),
87 match_start_point,
88 max_return_elements,
89 NULL,
90 word_complete,
91 matches);
92 return matches.GetSize();
93 }
94
Chris Lattner24943d22010-06-08 16:52:24 +000095 Options *
96 GetOptions ()
97 {
98 return &m_options;
99 }
100
Jim Inghamda26bd22012-06-08 21:56:10 +0000101 virtual const char *GetRepeatCommand (Args &current_command_args, uint32_t index)
102 {
103 // No repeat for "process launch"...
104 return "";
105 }
106
107protected:
Chris Lattner24943d22010-06-08 16:52:24 +0000108 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000109 DoExecute (Args& launch_args, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +0000110 {
Greg Claytonabb33022011-11-08 02:43:13 +0000111 Debugger &debugger = m_interpreter.GetDebugger();
112 Target *target = debugger.GetSelectedTarget().get();
113 Error error;
Chris Lattner24943d22010-06-08 16:52:24 +0000114
115 if (target == NULL)
116 {
Greg Claytone1f50b92011-05-03 22:09:39 +0000117 result.AppendError ("invalid target, create a debug target using the 'target create' command");
Chris Lattner24943d22010-06-08 16:52:24 +0000118 result.SetStatus (eReturnStatusFailed);
119 return false;
120 }
Chris Lattner24943d22010-06-08 16:52:24 +0000121 // If our listener is NULL, users aren't allows to launch
Chris Lattner24943d22010-06-08 16:52:24 +0000122 char filename[PATH_MAX];
Greg Clayton5beb99d2011-08-11 02:48:45 +0000123 const Module *exe_module = target->GetExecutableModulePointer();
Greg Claytona2f74232011-02-24 22:24:29 +0000124
125 if (exe_module == NULL)
126 {
Greg Claytone1f50b92011-05-03 22:09:39 +0000127 result.AppendError ("no file in target, create a debug target using the 'target create' command");
Greg Claytona2f74232011-02-24 22:24:29 +0000128 result.SetStatus (eReturnStatusFailed);
129 return false;
130 }
131
Greg Claytona2f74232011-02-24 22:24:29 +0000132 StateType state = eStateInvalid;
Greg Clayton567e7f32011-09-22 04:58:26 +0000133 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytona2f74232011-02-24 22:24:29 +0000134 if (process)
135 {
136 state = process->GetState();
137
138 if (process->IsAlive() && state != eStateConnected)
139 {
140 char message[1024];
141 if (process->GetState() == eStateAttaching)
142 ::strncpy (message, "There is a pending attach, abort it and launch a new process?", sizeof(message));
143 else
144 ::strncpy (message, "There is a running process, kill it and restart?", sizeof(message));
145
146 if (!m_interpreter.Confirm (message, true))
Jim Ingham22dc9722010-12-09 18:58:16 +0000147 {
Greg Claytona2f74232011-02-24 22:24:29 +0000148 result.SetStatus (eReturnStatusFailed);
149 return false;
Jim Ingham22dc9722010-12-09 18:58:16 +0000150 }
151 else
152 {
Greg Claytonabb33022011-11-08 02:43:13 +0000153 Error destroy_error (process->Destroy());
154 if (destroy_error.Success())
Greg Claytona2f74232011-02-24 22:24:29 +0000155 {
156 result.SetStatus (eReturnStatusSuccessFinishResult);
157 }
158 else
159 {
Greg Claytonabb33022011-11-08 02:43:13 +0000160 result.AppendErrorWithFormat ("Failed to kill process: %s\n", destroy_error.AsCString());
Greg Claytona2f74232011-02-24 22:24:29 +0000161 result.SetStatus (eReturnStatusFailed);
162 }
Jim Ingham22dc9722010-12-09 18:58:16 +0000163 }
164 }
Chris Lattner24943d22010-06-08 16:52:24 +0000165 }
Jim Ingham22dc9722010-12-09 18:58:16 +0000166
Greg Clayton0c8446c2012-10-17 22:57:12 +0000167 const char *target_settings_argv0 = target->GetArg0();
168
169 exe_module->GetFileSpec().GetPath (filename, sizeof(filename));
170
171 if (target_settings_argv0)
172 {
173 m_options.launch_info.GetArguments().AppendArgument (target_settings_argv0);
174 m_options.launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), false);
175 }
176 else
177 {
178 m_options.launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
179 }
180
Greg Clayton527154d2011-11-15 03:53:30 +0000181 if (launch_args.GetArgumentCount() == 0)
182 {
Greg Clayton73844aa2012-08-22 17:17:09 +0000183 Args target_setting_args;
Greg Clayton0c8446c2012-10-17 22:57:12 +0000184 if (target->GetRunArguments(target_setting_args))
Greg Clayton73844aa2012-08-22 17:17:09 +0000185 m_options.launch_info.GetArguments().AppendArguments (target_setting_args);
Greg Clayton527154d2011-11-15 03:53:30 +0000186 }
187 else
Greg Claytonabb33022011-11-08 02:43:13 +0000188 {
Greg Clayton0c8446c2012-10-17 22:57:12 +0000189 m_options.launch_info.GetArguments().AppendArguments (launch_args);
190
Greg Clayton3e6f2cc2011-11-21 21:51:18 +0000191 // Save the arguments for subsequent runs in the current target.
192 target->SetRunArguments (launch_args);
Greg Claytonabb33022011-11-08 02:43:13 +0000193 }
Greg Claytonabb33022011-11-08 02:43:13 +0000194
Greg Clayton527154d2011-11-15 03:53:30 +0000195 if (target->GetDisableASLR())
196 m_options.launch_info.GetFlags().Set (eLaunchFlagDisableASLR);
197
198 if (target->GetDisableSTDIO())
199 m_options.launch_info.GetFlags().Set (eLaunchFlagDisableSTDIO);
200
201 m_options.launch_info.GetFlags().Set (eLaunchFlagDebug);
202
203 Args environment;
204 target->GetEnvironmentAsArgs (environment);
205 if (environment.GetArgumentCount() > 0)
206 m_options.launch_info.GetEnvironmentEntries ().AppendArguments (environment);
207
Greg Clayton464c6162011-11-17 22:14:31 +0000208 // Finalize the file actions, and if none were given, default to opening
209 // up a pseudo terminal
210 const bool default_to_use_pty = true;
211 m_options.launch_info.FinalizeFileActions (target, default_to_use_pty);
Greg Clayton527154d2011-11-15 03:53:30 +0000212
Greg Claytonabb33022011-11-08 02:43:13 +0000213 if (state == eStateConnected)
214 {
215 if (m_options.launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
216 {
217 result.AppendWarning("can't launch in tty when launching through a remote connection");
218 m_options.launch_info.GetFlags().Clear (eLaunchFlagLaunchInTTY);
219 }
220 }
221 else
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000222 {
Greg Clayton527154d2011-11-15 03:53:30 +0000223 if (!m_options.launch_info.GetArchitecture().IsValid())
Greg Clayton2d9adb72011-11-12 02:10:56 +0000224 m_options.launch_info.GetArchitecture() = target->GetArchitecture();
225
Greg Clayton75d8c252011-11-28 01:45:00 +0000226 PlatformSP platform_sp (target->GetPlatform());
227
228 if (platform_sp && platform_sp->CanDebugProcess ())
229 {
230 process = target->GetPlatform()->DebugProcess (m_options.launch_info,
231 debugger,
232 target,
233 debugger.GetListener(),
234 error).get();
235 }
236 else
237 {
238 const char *plugin_name = m_options.launch_info.GetProcessPluginName();
Greg Clayton46c9a352012-02-09 06:16:32 +0000239 process = target->CreateProcess (debugger.GetListener(), plugin_name, NULL).get();
Greg Clayton75d8c252011-11-28 01:45:00 +0000240 if (process)
241 error = process->Launch (m_options.launch_info);
242 }
Greg Claytonabb33022011-11-08 02:43:13 +0000243
Greg Claytona2f74232011-02-24 22:24:29 +0000244 if (process == NULL)
245 {
Greg Clayton527154d2011-11-15 03:53:30 +0000246 result.SetError (error, "failed to launch or debug process");
Greg Claytona2f74232011-02-24 22:24:29 +0000247 return false;
248 }
Chris Lattner24943d22010-06-08 16:52:24 +0000249 }
Greg Claytonabb33022011-11-08 02:43:13 +0000250
Greg Clayton238c0a12010-09-18 01:14:36 +0000251 if (error.Success())
252 {
Greg Clayton940b1032011-02-23 00:35:02 +0000253 const char *archname = exe_module->GetArchitecture().GetArchitectureName();
Greg Claytonc1d37752010-10-18 01:45:30 +0000254
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000255 result.AppendMessageWithFormat ("Process %" PRIu64 " launched: '%s' (%s)\n", process->GetID(), filename, archname);
Greg Claytond8c62532010-10-07 04:19:01 +0000256 result.SetDidChangeProcessState (true);
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000257 if (m_options.launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
Greg Clayton238c0a12010-09-18 01:14:36 +0000258 {
Greg Claytond8c62532010-10-07 04:19:01 +0000259 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
Greg Clayton238c0a12010-09-18 01:14:36 +0000260 StateType state = process->WaitForProcessToStop (NULL);
261
262 if (state == eStateStopped)
263 {
Greg Claytond8c62532010-10-07 04:19:01 +0000264 error = process->Resume();
265 if (error.Success())
266 {
267 bool synchronous_execution = m_interpreter.GetSynchronous ();
268 if (synchronous_execution)
269 {
270 state = process->WaitForProcessToStop (NULL);
Greg Clayton20206082011-11-17 01:23:07 +0000271 const bool must_be_alive = true;
272 if (!StateIsStoppedState(state, must_be_alive))
Greg Clayton395fc332011-02-15 21:59:32 +0000273 {
Greg Clayton527154d2011-11-15 03:53:30 +0000274 result.AppendErrorWithFormat ("process isn't stopped: %s", StateAsCString(state));
Greg Clayton395fc332011-02-15 21:59:32 +0000275 }
Greg Claytond8c62532010-10-07 04:19:01 +0000276 result.SetDidChangeProcessState (true);
277 result.SetStatus (eReturnStatusSuccessFinishResult);
278 }
279 else
280 {
281 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
282 }
283 }
Greg Clayton395fc332011-02-15 21:59:32 +0000284 else
285 {
Greg Clayton527154d2011-11-15 03:53:30 +0000286 result.AppendErrorWithFormat ("process resume at entry point failed: %s", error.AsCString());
Greg Clayton395fc332011-02-15 21:59:32 +0000287 result.SetStatus (eReturnStatusFailed);
288 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000289 }
Greg Clayton395fc332011-02-15 21:59:32 +0000290 else
291 {
Greg Clayton527154d2011-11-15 03:53:30 +0000292 result.AppendErrorWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
Greg Clayton395fc332011-02-15 21:59:32 +0000293 result.SetStatus (eReturnStatusFailed);
294 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000295 }
296 }
Greg Clayton395fc332011-02-15 21:59:32 +0000297 else
298 {
Greg Claytona9eb8272011-07-02 21:07:54 +0000299 result.AppendErrorWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton395fc332011-02-15 21:59:32 +0000300 result.SetStatus (eReturnStatusFailed);
301 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000302
Chris Lattner24943d22010-06-08 16:52:24 +0000303 return result.Succeeded();
304 }
305
306protected:
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000307 ProcessLaunchCommandOptions m_options;
Chris Lattner24943d22010-06-08 16:52:24 +0000308};
309
310
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000311//#define SET1 LLDB_OPT_SET_1
312//#define SET2 LLDB_OPT_SET_2
313//#define SET3 LLDB_OPT_SET_3
314//
315//OptionDefinition
316//CommandObjectProcessLaunch::CommandOptions::g_option_table[] =
317//{
318//{ 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 +0000319//{ SET1 , false, "stdin", 'i', required_argument, NULL, 0, eArgTypeDirectoryName, "Redirect stdin for the process to <path>."},
320//{ SET1 , false, "stdout", 'o', required_argument, NULL, 0, eArgTypeDirectoryName, "Redirect stdout for the process to <path>."},
321//{ SET1 , false, "stderr", 'e', required_argument, NULL, 0, eArgTypeDirectoryName, "Redirect stderr for the process to <path>."},
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000322//{ 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 +0000323//{ 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 +0000324//{ 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 +0000325//{ 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 +0000326//{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
327//};
328//
329//#undef SET1
330//#undef SET2
331//#undef SET3
Chris Lattner24943d22010-06-08 16:52:24 +0000332
333//-------------------------------------------------------------------------
334// CommandObjectProcessAttach
335//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000336#pragma mark CommandObjectProcessAttach
Jim Inghamda26bd22012-06-08 21:56:10 +0000337class CommandObjectProcessAttach : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000338{
339public:
340
Chris Lattner24943d22010-06-08 16:52:24 +0000341 class CommandOptions : public Options
342 {
343 public:
344
Greg Claytonf15996e2011-04-07 22:46:35 +0000345 CommandOptions (CommandInterpreter &interpreter) :
346 Options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000347 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000348 // Keep default values of all options in one place: OptionParsingStarting ()
349 OptionParsingStarting ();
Chris Lattner24943d22010-06-08 16:52:24 +0000350 }
351
352 ~CommandOptions ()
353 {
354 }
355
356 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000357 SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +0000358 {
359 Error error;
Greg Clayton6475c422012-12-04 00:32:51 +0000360 const int short_option = m_getopt_table[option_idx].val;
Chris Lattner24943d22010-06-08 16:52:24 +0000361 bool success = false;
362 switch (short_option)
363 {
Johnny Chen7c099972012-05-24 00:43:00 +0000364 case 'c':
365 attach_info.SetContinueOnceAttached(true);
366 break;
367
Chris Lattner24943d22010-06-08 16:52:24 +0000368 case 'p':
Chris Lattner24943d22010-06-08 16:52:24 +0000369 {
Greg Clayton527154d2011-11-15 03:53:30 +0000370 lldb::pid_t pid = Args::StringToUInt32 (option_arg, LLDB_INVALID_PROCESS_ID, 0, &success);
371 if (!success || pid == LLDB_INVALID_PROCESS_ID)
372 {
373 error.SetErrorStringWithFormat("invalid process ID '%s'", option_arg);
374 }
375 else
376 {
377 attach_info.SetProcessID (pid);
378 }
Chris Lattner24943d22010-06-08 16:52:24 +0000379 }
380 break;
381
382 case 'P':
Greg Clayton527154d2011-11-15 03:53:30 +0000383 attach_info.SetProcessPluginName (option_arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000384 break;
385
386 case 'n':
Greg Clayton527154d2011-11-15 03:53:30 +0000387 attach_info.GetExecutableFile().SetFile(option_arg, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000388 break;
389
390 case 'w':
Greg Clayton527154d2011-11-15 03:53:30 +0000391 attach_info.SetWaitForLaunch(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000392 break;
Jim Ingham3a458eb2012-07-20 21:37:13 +0000393
394 case 'i':
395 attach_info.SetIgnoreExisting(false);
396 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000397
398 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000399 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner24943d22010-06-08 16:52:24 +0000400 break;
401 }
402 return error;
403 }
404
405 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000406 OptionParsingStarting ()
Chris Lattner24943d22010-06-08 16:52:24 +0000407 {
Greg Clayton527154d2011-11-15 03:53:30 +0000408 attach_info.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000409 }
410
Greg Claytonb3448432011-03-24 21:19:54 +0000411 const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +0000412 GetDefinitions ()
413 {
414 return g_option_table;
415 }
416
Jim Ingham7508e732010-08-09 23:31:02 +0000417 virtual bool
Greg Claytonf15996e2011-04-07 22:46:35 +0000418 HandleOptionArgumentCompletion (Args &input,
Jim Ingham7508e732010-08-09 23:31:02 +0000419 int cursor_index,
420 int char_pos,
421 OptionElementVector &opt_element_vector,
422 int opt_element_index,
423 int match_start_point,
424 int max_return_elements,
425 bool &word_complete,
426 StringList &matches)
427 {
428 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
429 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
430
431 // We are only completing the name option for now...
432
Greg Claytonb3448432011-03-24 21:19:54 +0000433 const OptionDefinition *opt_defs = GetDefinitions();
Jim Ingham7508e732010-08-09 23:31:02 +0000434 if (opt_defs[opt_defs_index].short_option == 'n')
435 {
436 // Are we in the name?
437
438 // Look to see if there is a -P argument provided, and if so use that plugin, otherwise
439 // use the default plugin.
Jim Ingham7508e732010-08-09 23:31:02 +0000440
441 const char *partial_name = NULL;
442 partial_name = input.GetArgumentAtIndex(opt_arg_pos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000443
Greg Claytonb72d0f02011-04-12 05:54:46 +0000444 PlatformSP platform_sp (m_interpreter.GetPlatform (true));
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000445 if (platform_sp)
Jim Ingham7508e732010-08-09 23:31:02 +0000446 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000447 ProcessInstanceInfoList process_infos;
448 ProcessInstanceInfoMatch match_info;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000449 if (partial_name)
450 {
Greg Clayton527154d2011-11-15 03:53:30 +0000451 match_info.GetProcessInfo().GetExecutableFile().SetFile(partial_name, false);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000452 match_info.SetNameMatchType(eNameMatchStartsWith);
453 }
454 platform_sp->FindProcesses (match_info, process_infos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000455 const uint32_t num_matches = process_infos.GetSize();
456 if (num_matches > 0)
457 {
458 for (uint32_t i=0; i<num_matches; ++i)
459 {
460 matches.AppendString (process_infos.GetProcessNameAtIndex(i),
461 process_infos.GetProcessNameLengthAtIndex(i));
462 }
463 }
Jim Ingham7508e732010-08-09 23:31:02 +0000464 }
465 }
466
467 return false;
468 }
469
Chris Lattner24943d22010-06-08 16:52:24 +0000470 // Options table: Required for subclasses of Options.
471
Greg Claytonb3448432011-03-24 21:19:54 +0000472 static OptionDefinition g_option_table[];
Chris Lattner24943d22010-06-08 16:52:24 +0000473
474 // Instance variables to hold the values for command options.
475
Greg Clayton527154d2011-11-15 03:53:30 +0000476 ProcessAttachInfo attach_info;
Chris Lattner24943d22010-06-08 16:52:24 +0000477 };
478
Greg Clayton238c0a12010-09-18 01:14:36 +0000479 CommandObjectProcessAttach (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000480 CommandObjectParsed (interpreter,
481 "process attach",
482 "Attach to a process.",
483 "process attach <cmd-options>"),
Greg Claytonf15996e2011-04-07 22:46:35 +0000484 m_options (interpreter)
Jim Ingham7508e732010-08-09 23:31:02 +0000485 {
Jim Ingham7508e732010-08-09 23:31:02 +0000486 }
487
488 ~CommandObjectProcessAttach ()
489 {
490 }
491
Jim Inghamda26bd22012-06-08 21:56:10 +0000492 Options *
493 GetOptions ()
494 {
495 return &m_options;
496 }
497
498protected:
Jim Ingham7508e732010-08-09 23:31:02 +0000499 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000500 DoExecute (Args& command,
Jim Ingham7508e732010-08-09 23:31:02 +0000501 CommandReturnObject &result)
502 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000503 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Jim Inghamee940e22011-09-15 01:08:57 +0000504 // N.B. The attach should be synchronous. It doesn't help much to get the prompt back between initiating the attach
505 // and the target actually stopping. So even if the interpreter is set to be asynchronous, we wait for the stop
506 // ourselves here.
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000507
Greg Clayton567e7f32011-09-22 04:58:26 +0000508 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytona2f74232011-02-24 22:24:29 +0000509 StateType state = eStateInvalid;
Jim Ingham7508e732010-08-09 23:31:02 +0000510 if (process)
511 {
Greg Claytona2f74232011-02-24 22:24:29 +0000512 state = process->GetState();
513 if (process->IsAlive() && state != eStateConnected)
Jim Ingham7508e732010-08-09 23:31:02 +0000514 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000515 result.AppendErrorWithFormat ("Process %" PRIu64 " is currently being debugged, kill the process before attaching.\n",
Jim Ingham7508e732010-08-09 23:31:02 +0000516 process->GetID());
517 result.SetStatus (eReturnStatusFailed);
518 return false;
519 }
520 }
521
522 if (target == NULL)
523 {
524 // If there isn't a current target create one.
525 TargetSP new_target_sp;
Jim Ingham7508e732010-08-09 23:31:02 +0000526 Error error;
527
Greg Clayton238c0a12010-09-18 01:14:36 +0000528 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
Greg Claytoned0a0fb2012-10-18 16:33:33 +0000529 NULL,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000530 NULL,
Greg Clayton238c0a12010-09-18 01:14:36 +0000531 false,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000532 NULL, // No platform options
Greg Clayton238c0a12010-09-18 01:14:36 +0000533 new_target_sp);
Jim Ingham7508e732010-08-09 23:31:02 +0000534 target = new_target_sp.get();
535 if (target == NULL || error.Fail())
536 {
Greg Claytone71e2582011-02-04 01:58:07 +0000537 result.AppendError(error.AsCString("Error creating target"));
Jim Ingham7508e732010-08-09 23:31:02 +0000538 return false;
539 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000540 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target);
Jim Ingham7508e732010-08-09 23:31:02 +0000541 }
542
543 // Record the old executable module, we want to issue a warning if the process of attaching changed the
544 // current executable (like somebody said "file foo" then attached to a PID whose executable was bar.)
545
546 ModuleSP old_exec_module_sp = target->GetExecutableModule();
547 ArchSpec old_arch_spec = target->GetArchitecture();
548
549 if (command.GetArgumentCount())
550 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000551 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 +0000552 result.SetStatus (eReturnStatusFailed);
553 }
554 else
555 {
Greg Claytona2f74232011-02-24 22:24:29 +0000556 if (state != eStateConnected)
557 {
Greg Clayton527154d2011-11-15 03:53:30 +0000558 const char *plugin_name = m_options.attach_info.GetProcessPluginName();
Greg Clayton46c9a352012-02-09 06:16:32 +0000559 process = target->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name, NULL).get();
Greg Claytona2f74232011-02-24 22:24:29 +0000560 }
Jim Ingham7508e732010-08-09 23:31:02 +0000561
562 if (process)
563 {
564 Error error;
Greg Clayton527154d2011-11-15 03:53:30 +0000565 // If no process info was specified, then use the target executable
566 // name as the process to attach to by default
567 if (!m_options.attach_info.ProcessInfoSpecified ())
Jim Ingham4805a1c2010-09-15 01:34:14 +0000568 {
569 if (old_exec_module_sp)
Greg Clayton1d1f39e2011-11-29 04:03:30 +0000570 m_options.attach_info.GetExecutableFile().GetFilename() = old_exec_module_sp->GetPlatformFileSpec().GetFilename();
Jim Ingham4805a1c2010-09-15 01:34:14 +0000571
Greg Clayton527154d2011-11-15 03:53:30 +0000572 if (!m_options.attach_info.ProcessInfoSpecified ())
573 {
574 error.SetErrorString ("no process specified, create a target with a file, or specify the --pid or --name command option");
575 }
576 }
577
578 if (error.Success())
579 {
580 error = process->Attach (m_options.attach_info);
581
Jim Ingham4805a1c2010-09-15 01:34:14 +0000582 if (error.Success())
583 {
584 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
585 }
Jim Ingham7508e732010-08-09 23:31:02 +0000586 else
587 {
Greg Clayton527154d2011-11-15 03:53:30 +0000588 result.AppendErrorWithFormat ("attach failed: %s\n", error.AsCString());
Jim Ingham4805a1c2010-09-15 01:34:14 +0000589 result.SetStatus (eReturnStatusFailed);
590 return false;
Jim Ingham7508e732010-08-09 23:31:02 +0000591 }
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000592 // If we're synchronous, wait for the stopped event and report that.
593 // Otherwise just return.
594 // FIXME: in the async case it will now be possible to get to the command
595 // interpreter with a state eStateAttaching. Make sure we handle that correctly.
Jim Inghamee940e22011-09-15 01:08:57 +0000596 StateType state = process->WaitForProcessToStop (NULL);
Greg Clayton527154d2011-11-15 03:53:30 +0000597
Jim Inghamee940e22011-09-15 01:08:57 +0000598 result.SetDidChangeProcessState (true);
Johnny Chen9986a3b2012-05-18 00:51:36 +0000599
600 if (state == eStateStopped)
601 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000602 result.AppendMessageWithFormat ("Process %" PRIu64 " %s\n", process->GetID(), StateAsCString (state));
Johnny Chen9986a3b2012-05-18 00:51:36 +0000603 result.SetStatus (eReturnStatusSuccessFinishNoResult);
604 }
605 else
606 {
607 result.AppendError ("attach failed: process did not stop (no such process or permission problem?)");
Jim Ingham5d90ade2012-07-27 23:57:19 +0000608 process->Destroy();
Johnny Chen9986a3b2012-05-18 00:51:36 +0000609 result.SetStatus (eReturnStatusFailed);
610 return false;
611 }
Jim Ingham7508e732010-08-09 23:31:02 +0000612 }
Jim Ingham7508e732010-08-09 23:31:02 +0000613 }
614 }
615
616 if (result.Succeeded())
617 {
618 // Okay, we're done. Last step is to warn if the executable module has changed:
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000619 char new_path[PATH_MAX];
Greg Clayton5beb99d2011-08-11 02:48:45 +0000620 ModuleSP new_exec_module_sp (target->GetExecutableModule());
Jim Ingham7508e732010-08-09 23:31:02 +0000621 if (!old_exec_module_sp)
622 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000623 // We might not have a module if we attached to a raw pid...
Greg Clayton5beb99d2011-08-11 02:48:45 +0000624 if (new_exec_module_sp)
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000625 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000626 new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000627 result.AppendMessageWithFormat("Executable module set to \"%s\".\n", new_path);
628 }
Jim Ingham7508e732010-08-09 23:31:02 +0000629 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000630 else if (old_exec_module_sp->GetFileSpec() != new_exec_module_sp->GetFileSpec())
Jim Ingham7508e732010-08-09 23:31:02 +0000631 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000632 char old_path[PATH_MAX];
Jim Ingham7508e732010-08-09 23:31:02 +0000633
Greg Clayton5beb99d2011-08-11 02:48:45 +0000634 old_exec_module_sp->GetFileSpec().GetPath (old_path, PATH_MAX);
635 new_exec_module_sp->GetFileSpec().GetPath (new_path, PATH_MAX);
Jim Ingham7508e732010-08-09 23:31:02 +0000636
637 result.AppendWarningWithFormat("Executable module changed from \"%s\" to \"%s\".\n",
638 old_path, new_path);
639 }
640
641 if (!old_arch_spec.IsValid())
642 {
Greg Clayton92cb9a92012-09-14 02:41:36 +0000643 result.AppendMessageWithFormat ("Architecture set to: %s.\n", target->GetArchitecture().GetTriple().getTriple().c_str());
Jim Ingham7508e732010-08-09 23:31:02 +0000644 }
645 else if (old_arch_spec != target->GetArchitecture())
646 {
647 result.AppendWarningWithFormat("Architecture changed from %s to %s.\n",
Greg Clayton92cb9a92012-09-14 02:41:36 +0000648 old_arch_spec.GetTriple().getTriple().c_str(),
649 target->GetArchitecture().GetTriple().getTriple().c_str());
Jim Ingham7508e732010-08-09 23:31:02 +0000650 }
Johnny Chen7c099972012-05-24 00:43:00 +0000651
652 // This supports the use-case scenario of immediately continuing the process once attached.
653 if (m_options.attach_info.GetContinueOnceAttached())
Sean Callanan4336d932012-05-31 01:30:08 +0000654 m_interpreter.HandleCommand("process continue", eLazyBoolNo, result);
Jim Ingham7508e732010-08-09 23:31:02 +0000655 }
656 return result.Succeeded();
657 }
658
Chris Lattner24943d22010-06-08 16:52:24 +0000659 CommandOptions m_options;
660};
661
662
Greg Claytonb3448432011-03-24 21:19:54 +0000663OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000664CommandObjectProcessAttach::CommandOptions::g_option_table[] =
665{
Jim Ingham3a458eb2012-07-20 21:37:13 +0000666{ LLDB_OPT_SET_ALL, false, "continue",'c', no_argument, NULL, 0, eArgTypeNone, "Immediately continue the process once attached."},
667{ LLDB_OPT_SET_ALL, false, "plugin", 'P', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
668{ LLDB_OPT_SET_1, false, "pid", 'p', required_argument, NULL, 0, eArgTypePid, "The process ID of an existing process to attach to."},
669{ LLDB_OPT_SET_2, false, "name", 'n', required_argument, NULL, 0, eArgTypeProcessName, "The name of the process to attach to."},
670{ LLDB_OPT_SET_2, false, "include-existing", 'i', no_argument, NULL, 0, eArgTypeNone, "Include existing processes when doing attach -w."},
671{ 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 +0000672{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000673};
674
675//-------------------------------------------------------------------------
676// CommandObjectProcessContinue
677//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000678#pragma mark CommandObjectProcessContinue
Chris Lattner24943d22010-06-08 16:52:24 +0000679
Jim Inghamda26bd22012-06-08 21:56:10 +0000680class CommandObjectProcessContinue : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000681{
682public:
683
Greg Clayton238c0a12010-09-18 01:14:36 +0000684 CommandObjectProcessContinue (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000685 CommandObjectParsed (interpreter,
686 "process continue",
687 "Continue execution of all threads in the current process.",
688 "process continue",
Jim Ingham124e6902012-08-11 01:27:55 +0000689 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused),
690 m_options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000691 {
692 }
693
694
695 ~CommandObjectProcessContinue ()
696 {
697 }
698
Jim Inghamda26bd22012-06-08 21:56:10 +0000699protected:
Jim Ingham124e6902012-08-11 01:27:55 +0000700
701 class CommandOptions : public Options
702 {
703 public:
704
705 CommandOptions (CommandInterpreter &interpreter) :
706 Options(interpreter)
707 {
708 // Keep default values of all options in one place: OptionParsingStarting ()
709 OptionParsingStarting ();
710 }
711
712 ~CommandOptions ()
713 {
714 }
715
716 Error
717 SetOptionValue (uint32_t option_idx, const char *option_arg)
718 {
719 Error error;
Greg Clayton6475c422012-12-04 00:32:51 +0000720 const int short_option = m_getopt_table[option_idx].val;
Jim Ingham124e6902012-08-11 01:27:55 +0000721 bool success = false;
722 switch (short_option)
723 {
724 case 'i':
725 m_ignore = Args::StringToUInt32 (option_arg, 0, 0, &success);
726 if (!success)
727 error.SetErrorStringWithFormat ("invalid value for ignore option: \"%s\", should be a number.", option_arg);
728 break;
729
730 default:
731 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
732 break;
733 }
734 return error;
735 }
736
737 void
738 OptionParsingStarting ()
739 {
740 m_ignore = 0;
741 }
742
743 const OptionDefinition*
744 GetDefinitions ()
745 {
746 return g_option_table;
747 }
748
749 // Options table: Required for subclasses of Options.
750
751 static OptionDefinition g_option_table[];
752
753 uint32_t m_ignore;
754 };
755
Chris Lattner24943d22010-06-08 16:52:24 +0000756 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000757 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000758 CommandReturnObject &result)
759 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000760 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton238c0a12010-09-18 01:14:36 +0000761 bool synchronous_execution = m_interpreter.GetSynchronous ();
Chris Lattner24943d22010-06-08 16:52:24 +0000762
763 if (process == NULL)
764 {
765 result.AppendError ("no process to continue");
766 result.SetStatus (eReturnStatusFailed);
767 return false;
768 }
769
770 StateType state = process->GetState();
771 if (state == eStateStopped)
772 {
773 if (command.GetArgumentCount() != 0)
774 {
775 result.AppendErrorWithFormat ("The '%s' command does not take any arguments.\n", m_cmd_name.c_str());
776 result.SetStatus (eReturnStatusFailed);
777 return false;
778 }
779
Jim Ingham124e6902012-08-11 01:27:55 +0000780 if (m_options.m_ignore > 0)
781 {
782 ThreadSP sel_thread_sp(process->GetThreadList().GetSelectedThread());
783 if (sel_thread_sp)
784 {
785 StopInfoSP stop_info_sp = sel_thread_sp->GetStopInfo();
786 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint)
787 {
788 uint64_t bp_site_id = stop_info_sp->GetValue();
789 BreakpointSiteSP bp_site_sp(process->GetBreakpointSiteList().FindByID(bp_site_id));
790 if (bp_site_sp)
791 {
792 uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
793 for (uint32_t i = 0; i < num_owners; i++)
794 {
795 Breakpoint &bp_ref = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
796 if (!bp_ref.IsInternal())
797 {
798 bp_ref.SetIgnoreCount(m_options.m_ignore);
799 }
800 }
801 }
802 }
803 }
804 }
805
Jim Inghamb9950592012-09-10 20:50:15 +0000806 { // Scope for thread list mutex:
807 Mutex::Locker locker (process->GetThreadList().GetMutex());
808 const uint32_t num_threads = process->GetThreadList().GetSize();
Chris Lattner24943d22010-06-08 16:52:24 +0000809
Jim Inghamb9950592012-09-10 20:50:15 +0000810 // Set the actions that the threads should each take when resuming
811 for (uint32_t idx=0; idx<num_threads; ++idx)
812 {
813 process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState (eStateRunning);
814 }
Chris Lattner24943d22010-06-08 16:52:24 +0000815 }
Jim Inghamb9950592012-09-10 20:50:15 +0000816
Chris Lattner24943d22010-06-08 16:52:24 +0000817 Error error(process->Resume());
818 if (error.Success())
819 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000820 result.AppendMessageWithFormat ("Process %" PRIu64 " resuming\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000821 if (synchronous_execution)
822 {
Greg Claytonbef15832010-07-14 00:18:15 +0000823 state = process->WaitForProcessToStop (NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000824
825 result.SetDidChangeProcessState (true);
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000826 result.AppendMessageWithFormat ("Process %" PRIu64 " %s\n", process->GetID(), StateAsCString (state));
Chris Lattner24943d22010-06-08 16:52:24 +0000827 result.SetStatus (eReturnStatusSuccessFinishNoResult);
828 }
829 else
830 {
831 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
832 }
833 }
834 else
835 {
836 result.AppendErrorWithFormat("Failed to resume process: %s.\n", error.AsCString());
837 result.SetStatus (eReturnStatusFailed);
838 }
839 }
840 else
841 {
842 result.AppendErrorWithFormat ("Process cannot be continued from its current state (%s).\n",
843 StateAsCString(state));
844 result.SetStatus (eReturnStatusFailed);
845 }
846 return result.Succeeded();
847 }
Jim Ingham124e6902012-08-11 01:27:55 +0000848
849 Options *
850 GetOptions ()
851 {
852 return &m_options;
853 }
854
855 CommandOptions m_options;
856
857};
858
859OptionDefinition
860CommandObjectProcessContinue::CommandOptions::g_option_table[] =
861{
862{ LLDB_OPT_SET_ALL, false, "ignore-count",'i', required_argument, NULL, 0, eArgTypeUnsignedInteger,
863 "Ignore <N> crossings of the breakpoint (if it exists) for the currently selected thread."},
864{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000865};
866
867//-------------------------------------------------------------------------
868// CommandObjectProcessDetach
869//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000870#pragma mark CommandObjectProcessDetach
Chris Lattner24943d22010-06-08 16:52:24 +0000871
Jim Inghamda26bd22012-06-08 21:56:10 +0000872class CommandObjectProcessDetach : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000873{
874public:
875
Greg Clayton238c0a12010-09-18 01:14:36 +0000876 CommandObjectProcessDetach (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000877 CommandObjectParsed (interpreter,
878 "process detach",
879 "Detach from the current process being debugged.",
880 "process detach",
881 eFlagProcessMustBeLaunched)
Chris Lattner24943d22010-06-08 16:52:24 +0000882 {
883 }
884
885 ~CommandObjectProcessDetach ()
886 {
887 }
888
Jim Inghamda26bd22012-06-08 21:56:10 +0000889protected:
Chris Lattner24943d22010-06-08 16:52:24 +0000890 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000891 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000892 CommandReturnObject &result)
893 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000894 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000895 if (process == NULL)
896 {
897 result.AppendError ("must have a valid process in order to detach");
898 result.SetStatus (eReturnStatusFailed);
899 return false;
900 }
901
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000902 result.AppendMessageWithFormat ("Detaching from process %" PRIu64 "\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000903 Error error (process->Detach());
904 if (error.Success())
905 {
906 result.SetStatus (eReturnStatusSuccessFinishResult);
907 }
908 else
909 {
910 result.AppendErrorWithFormat ("Detach failed: %s\n", error.AsCString());
911 result.SetStatus (eReturnStatusFailed);
912 return false;
913 }
914 return result.Succeeded();
915 }
916};
917
918//-------------------------------------------------------------------------
Greg Claytone71e2582011-02-04 01:58:07 +0000919// CommandObjectProcessConnect
920//-------------------------------------------------------------------------
921#pragma mark CommandObjectProcessConnect
922
Jim Inghamda26bd22012-06-08 21:56:10 +0000923class CommandObjectProcessConnect : public CommandObjectParsed
Greg Claytone71e2582011-02-04 01:58:07 +0000924{
925public:
926
927 class CommandOptions : public Options
928 {
929 public:
930
Greg Claytonf15996e2011-04-07 22:46:35 +0000931 CommandOptions (CommandInterpreter &interpreter) :
932 Options(interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000933 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000934 // Keep default values of all options in one place: OptionParsingStarting ()
935 OptionParsingStarting ();
Greg Claytone71e2582011-02-04 01:58:07 +0000936 }
937
938 ~CommandOptions ()
939 {
940 }
941
942 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000943 SetOptionValue (uint32_t option_idx, const char *option_arg)
Greg Claytone71e2582011-02-04 01:58:07 +0000944 {
945 Error error;
Greg Clayton6475c422012-12-04 00:32:51 +0000946 const int short_option = m_getopt_table[option_idx].val;
Greg Claytone71e2582011-02-04 01:58:07 +0000947
948 switch (short_option)
949 {
950 case 'p':
951 plugin_name.assign (option_arg);
952 break;
953
954 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000955 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Greg Claytone71e2582011-02-04 01:58:07 +0000956 break;
957 }
958 return error;
959 }
960
961 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000962 OptionParsingStarting ()
Greg Claytone71e2582011-02-04 01:58:07 +0000963 {
Greg Claytone71e2582011-02-04 01:58:07 +0000964 plugin_name.clear();
965 }
966
Greg Claytonb3448432011-03-24 21:19:54 +0000967 const OptionDefinition*
Greg Claytone71e2582011-02-04 01:58:07 +0000968 GetDefinitions ()
969 {
970 return g_option_table;
971 }
972
973 // Options table: Required for subclasses of Options.
974
Greg Claytonb3448432011-03-24 21:19:54 +0000975 static OptionDefinition g_option_table[];
Greg Claytone71e2582011-02-04 01:58:07 +0000976
977 // Instance variables to hold the values for command options.
978
979 std::string plugin_name;
980 };
981
982 CommandObjectProcessConnect (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000983 CommandObjectParsed (interpreter,
984 "process connect",
985 "Connect to a remote debug service.",
986 "process connect <remote-url>",
987 0),
Greg Claytonf15996e2011-04-07 22:46:35 +0000988 m_options (interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000989 {
990 }
991
992 ~CommandObjectProcessConnect ()
993 {
994 }
995
996
Jim Inghamda26bd22012-06-08 21:56:10 +0000997 Options *
998 GetOptions ()
999 {
1000 return &m_options;
1001 }
1002
1003protected:
Greg Claytone71e2582011-02-04 01:58:07 +00001004 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001005 DoExecute (Args& command,
Greg Claytone71e2582011-02-04 01:58:07 +00001006 CommandReturnObject &result)
1007 {
1008
1009 TargetSP target_sp (m_interpreter.GetDebugger().GetSelectedTarget());
1010 Error error;
Greg Clayton567e7f32011-09-22 04:58:26 +00001011 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytone71e2582011-02-04 01:58:07 +00001012 if (process)
1013 {
1014 if (process->IsAlive())
1015 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001016 result.AppendErrorWithFormat ("Process %" PRIu64 " is currently being debugged, kill the process before connecting.\n",
Greg Claytone71e2582011-02-04 01:58:07 +00001017 process->GetID());
1018 result.SetStatus (eReturnStatusFailed);
1019 return false;
1020 }
1021 }
1022
1023 if (!target_sp)
1024 {
1025 // If there isn't a current target create one.
Greg Claytone71e2582011-02-04 01:58:07 +00001026
1027 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
Greg Claytoned0a0fb2012-10-18 16:33:33 +00001028 NULL,
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001029 NULL,
Greg Claytone71e2582011-02-04 01:58:07 +00001030 false,
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001031 NULL, // No platform options
Greg Claytone71e2582011-02-04 01:58:07 +00001032 target_sp);
1033 if (!target_sp || error.Fail())
1034 {
1035 result.AppendError(error.AsCString("Error creating target"));
1036 result.SetStatus (eReturnStatusFailed);
1037 return false;
1038 }
1039 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target_sp.get());
1040 }
1041
1042 if (command.GetArgumentCount() == 1)
1043 {
1044 const char *plugin_name = NULL;
1045 if (!m_options.plugin_name.empty())
1046 plugin_name = m_options.plugin_name.c_str();
1047
1048 const char *remote_url = command.GetArgumentAtIndex(0);
Greg Clayton46c9a352012-02-09 06:16:32 +00001049 process = target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name, NULL).get();
Greg Claytone71e2582011-02-04 01:58:07 +00001050
1051 if (process)
1052 {
Jason Molendafac2e622012-09-29 04:02:01 +00001053 error = process->ConnectRemote (&process->GetTarget().GetDebugger().GetOutputStream(), remote_url);
Greg Claytone71e2582011-02-04 01:58:07 +00001054
1055 if (error.Fail())
1056 {
1057 result.AppendError(error.AsCString("Remote connect failed"));
1058 result.SetStatus (eReturnStatusFailed);
Greg Clayton0cbb93b2012-03-31 00:10:30 +00001059 target_sp->DeleteCurrentProcess();
Greg Claytone71e2582011-02-04 01:58:07 +00001060 return false;
1061 }
1062 }
1063 else
1064 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001065 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",
1066 m_cmd_name.c_str());
Greg Claytone71e2582011-02-04 01:58:07 +00001067 result.SetStatus (eReturnStatusFailed);
1068 }
1069 }
1070 else
1071 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001072 result.AppendErrorWithFormat ("'%s' takes exactly one argument:\nUsage: %s\n",
Greg Claytone71e2582011-02-04 01:58:07 +00001073 m_cmd_name.c_str(),
1074 m_cmd_syntax.c_str());
1075 result.SetStatus (eReturnStatusFailed);
1076 }
1077 return result.Succeeded();
1078 }
Greg Claytone71e2582011-02-04 01:58:07 +00001079
1080 CommandOptions m_options;
1081};
1082
Greg Claytonb3448432011-03-24 21:19:54 +00001083OptionDefinition
Greg Claytone71e2582011-02-04 01:58:07 +00001084CommandObjectProcessConnect::CommandOptions::g_option_table[] =
1085{
1086 { LLDB_OPT_SET_ALL, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
1087 { 0, false, NULL, 0 , 0, NULL, 0, eArgTypeNone, NULL }
1088};
1089
1090//-------------------------------------------------------------------------
Greg Clayton13193d52012-10-13 02:07:45 +00001091// CommandObjectProcessPlugin
1092//-------------------------------------------------------------------------
1093#pragma mark CommandObjectProcessPlugin
1094
1095class CommandObjectProcessPlugin : public CommandObjectProxy
1096{
1097public:
1098
1099 CommandObjectProcessPlugin (CommandInterpreter &interpreter) :
1100 CommandObjectProxy (interpreter,
1101 "process plugin",
1102 "Send a custom command to the current process plug-in.",
1103 "process plugin <args>",
1104 0)
1105 {
1106 }
1107
1108 ~CommandObjectProcessPlugin ()
1109 {
1110 }
1111
1112 virtual CommandObject *
1113 GetProxyCommandObject()
1114 {
1115 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
1116 if (process)
1117 return process->GetPluginCommandObject();
1118 return NULL;
1119 }
1120};
1121
1122
1123//-------------------------------------------------------------------------
Greg Clayton0baa3942010-11-04 01:54:29 +00001124// CommandObjectProcessLoad
1125//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001126#pragma mark CommandObjectProcessLoad
Greg Clayton0baa3942010-11-04 01:54:29 +00001127
Jim Inghamda26bd22012-06-08 21:56:10 +00001128class CommandObjectProcessLoad : public CommandObjectParsed
Greg Clayton0baa3942010-11-04 01:54:29 +00001129{
1130public:
1131
1132 CommandObjectProcessLoad (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001133 CommandObjectParsed (interpreter,
1134 "process load",
1135 "Load a shared library into the current process.",
1136 "process load <filename> [<filename> ...]",
1137 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
Greg Clayton0baa3942010-11-04 01:54:29 +00001138 {
1139 }
1140
1141 ~CommandObjectProcessLoad ()
1142 {
1143 }
1144
Jim Inghamda26bd22012-06-08 21:56:10 +00001145protected:
Greg Clayton0baa3942010-11-04 01:54:29 +00001146 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001147 DoExecute (Args& command,
Greg Clayton0baa3942010-11-04 01:54:29 +00001148 CommandReturnObject &result)
1149 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001150 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton0baa3942010-11-04 01:54:29 +00001151 if (process == NULL)
1152 {
1153 result.AppendError ("must have a valid process in order to load a shared library");
1154 result.SetStatus (eReturnStatusFailed);
1155 return false;
1156 }
1157
1158 const uint32_t argc = command.GetArgumentCount();
1159
1160 for (uint32_t i=0; i<argc; ++i)
1161 {
1162 Error error;
1163 const char *image_path = command.GetArgumentAtIndex(i);
1164 FileSpec image_spec (image_path, false);
Greg Claytonf2bf8702011-08-11 16:25:18 +00001165 process->GetTarget().GetPlatform()->ResolveRemotePath(image_spec, image_spec);
Greg Clayton0baa3942010-11-04 01:54:29 +00001166 uint32_t image_token = process->LoadImage(image_spec, error);
1167 if (image_token != LLDB_INVALID_IMAGE_TOKEN)
1168 {
1169 result.AppendMessageWithFormat ("Loading \"%s\"...ok\nImage %u loaded.\n", image_path, image_token);
1170 result.SetStatus (eReturnStatusSuccessFinishResult);
1171 }
1172 else
1173 {
1174 result.AppendErrorWithFormat ("failed to load '%s': %s", image_path, error.AsCString());
1175 result.SetStatus (eReturnStatusFailed);
1176 }
1177 }
1178 return result.Succeeded();
1179 }
1180};
1181
1182
1183//-------------------------------------------------------------------------
1184// CommandObjectProcessUnload
1185//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001186#pragma mark CommandObjectProcessUnload
Greg Clayton0baa3942010-11-04 01:54:29 +00001187
Jim Inghamda26bd22012-06-08 21:56:10 +00001188class CommandObjectProcessUnload : public CommandObjectParsed
Greg Clayton0baa3942010-11-04 01:54:29 +00001189{
1190public:
1191
1192 CommandObjectProcessUnload (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001193 CommandObjectParsed (interpreter,
1194 "process unload",
1195 "Unload a shared library from the current process using the index returned by a previous call to \"process load\".",
1196 "process unload <index>",
1197 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
Greg Clayton0baa3942010-11-04 01:54:29 +00001198 {
1199 }
1200
1201 ~CommandObjectProcessUnload ()
1202 {
1203 }
1204
Jim Inghamda26bd22012-06-08 21:56:10 +00001205protected:
Greg Clayton0baa3942010-11-04 01:54:29 +00001206 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001207 DoExecute (Args& command,
Greg Clayton0baa3942010-11-04 01:54:29 +00001208 CommandReturnObject &result)
1209 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001210 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton0baa3942010-11-04 01:54:29 +00001211 if (process == NULL)
1212 {
1213 result.AppendError ("must have a valid process in order to load a shared library");
1214 result.SetStatus (eReturnStatusFailed);
1215 return false;
1216 }
1217
1218 const uint32_t argc = command.GetArgumentCount();
1219
1220 for (uint32_t i=0; i<argc; ++i)
1221 {
1222 const char *image_token_cstr = command.GetArgumentAtIndex(i);
1223 uint32_t image_token = Args::StringToUInt32(image_token_cstr, LLDB_INVALID_IMAGE_TOKEN, 0);
1224 if (image_token == LLDB_INVALID_IMAGE_TOKEN)
1225 {
1226 result.AppendErrorWithFormat ("invalid image index argument '%s'", image_token_cstr);
1227 result.SetStatus (eReturnStatusFailed);
1228 break;
1229 }
1230 else
1231 {
1232 Error error (process->UnloadImage(image_token));
1233 if (error.Success())
1234 {
1235 result.AppendMessageWithFormat ("Unloading shared library with index %u...ok\n", image_token);
1236 result.SetStatus (eReturnStatusSuccessFinishResult);
1237 }
1238 else
1239 {
1240 result.AppendErrorWithFormat ("failed to unload image: %s", error.AsCString());
1241 result.SetStatus (eReturnStatusFailed);
1242 break;
1243 }
1244 }
1245 }
1246 return result.Succeeded();
1247 }
1248};
1249
1250//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001251// CommandObjectProcessSignal
1252//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001253#pragma mark CommandObjectProcessSignal
Chris Lattner24943d22010-06-08 16:52:24 +00001254
Jim Inghamda26bd22012-06-08 21:56:10 +00001255class CommandObjectProcessSignal : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001256{
1257public:
1258
Greg Clayton238c0a12010-09-18 01:14:36 +00001259 CommandObjectProcessSignal (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001260 CommandObjectParsed (interpreter,
1261 "process signal",
1262 "Send a UNIX signal to the current process being debugged.",
1263 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001264 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001265 CommandArgumentEntry arg;
1266 CommandArgumentData signal_arg;
1267
1268 // Define the first (and only) variant of this arg.
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001269 signal_arg.arg_type = eArgTypeUnixSignal;
Caroline Tice43b014a2010-10-04 22:28:36 +00001270 signal_arg.arg_repetition = eArgRepeatPlain;
1271
1272 // There is only one variant this argument could be; put it into the argument entry.
1273 arg.push_back (signal_arg);
1274
1275 // Push the data for the first argument into the m_arguments vector.
1276 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001277 }
1278
1279 ~CommandObjectProcessSignal ()
1280 {
1281 }
1282
Jim Inghamda26bd22012-06-08 21:56:10 +00001283protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001284 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001285 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001286 CommandReturnObject &result)
1287 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001288 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001289 if (process == NULL)
1290 {
1291 result.AppendError ("no process to signal");
1292 result.SetStatus (eReturnStatusFailed);
1293 return false;
1294 }
1295
1296 if (command.GetArgumentCount() == 1)
1297 {
Greg Clayton8f6be2a2010-10-09 01:40:57 +00001298 int signo = LLDB_INVALID_SIGNAL_NUMBER;
1299
1300 const char *signal_name = command.GetArgumentAtIndex(0);
1301 if (::isxdigit (signal_name[0]))
1302 signo = Args::StringToSInt32(signal_name, LLDB_INVALID_SIGNAL_NUMBER, 0);
1303 else
1304 signo = process->GetUnixSignals().GetSignalNumberFromName (signal_name);
1305
1306 if (signo == LLDB_INVALID_SIGNAL_NUMBER)
Chris Lattner24943d22010-06-08 16:52:24 +00001307 {
1308 result.AppendErrorWithFormat ("Invalid signal argument '%s'.\n", command.GetArgumentAtIndex(0));
1309 result.SetStatus (eReturnStatusFailed);
1310 }
1311 else
1312 {
1313 Error error (process->Signal (signo));
1314 if (error.Success())
1315 {
1316 result.SetStatus (eReturnStatusSuccessFinishResult);
1317 }
1318 else
1319 {
1320 result.AppendErrorWithFormat ("Failed to send signal %i: %s\n", signo, error.AsCString());
1321 result.SetStatus (eReturnStatusFailed);
1322 }
1323 }
1324 }
1325 else
1326 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001327 result.AppendErrorWithFormat("'%s' takes exactly one signal number argument:\nUsage: %s\n", m_cmd_name.c_str(),
Chris Lattner24943d22010-06-08 16:52:24 +00001328 m_cmd_syntax.c_str());
1329 result.SetStatus (eReturnStatusFailed);
1330 }
1331 return result.Succeeded();
1332 }
1333};
1334
1335
1336//-------------------------------------------------------------------------
1337// CommandObjectProcessInterrupt
1338//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001339#pragma mark CommandObjectProcessInterrupt
Chris Lattner24943d22010-06-08 16:52:24 +00001340
Jim Inghamda26bd22012-06-08 21:56:10 +00001341class CommandObjectProcessInterrupt : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001342{
1343public:
1344
1345
Greg Clayton238c0a12010-09-18 01:14:36 +00001346 CommandObjectProcessInterrupt (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001347 CommandObjectParsed (interpreter,
1348 "process interrupt",
1349 "Interrupt the current process being debugged.",
1350 "process interrupt",
1351 eFlagProcessMustBeLaunched)
Chris Lattner24943d22010-06-08 16:52:24 +00001352 {
1353 }
1354
1355 ~CommandObjectProcessInterrupt ()
1356 {
1357 }
1358
Jim Inghamda26bd22012-06-08 21:56:10 +00001359protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001360 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001361 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001362 CommandReturnObject &result)
1363 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001364 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001365 if (process == NULL)
1366 {
1367 result.AppendError ("no process to halt");
1368 result.SetStatus (eReturnStatusFailed);
1369 return false;
1370 }
1371
1372 if (command.GetArgumentCount() == 0)
1373 {
1374 Error error(process->Halt ());
1375 if (error.Success())
1376 {
1377 result.SetStatus (eReturnStatusSuccessFinishResult);
1378
1379 // Maybe we should add a "SuspendThreadPlans so we
1380 // can halt, and keep in place all the current thread plans.
1381 process->GetThreadList().DiscardThreadPlans();
1382 }
1383 else
1384 {
1385 result.AppendErrorWithFormat ("Failed to halt process: %s\n", error.AsCString());
1386 result.SetStatus (eReturnStatusFailed);
1387 }
1388 }
1389 else
1390 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001391 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner24943d22010-06-08 16:52:24 +00001392 m_cmd_name.c_str(),
1393 m_cmd_syntax.c_str());
1394 result.SetStatus (eReturnStatusFailed);
1395 }
1396 return result.Succeeded();
1397 }
1398};
1399
1400//-------------------------------------------------------------------------
1401// CommandObjectProcessKill
1402//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001403#pragma mark CommandObjectProcessKill
Chris Lattner24943d22010-06-08 16:52:24 +00001404
Jim Inghamda26bd22012-06-08 21:56:10 +00001405class CommandObjectProcessKill : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001406{
1407public:
1408
Greg Clayton238c0a12010-09-18 01:14:36 +00001409 CommandObjectProcessKill (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001410 CommandObjectParsed (interpreter,
1411 "process kill",
1412 "Terminate the current process being debugged.",
1413 "process kill",
1414 eFlagProcessMustBeLaunched)
Chris Lattner24943d22010-06-08 16:52:24 +00001415 {
1416 }
1417
1418 ~CommandObjectProcessKill ()
1419 {
1420 }
1421
Jim Inghamda26bd22012-06-08 21:56:10 +00001422protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001423 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001424 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001425 CommandReturnObject &result)
1426 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001427 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001428 if (process == NULL)
1429 {
1430 result.AppendError ("no process to kill");
1431 result.SetStatus (eReturnStatusFailed);
1432 return false;
1433 }
1434
1435 if (command.GetArgumentCount() == 0)
1436 {
1437 Error error (process->Destroy());
1438 if (error.Success())
1439 {
1440 result.SetStatus (eReturnStatusSuccessFinishResult);
1441 }
1442 else
1443 {
1444 result.AppendErrorWithFormat ("Failed to kill process: %s\n", error.AsCString());
1445 result.SetStatus (eReturnStatusFailed);
1446 }
1447 }
1448 else
1449 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001450 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner24943d22010-06-08 16:52:24 +00001451 m_cmd_name.c_str(),
1452 m_cmd_syntax.c_str());
1453 result.SetStatus (eReturnStatusFailed);
1454 }
1455 return result.Succeeded();
1456 }
1457};
1458
1459//-------------------------------------------------------------------------
Jim Ingham41313fc2010-06-18 01:23:09 +00001460// CommandObjectProcessStatus
1461//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001462#pragma mark CommandObjectProcessStatus
1463
Jim Inghamda26bd22012-06-08 21:56:10 +00001464class CommandObjectProcessStatus : public CommandObjectParsed
Jim Ingham41313fc2010-06-18 01:23:09 +00001465{
1466public:
Greg Clayton238c0a12010-09-18 01:14:36 +00001467 CommandObjectProcessStatus (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001468 CommandObjectParsed (interpreter,
1469 "process status",
1470 "Show the current status and location of executing process.",
1471 "process status",
1472 0)
Jim Ingham41313fc2010-06-18 01:23:09 +00001473 {
1474 }
1475
1476 ~CommandObjectProcessStatus()
1477 {
1478 }
1479
1480
1481 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001482 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham41313fc2010-06-18 01:23:09 +00001483 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001484 Stream &strm = result.GetOutputStream();
Jim Ingham41313fc2010-06-18 01:23:09 +00001485 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Claytonb72d0f02011-04-12 05:54:46 +00001486 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
Greg Clayton567e7f32011-09-22 04:58:26 +00001487 Process *process = exe_ctx.GetProcessPtr();
1488 if (process)
Jim Ingham41313fc2010-06-18 01:23:09 +00001489 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001490 const bool only_threads_with_stop_reason = true;
1491 const uint32_t start_frame = 0;
1492 const uint32_t num_frames = 1;
1493 const uint32_t num_frames_with_source = 1;
Greg Clayton567e7f32011-09-22 04:58:26 +00001494 process->GetStatus(strm);
1495 process->GetThreadStatus (strm,
1496 only_threads_with_stop_reason,
1497 start_frame,
1498 num_frames,
1499 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001500
Jim Ingham41313fc2010-06-18 01:23:09 +00001501 }
1502 else
1503 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001504 result.AppendError ("No process.");
Jim Ingham41313fc2010-06-18 01:23:09 +00001505 result.SetStatus (eReturnStatusFailed);
1506 }
1507 return result.Succeeded();
1508 }
1509};
1510
1511//-------------------------------------------------------------------------
Caroline Tice23d6f272010-10-13 20:44:39 +00001512// CommandObjectProcessHandle
1513//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001514#pragma mark CommandObjectProcessHandle
Caroline Tice23d6f272010-10-13 20:44:39 +00001515
Jim Inghamda26bd22012-06-08 21:56:10 +00001516class CommandObjectProcessHandle : public CommandObjectParsed
Caroline Tice23d6f272010-10-13 20:44:39 +00001517{
1518public:
1519
1520 class CommandOptions : public Options
1521 {
1522 public:
1523
Greg Claytonf15996e2011-04-07 22:46:35 +00001524 CommandOptions (CommandInterpreter &interpreter) :
1525 Options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001526 {
Greg Clayton143fcc32011-04-13 00:18:08 +00001527 OptionParsingStarting ();
Caroline Tice23d6f272010-10-13 20:44:39 +00001528 }
1529
1530 ~CommandOptions ()
1531 {
1532 }
1533
1534 Error
Greg Clayton143fcc32011-04-13 00:18:08 +00001535 SetOptionValue (uint32_t option_idx, const char *option_arg)
Caroline Tice23d6f272010-10-13 20:44:39 +00001536 {
1537 Error error;
Greg Clayton6475c422012-12-04 00:32:51 +00001538 const int short_option = m_getopt_table[option_idx].val;
Caroline Tice23d6f272010-10-13 20:44:39 +00001539
1540 switch (short_option)
1541 {
1542 case 's':
1543 stop = option_arg;
1544 break;
1545 case 'n':
1546 notify = option_arg;
1547 break;
1548 case 'p':
1549 pass = option_arg;
1550 break;
1551 default:
Greg Clayton9c236732011-10-26 00:56:27 +00001552 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Caroline Tice23d6f272010-10-13 20:44:39 +00001553 break;
1554 }
1555 return error;
1556 }
1557
1558 void
Greg Clayton143fcc32011-04-13 00:18:08 +00001559 OptionParsingStarting ()
Caroline Tice23d6f272010-10-13 20:44:39 +00001560 {
Caroline Tice23d6f272010-10-13 20:44:39 +00001561 stop.clear();
1562 notify.clear();
1563 pass.clear();
1564 }
1565
Greg Claytonb3448432011-03-24 21:19:54 +00001566 const OptionDefinition*
Caroline Tice23d6f272010-10-13 20:44:39 +00001567 GetDefinitions ()
1568 {
1569 return g_option_table;
1570 }
1571
1572 // Options table: Required for subclasses of Options.
1573
Greg Claytonb3448432011-03-24 21:19:54 +00001574 static OptionDefinition g_option_table[];
Caroline Tice23d6f272010-10-13 20:44:39 +00001575
1576 // Instance variables to hold the values for command options.
1577
1578 std::string stop;
1579 std::string notify;
1580 std::string pass;
1581 };
1582
1583
1584 CommandObjectProcessHandle (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001585 CommandObjectParsed (interpreter,
1586 "process handle",
1587 "Show or update what the process and debugger should do with various signals received from the OS.",
1588 NULL),
Greg Claytonf15996e2011-04-07 22:46:35 +00001589 m_options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001590 {
Caroline Ticee7471982010-10-14 21:31:13 +00001591 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 +00001592 CommandArgumentEntry arg;
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001593 CommandArgumentData signal_arg;
Caroline Tice23d6f272010-10-13 20:44:39 +00001594
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001595 signal_arg.arg_type = eArgTypeUnixSignal;
1596 signal_arg.arg_repetition = eArgRepeatStar;
Caroline Tice23d6f272010-10-13 20:44:39 +00001597
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001598 arg.push_back (signal_arg);
Caroline Tice23d6f272010-10-13 20:44:39 +00001599
1600 m_arguments.push_back (arg);
1601 }
1602
1603 ~CommandObjectProcessHandle ()
1604 {
1605 }
1606
1607 Options *
1608 GetOptions ()
1609 {
1610 return &m_options;
1611 }
1612
1613 bool
Caroline Ticee7471982010-10-14 21:31:13 +00001614 VerifyCommandOptionValue (const std::string &option, int &real_value)
Caroline Tice23d6f272010-10-13 20:44:39 +00001615 {
1616 bool okay = true;
1617
Caroline Ticee7471982010-10-14 21:31:13 +00001618 bool success = false;
1619 bool tmp_value = Args::StringToBoolean (option.c_str(), false, &success);
1620
1621 if (success && tmp_value)
1622 real_value = 1;
1623 else if (success && !tmp_value)
1624 real_value = 0;
Caroline Tice23d6f272010-10-13 20:44:39 +00001625 else
1626 {
1627 // If the value isn't 'true' or 'false', it had better be 0 or 1.
Caroline Ticee7471982010-10-14 21:31:13 +00001628 real_value = Args::StringToUInt32 (option.c_str(), 3);
1629 if (real_value != 0 && real_value != 1)
Caroline Tice23d6f272010-10-13 20:44:39 +00001630 okay = false;
1631 }
1632
1633 return okay;
1634 }
1635
Caroline Ticee7471982010-10-14 21:31:13 +00001636 void
1637 PrintSignalHeader (Stream &str)
1638 {
1639 str.Printf ("NAME PASS STOP NOTIFY\n");
1640 str.Printf ("========== ===== ===== ======\n");
1641 }
1642
1643 void
1644 PrintSignal (Stream &str, int32_t signo, const char *sig_name, UnixSignals &signals)
1645 {
1646 bool stop;
1647 bool suppress;
1648 bool notify;
1649
1650 str.Printf ("%-10s ", sig_name);
1651 if (signals.GetSignalInfo (signo, suppress, stop, notify))
1652 {
1653 bool pass = !suppress;
1654 str.Printf ("%s %s %s",
1655 (pass ? "true " : "false"),
1656 (stop ? "true " : "false"),
1657 (notify ? "true " : "false"));
1658 }
1659 str.Printf ("\n");
1660 }
1661
1662 void
1663 PrintSignalInformation (Stream &str, Args &signal_args, int num_valid_signals, UnixSignals &signals)
1664 {
1665 PrintSignalHeader (str);
1666
1667 if (num_valid_signals > 0)
1668 {
1669 size_t num_args = signal_args.GetArgumentCount();
1670 for (size_t i = 0; i < num_args; ++i)
1671 {
1672 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1673 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
1674 PrintSignal (str, signo, signal_args.GetArgumentAtIndex (i), signals);
1675 }
1676 }
1677 else // Print info for ALL signals
1678 {
1679 int32_t signo = signals.GetFirstSignalNumber();
1680 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1681 {
1682 PrintSignal (str, signo, signals.GetSignalAsCString (signo), signals);
1683 signo = signals.GetNextSignalNumber (signo);
1684 }
1685 }
1686 }
1687
Jim Inghamda26bd22012-06-08 21:56:10 +00001688protected:
Caroline Tice23d6f272010-10-13 20:44:39 +00001689 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001690 DoExecute (Args &signal_args, CommandReturnObject &result)
Caroline Tice23d6f272010-10-13 20:44:39 +00001691 {
1692 TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
1693
1694 if (!target_sp)
1695 {
1696 result.AppendError ("No current target;"
1697 " cannot handle signals until you have a valid target and process.\n");
1698 result.SetStatus (eReturnStatusFailed);
1699 return false;
1700 }
1701
1702 ProcessSP process_sp = target_sp->GetProcessSP();
1703
1704 if (!process_sp)
1705 {
1706 result.AppendError ("No current process; cannot handle signals until you have a valid process.\n");
1707 result.SetStatus (eReturnStatusFailed);
1708 return false;
1709 }
1710
Caroline Tice23d6f272010-10-13 20:44:39 +00001711 int stop_action = -1; // -1 means leave the current setting alone
Caroline Ticee7471982010-10-14 21:31:13 +00001712 int pass_action = -1; // -1 means leave the current setting alone
Caroline Tice23d6f272010-10-13 20:44:39 +00001713 int notify_action = -1; // -1 means leave the current setting alone
1714
1715 if (! m_options.stop.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001716 && ! VerifyCommandOptionValue (m_options.stop, stop_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001717 {
1718 result.AppendError ("Invalid argument for command option --stop; must be true or false.\n");
1719 result.SetStatus (eReturnStatusFailed);
1720 return false;
1721 }
1722
1723 if (! m_options.notify.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001724 && ! VerifyCommandOptionValue (m_options.notify, notify_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001725 {
1726 result.AppendError ("Invalid argument for command option --notify; must be true or false.\n");
1727 result.SetStatus (eReturnStatusFailed);
1728 return false;
1729 }
1730
1731 if (! m_options.pass.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001732 && ! VerifyCommandOptionValue (m_options.pass, pass_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001733 {
1734 result.AppendError ("Invalid argument for command option --pass; must be true or false.\n");
1735 result.SetStatus (eReturnStatusFailed);
1736 return false;
1737 }
1738
1739 size_t num_args = signal_args.GetArgumentCount();
1740 UnixSignals &signals = process_sp->GetUnixSignals();
1741 int num_signals_set = 0;
1742
Caroline Ticee7471982010-10-14 21:31:13 +00001743 if (num_args > 0)
Caroline Tice23d6f272010-10-13 20:44:39 +00001744 {
Caroline Ticee7471982010-10-14 21:31:13 +00001745 for (size_t i = 0; i < num_args; ++i)
Caroline Tice23d6f272010-10-13 20:44:39 +00001746 {
Caroline Ticee7471982010-10-14 21:31:13 +00001747 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1748 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
Caroline Tice23d6f272010-10-13 20:44:39 +00001749 {
Caroline Ticee7471982010-10-14 21:31:13 +00001750 // Casting the actions as bools here should be okay, because VerifyCommandOptionValue guarantees
1751 // the value is either 0 or 1.
1752 if (stop_action != -1)
1753 signals.SetShouldStop (signo, (bool) stop_action);
1754 if (pass_action != -1)
1755 {
1756 bool suppress = ! ((bool) pass_action);
1757 signals.SetShouldSuppress (signo, suppress);
1758 }
1759 if (notify_action != -1)
1760 signals.SetShouldNotify (signo, (bool) notify_action);
1761 ++num_signals_set;
Caroline Tice23d6f272010-10-13 20:44:39 +00001762 }
Caroline Ticee7471982010-10-14 21:31:13 +00001763 else
1764 {
1765 result.AppendErrorWithFormat ("Invalid signal name '%s'\n", signal_args.GetArgumentAtIndex (i));
1766 }
Caroline Tice23d6f272010-10-13 20:44:39 +00001767 }
1768 }
Caroline Ticee7471982010-10-14 21:31:13 +00001769 else
1770 {
1771 // No signal specified, if any command options were specified, update ALL signals.
1772 if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1))
1773 {
1774 if (m_interpreter.Confirm ("Do you really want to update all the signals?", false))
1775 {
1776 int32_t signo = signals.GetFirstSignalNumber();
1777 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1778 {
1779 if (notify_action != -1)
1780 signals.SetShouldNotify (signo, (bool) notify_action);
1781 if (stop_action != -1)
1782 signals.SetShouldStop (signo, (bool) stop_action);
1783 if (pass_action != -1)
1784 {
1785 bool suppress = ! ((bool) pass_action);
1786 signals.SetShouldSuppress (signo, suppress);
1787 }
1788 signo = signals.GetNextSignalNumber (signo);
1789 }
1790 }
1791 }
1792 }
1793
1794 PrintSignalInformation (result.GetOutputStream(), signal_args, num_signals_set, signals);
Caroline Tice23d6f272010-10-13 20:44:39 +00001795
1796 if (num_signals_set > 0)
1797 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1798 else
1799 result.SetStatus (eReturnStatusFailed);
1800
1801 return result.Succeeded();
1802 }
1803
Caroline Tice23d6f272010-10-13 20:44:39 +00001804 CommandOptions m_options;
1805};
1806
Greg Claytonb3448432011-03-24 21:19:54 +00001807OptionDefinition
Caroline Tice23d6f272010-10-13 20:44:39 +00001808CommandObjectProcessHandle::CommandOptions::g_option_table[] =
1809{
1810{ 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." },
1811{ 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." },
1812{ LLDB_OPT_SET_1, false, "pass", 'p', required_argument, NULL, 0, eArgTypeBoolean, "Whether or not the signal should be passed to the process." },
1813{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1814};
1815
1816//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001817// CommandObjectMultiwordProcess
1818//-------------------------------------------------------------------------
1819
Greg Clayton63094e02010-06-23 01:19:29 +00001820CommandObjectMultiwordProcess::CommandObjectMultiwordProcess (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00001821 CommandObjectMultiword (interpreter,
1822 "process",
1823 "A set of commands for operating on a process.",
1824 "process <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00001825{
Greg Claytona9eb8272011-07-02 21:07:54 +00001826 LoadSubCommand ("attach", CommandObjectSP (new CommandObjectProcessAttach (interpreter)));
1827 LoadSubCommand ("launch", CommandObjectSP (new CommandObjectProcessLaunch (interpreter)));
1828 LoadSubCommand ("continue", CommandObjectSP (new CommandObjectProcessContinue (interpreter)));
1829 LoadSubCommand ("connect", CommandObjectSP (new CommandObjectProcessConnect (interpreter)));
1830 LoadSubCommand ("detach", CommandObjectSP (new CommandObjectProcessDetach (interpreter)));
1831 LoadSubCommand ("load", CommandObjectSP (new CommandObjectProcessLoad (interpreter)));
1832 LoadSubCommand ("unload", CommandObjectSP (new CommandObjectProcessUnload (interpreter)));
1833 LoadSubCommand ("signal", CommandObjectSP (new CommandObjectProcessSignal (interpreter)));
1834 LoadSubCommand ("handle", CommandObjectSP (new CommandObjectProcessHandle (interpreter)));
1835 LoadSubCommand ("status", CommandObjectSP (new CommandObjectProcessStatus (interpreter)));
Greg Clayton238c0a12010-09-18 01:14:36 +00001836 LoadSubCommand ("interrupt", CommandObjectSP (new CommandObjectProcessInterrupt (interpreter)));
Greg Claytona9eb8272011-07-02 21:07:54 +00001837 LoadSubCommand ("kill", CommandObjectSP (new CommandObjectProcessKill (interpreter)));
Greg Clayton13193d52012-10-13 02:07:45 +00001838 LoadSubCommand ("plugin", CommandObjectSP (new CommandObjectProcessPlugin (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00001839}
1840
1841CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess ()
1842{
1843}
1844