blob: aed04bd6a81822af0deddf5c4db31e60ebdeb57a [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
10#include "CommandObjectProcess.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
Jim Ingham84cdc152010-06-15 19:49:27 +000016#include "lldb/Interpreter/Args.h"
17#include "lldb/Interpreter/Options.h"
Chris Lattner24943d22010-06-08 16:52:24 +000018#include "lldb/Core/State.h"
19#include "lldb/Interpreter/CommandInterpreter.h"
20#include "lldb/Interpreter/CommandReturnObject.h"
Greg Claytone4b9c1f2011-03-08 22:40:15 +000021#include "CommandObjectThread.h"
Greg Claytoncd548032011-02-01 01:31:41 +000022#include "lldb/Host/Host.h"
Greg Claytone4b9c1f2011-03-08 22:40:15 +000023#include "lldb/Target/Platform.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Target/Process.h"
25#include "lldb/Target/Target.h"
26#include "lldb/Target/Thread.h"
27
28using namespace lldb;
29using namespace lldb_private;
30
31//-------------------------------------------------------------------------
32// CommandObjectProcessLaunch
33//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +000034#pragma mark CommandObjectProjectLaunch
Chris Lattner24943d22010-06-08 16:52:24 +000035class CommandObjectProcessLaunch : public CommandObject
36{
37public:
38
39 class CommandOptions : public Options
40 {
41 public:
42
Greg Claytonf15996e2011-04-07 22:46:35 +000043 CommandOptions (CommandInterpreter &interpreter) :
44 Options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +000045 {
Greg Clayton143fcc32011-04-13 00:18:08 +000046 // Keep default values of all options in one place: OptionParsingStarting ()
47 OptionParsingStarting ();
Chris Lattner24943d22010-06-08 16:52:24 +000048 }
49
50 ~CommandOptions ()
51 {
52 }
53
54 Error
Greg Clayton143fcc32011-04-13 00:18:08 +000055 SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +000056 {
57 Error error;
58 char short_option = (char) m_getopt_table[option_idx].val;
59
60 switch (short_option)
61 {
Greg Claytonde915be2011-01-23 05:56:20 +000062 case 's': stop_at_entry = true; break;
63 case 'e': stderr_path.assign (option_arg); break;
64 case 'i': stdin_path.assign (option_arg); break;
65 case 'o': stdout_path.assign (option_arg); break;
66 case 'p': plugin_name.assign (option_arg); break;
67 case 'n': no_stdio = true; break;
68 case 'w': working_dir.assign (option_arg); break;
Greg Claytonbb0c91f2010-10-19 23:16:00 +000069 case 't':
70 if (option_arg && option_arg[0])
71 tty_name.assign (option_arg);
72 in_new_tty = true;
73 break;
Chris Lattner24943d22010-06-08 16:52:24 +000074 default:
75 error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
76 break;
77
78 }
79 return error;
80 }
81
82 void
Greg Clayton143fcc32011-04-13 00:18:08 +000083 OptionParsingStarting ()
Chris Lattner24943d22010-06-08 16:52:24 +000084 {
Chris Lattner24943d22010-06-08 16:52:24 +000085 stop_at_entry = false;
Greg Claytonc1d37752010-10-18 01:45:30 +000086 in_new_tty = false;
Greg Claytonbb0c91f2010-10-19 23:16:00 +000087 tty_name.clear();
Chris Lattner24943d22010-06-08 16:52:24 +000088 stdin_path.clear();
89 stdout_path.clear();
90 stderr_path.clear();
91 plugin_name.clear();
Greg Claytonde915be2011-01-23 05:56:20 +000092 working_dir.clear();
Caroline Ticebd666012010-12-03 18:46:09 +000093 no_stdio = false;
Chris Lattner24943d22010-06-08 16:52:24 +000094 }
95
Greg Claytonb3448432011-03-24 21:19:54 +000096 const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +000097 GetDefinitions ()
98 {
99 return g_option_table;
100 }
101
102 // Options table: Required for subclasses of Options.
103
Greg Claytonb3448432011-03-24 21:19:54 +0000104 static OptionDefinition g_option_table[];
Chris Lattner24943d22010-06-08 16:52:24 +0000105
106 // Instance variables to hold the values for command options.
107
108 bool stop_at_entry;
Greg Claytonc1d37752010-10-18 01:45:30 +0000109 bool in_new_tty;
Caroline Ticebd666012010-12-03 18:46:09 +0000110 bool no_stdio;
Greg Claytonbb0c91f2010-10-19 23:16:00 +0000111 std::string tty_name;
Chris Lattner24943d22010-06-08 16:52:24 +0000112 std::string stderr_path;
113 std::string stdin_path;
114 std::string stdout_path;
115 std::string plugin_name;
Greg Claytonde915be2011-01-23 05:56:20 +0000116 std::string working_dir;
Chris Lattner24943d22010-06-08 16:52:24 +0000117
118 };
119
Greg Clayton238c0a12010-09-18 01:14:36 +0000120 CommandObjectProcessLaunch (CommandInterpreter &interpreter) :
121 CommandObject (interpreter,
122 "process launch",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000123 "Launch the executable in the debugger.",
Greg Claytonf15996e2011-04-07 22:46:35 +0000124 NULL),
125 m_options (interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000126 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000127 CommandArgumentEntry arg;
128 CommandArgumentData run_args_arg;
129
130 // Define the first (and only) variant of this arg.
131 run_args_arg.arg_type = eArgTypeRunArgs;
132 run_args_arg.arg_repetition = eArgRepeatOptional;
133
134 // There is only one variant this argument could be; put it into the argument entry.
135 arg.push_back (run_args_arg);
136
137 // Push the data for the first argument into the m_arguments vector.
138 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000139 }
140
141
142 ~CommandObjectProcessLaunch ()
143 {
144 }
145
146 Options *
147 GetOptions ()
148 {
149 return &m_options;
150 }
151
152 bool
Greg Claytond8c62532010-10-07 04:19:01 +0000153 Execute (Args& launch_args, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +0000154 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000155 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000156
157 if (target == NULL)
158 {
159 result.AppendError ("invalid target, set executable file using 'file' command");
160 result.SetStatus (eReturnStatusFailed);
161 return false;
162 }
163
164 // If our listener is NULL, users aren't allows to launch
Chris Lattner24943d22010-06-08 16:52:24 +0000165 char filename[PATH_MAX];
Greg Claytonc1d37752010-10-18 01:45:30 +0000166 const Module *exe_module = target->GetExecutableModule().get();
Greg Claytona2f74232011-02-24 22:24:29 +0000167
168 if (exe_module == NULL)
169 {
170 result.AppendError ("no file in target, set executable file using 'file' command");
171 result.SetStatus (eReturnStatusFailed);
172 return false;
173 }
174
Chris Lattner24943d22010-06-08 16:52:24 +0000175 exe_module->GetFileSpec().GetPath(filename, sizeof(filename));
176
Greg Claytona2f74232011-02-24 22:24:29 +0000177 StateType state = eStateInvalid;
Greg Claytonb72d0f02011-04-12 05:54:46 +0000178 Process *process = m_interpreter.GetExecutionContext().process;
Greg Claytona2f74232011-02-24 22:24:29 +0000179 if (process)
180 {
181 state = process->GetState();
182
183 if (process->IsAlive() && state != eStateConnected)
184 {
185 char message[1024];
186 if (process->GetState() == eStateAttaching)
187 ::strncpy (message, "There is a pending attach, abort it and launch a new process?", sizeof(message));
188 else
189 ::strncpy (message, "There is a running process, kill it and restart?", sizeof(message));
190
191 if (!m_interpreter.Confirm (message, true))
Jim Ingham22dc9722010-12-09 18:58:16 +0000192 {
Greg Claytona2f74232011-02-24 22:24:29 +0000193 result.SetStatus (eReturnStatusFailed);
194 return false;
Jim Ingham22dc9722010-12-09 18:58:16 +0000195 }
196 else
197 {
Greg Claytona2f74232011-02-24 22:24:29 +0000198 Error error (process->Destroy());
199 if (error.Success())
200 {
201 result.SetStatus (eReturnStatusSuccessFinishResult);
202 }
203 else
204 {
205 result.AppendErrorWithFormat ("Failed to kill process: %s\n", error.AsCString());
206 result.SetStatus (eReturnStatusFailed);
207 }
Jim Ingham22dc9722010-12-09 18:58:16 +0000208 }
209 }
Chris Lattner24943d22010-06-08 16:52:24 +0000210 }
Jim Ingham22dc9722010-12-09 18:58:16 +0000211
Greg Claytona2f74232011-02-24 22:24:29 +0000212 if (state != eStateConnected)
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000213 {
Greg Claytona2f74232011-02-24 22:24:29 +0000214 const char *plugin_name;
215 if (!m_options.plugin_name.empty())
216 plugin_name = m_options.plugin_name.c_str();
217 else
218 plugin_name = NULL;
219
220 process = target->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name).get();
221 if (process == NULL)
222 {
223 result.AppendErrorWithFormat ("Failed to find a process plugin for executable.\n");
224 result.SetStatus (eReturnStatusFailed);
225 return false;
226 }
Chris Lattner24943d22010-06-08 16:52:24 +0000227 }
228
Greg Claytona2f74232011-02-24 22:24:29 +0000229
Greg Clayton238c0a12010-09-18 01:14:36 +0000230 // If no launch args were given on the command line, then use any that
231 // might have been set using the "run-args" set variable.
232 if (launch_args.GetArgumentCount() == 0)
233 {
234 if (process->GetRunArguments().GetArgumentCount() > 0)
235 launch_args = process->GetRunArguments();
236 }
237
Greg Claytonc1d37752010-10-18 01:45:30 +0000238 if (m_options.in_new_tty)
239 {
Greg Claytona2f74232011-02-24 22:24:29 +0000240 if (state == eStateConnected)
Greg Claytonc1d37752010-10-18 01:45:30 +0000241 {
Greg Claytona2f74232011-02-24 22:24:29 +0000242 result.AppendWarning("launch in tty option is ignored when launching through a remote connection");
243 m_options.in_new_tty = false;
Greg Claytonc1d37752010-10-18 01:45:30 +0000244 }
245 else
246 {
Greg Claytona2f74232011-02-24 22:24:29 +0000247 char exec_file_path[PATH_MAX];
248 if (exe_module->GetFileSpec().GetPath(exec_file_path, sizeof(exec_file_path)))
249 {
250 launch_args.InsertArgumentAtIndex(0, exec_file_path);
251 }
252 else
253 {
254 result.AppendError("invalid executable");
255 result.SetStatus (eReturnStatusFailed);
256 return false;
257 }
Greg Claytonc1d37752010-10-18 01:45:30 +0000258 }
259 }
260
Greg Clayton238c0a12010-09-18 01:14:36 +0000261 Args environment;
262
263 process->GetEnvironmentAsArgs (environment);
264
265 uint32_t launch_flags = eLaunchFlagNone;
266
267 if (process->GetDisableASLR())
268 launch_flags |= eLaunchFlagDisableASLR;
Greg Claytonb72d0f02011-04-12 05:54:46 +0000269
270 if (m_options.in_new_tty)
271 launch_flags |= eLaunchFlagLaunchInTTY;
272
Caroline Ticebd666012010-12-03 18:46:09 +0000273 if (m_options.no_stdio)
274 launch_flags |= eLaunchFlagDisableSTDIO;
275 else if (!m_options.in_new_tty
276 && m_options.stdin_path.empty()
277 && m_options.stdout_path.empty()
278 && m_options.stderr_path.empty())
279 {
280 // Only use the settings value if the user hasn't specified any options that would override it.
281 if (process->GetDisableSTDIO())
282 launch_flags |= eLaunchFlagDisableSTDIO;
283 }
284
Greg Claytonc1d37752010-10-18 01:45:30 +0000285 const char **inferior_argv = launch_args.GetArgumentCount() ? launch_args.GetConstArgumentVector() : NULL;
286 const char **inferior_envp = environment.GetArgumentCount() ? environment.GetConstArgumentVector() : NULL;
Greg Clayton238c0a12010-09-18 01:14:36 +0000287
Greg Claytonc1d37752010-10-18 01:45:30 +0000288 Error error;
Greg Claytonde915be2011-01-23 05:56:20 +0000289 const char *working_dir = NULL;
290 if (!m_options.working_dir.empty())
291 working_dir = m_options.working_dir.c_str();
Greg Clayton238c0a12010-09-18 01:14:36 +0000292
Greg Claytonb72d0f02011-04-12 05:54:46 +0000293 const char * stdin_path = NULL;
294 const char * stdout_path = NULL;
295 const char * stderr_path = NULL;
296
297 // Were any standard input/output/error paths given on the command line?
298 if (m_options.stdin_path.empty() &&
299 m_options.stdout_path.empty() &&
300 m_options.stderr_path.empty())
Greg Clayton238c0a12010-09-18 01:14:36 +0000301 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000302 // No standard file handles were given on the command line, check
303 // with the process object in case they were give using "set settings"
304 stdin_path = process->GetStandardInputPath();
305 stdout_path = process->GetStandardOutputPath();
306 stderr_path = process->GetStandardErrorPath();
Greg Clayton238c0a12010-09-18 01:14:36 +0000307 }
308 else
309 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000310 stdin_path = m_options.stdin_path.empty() ? NULL : m_options.stdin_path.c_str();
311 stdout_path = m_options.stdout_path.empty() ? NULL : m_options.stdout_path.c_str();
312 stderr_path = m_options.stderr_path.empty() ? NULL : m_options.stderr_path.c_str();
Greg Clayton238c0a12010-09-18 01:14:36 +0000313 }
Greg Claytonb72d0f02011-04-12 05:54:46 +0000314
315 error = process->Launch (inferior_argv,
316 inferior_envp,
317 launch_flags,
318 stdin_path,
319 stdout_path,
320 stderr_path,
321 working_dir);
Greg Clayton238c0a12010-09-18 01:14:36 +0000322
323 if (error.Success())
324 {
Greg Clayton940b1032011-02-23 00:35:02 +0000325 const char *archname = exe_module->GetArchitecture().GetArchitectureName();
Greg Claytonc1d37752010-10-18 01:45:30 +0000326
327 result.AppendMessageWithFormat ("Process %i launched: '%s' (%s)\n", process->GetID(), filename, archname);
Greg Claytond8c62532010-10-07 04:19:01 +0000328 result.SetDidChangeProcessState (true);
Greg Clayton238c0a12010-09-18 01:14:36 +0000329 if (m_options.stop_at_entry == false)
330 {
Greg Claytond8c62532010-10-07 04:19:01 +0000331 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
Greg Clayton238c0a12010-09-18 01:14:36 +0000332 StateType state = process->WaitForProcessToStop (NULL);
333
334 if (state == eStateStopped)
335 {
Greg Claytond8c62532010-10-07 04:19:01 +0000336 error = process->Resume();
337 if (error.Success())
338 {
339 bool synchronous_execution = m_interpreter.GetSynchronous ();
340 if (synchronous_execution)
341 {
342 state = process->WaitForProcessToStop (NULL);
Greg Clayton940b1032011-02-23 00:35:02 +0000343 if (!StateIsStoppedState(state))
Greg Clayton395fc332011-02-15 21:59:32 +0000344 {
345 result.AppendErrorWithFormat ("Process isn't stopped: %s", StateAsCString(state));
346 }
Greg Claytond8c62532010-10-07 04:19:01 +0000347 result.SetDidChangeProcessState (true);
348 result.SetStatus (eReturnStatusSuccessFinishResult);
349 }
350 else
351 {
352 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
353 }
354 }
Greg Clayton395fc332011-02-15 21:59:32 +0000355 else
356 {
357 result.AppendErrorWithFormat ("Process resume at entry point failed: %s", error.AsCString());
358 result.SetStatus (eReturnStatusFailed);
359 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000360 }
Greg Clayton395fc332011-02-15 21:59:32 +0000361 else
362 {
363 result.AppendErrorWithFormat ("Initial process state wasn't stopped: %s", StateAsCString(state));
364 result.SetStatus (eReturnStatusFailed);
365 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000366 }
367 }
Greg Clayton395fc332011-02-15 21:59:32 +0000368 else
369 {
370 result.AppendErrorWithFormat ("Process launch failed: %s", error.AsCString());
371 result.SetStatus (eReturnStatusFailed);
372 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000373
Chris Lattner24943d22010-06-08 16:52:24 +0000374 return result.Succeeded();
375 }
376
Jim Ingham767af882010-07-07 03:36:20 +0000377 virtual const char *GetRepeatCommand (Args &current_command_args, uint32_t index)
378 {
379 // No repeat for "process launch"...
380 return "";
381 }
382
Chris Lattner24943d22010-06-08 16:52:24 +0000383protected:
384
385 CommandOptions m_options;
386};
387
388
Greg Claytonc1d37752010-10-18 01:45:30 +0000389#define SET1 LLDB_OPT_SET_1
390#define SET2 LLDB_OPT_SET_2
Caroline Ticebd666012010-12-03 18:46:09 +0000391#define SET3 LLDB_OPT_SET_3
Greg Claytonc1d37752010-10-18 01:45:30 +0000392
Greg Claytonb3448432011-03-24 21:19:54 +0000393OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000394CommandObjectProcessLaunch::CommandOptions::g_option_table[] =
395{
Caroline Ticebd666012010-12-03 18:46:09 +0000396{ 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."},
397{ SET1 , false, "stdin", 'i', required_argument, NULL, 0, eArgTypePath, "Redirect stdin for the process to <path>."},
398{ SET1 , false, "stdout", 'o', required_argument, NULL, 0, eArgTypePath, "Redirect stdout for the process to <path>."},
399{ SET1 , false, "stderr", 'e', required_argument, NULL, 0, eArgTypePath, "Redirect stderr for the process to <path>."},
400{ SET1 | SET2 | SET3, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
401{ SET2 , false, "tty", 't', optional_argument, NULL, 0, eArgTypePath, "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."},
402{ SET3, false, "no-stdio", 'n', no_argument, NULL, 0, eArgTypeNone, "Do not set up for terminal I/O to go to running process."},
Greg Claytonde915be2011-01-23 05:56:20 +0000403{ SET1 | SET2 | SET3, false, "working-dir", 'w', required_argument, NULL, 0, eArgTypePath, "Set the current working directory to <path> when running the inferior."},
Caroline Ticebd666012010-12-03 18:46:09 +0000404{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000405};
406
Greg Claytonc1d37752010-10-18 01:45:30 +0000407#undef SET1
408#undef SET2
Caroline Ticebd666012010-12-03 18:46:09 +0000409#undef SET3
Chris Lattner24943d22010-06-08 16:52:24 +0000410
411//-------------------------------------------------------------------------
412// CommandObjectProcessAttach
413//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000414#pragma mark CommandObjectProcessAttach
Chris Lattner24943d22010-06-08 16:52:24 +0000415class CommandObjectProcessAttach : public CommandObject
416{
417public:
418
Chris Lattner24943d22010-06-08 16:52:24 +0000419 class CommandOptions : public Options
420 {
421 public:
422
Greg Claytonf15996e2011-04-07 22:46:35 +0000423 CommandOptions (CommandInterpreter &interpreter) :
424 Options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000425 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000426 // Keep default values of all options in one place: OptionParsingStarting ()
427 OptionParsingStarting ();
Chris Lattner24943d22010-06-08 16:52:24 +0000428 }
429
430 ~CommandOptions ()
431 {
432 }
433
434 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000435 SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +0000436 {
437 Error error;
438 char short_option = (char) m_getopt_table[option_idx].val;
439 bool success = false;
440 switch (short_option)
441 {
442 case 'p':
443 pid = Args::StringToUInt32 (option_arg, LLDB_INVALID_PROCESS_ID, 0, &success);
444 if (!success || pid == LLDB_INVALID_PROCESS_ID)
445 {
446 error.SetErrorStringWithFormat("Invalid process ID '%s'.\n", option_arg);
447 }
448 break;
449
450 case 'P':
451 plugin_name = option_arg;
452 break;
453
454 case 'n':
455 name.assign(option_arg);
456 break;
457
458 case 'w':
459 waitfor = true;
460 break;
461
462 default:
463 error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
464 break;
465 }
466 return error;
467 }
468
469 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000470 OptionParsingStarting ()
Chris Lattner24943d22010-06-08 16:52:24 +0000471 {
Chris Lattner24943d22010-06-08 16:52:24 +0000472 pid = LLDB_INVALID_PROCESS_ID;
473 name.clear();
474 waitfor = false;
475 }
476
Greg Claytonb3448432011-03-24 21:19:54 +0000477 const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +0000478 GetDefinitions ()
479 {
480 return g_option_table;
481 }
482
Jim Ingham7508e732010-08-09 23:31:02 +0000483 virtual bool
Greg Claytonf15996e2011-04-07 22:46:35 +0000484 HandleOptionArgumentCompletion (Args &input,
Jim Ingham7508e732010-08-09 23:31:02 +0000485 int cursor_index,
486 int char_pos,
487 OptionElementVector &opt_element_vector,
488 int opt_element_index,
489 int match_start_point,
490 int max_return_elements,
491 bool &word_complete,
492 StringList &matches)
493 {
494 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
495 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
496
497 // We are only completing the name option for now...
498
Greg Claytonb3448432011-03-24 21:19:54 +0000499 const OptionDefinition *opt_defs = GetDefinitions();
Jim Ingham7508e732010-08-09 23:31:02 +0000500 if (opt_defs[opt_defs_index].short_option == 'n')
501 {
502 // Are we in the name?
503
504 // Look to see if there is a -P argument provided, and if so use that plugin, otherwise
505 // use the default plugin.
Jim Ingham7508e732010-08-09 23:31:02 +0000506
507 const char *partial_name = NULL;
508 partial_name = input.GetArgumentAtIndex(opt_arg_pos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000509
Greg Claytonb72d0f02011-04-12 05:54:46 +0000510 PlatformSP platform_sp (m_interpreter.GetPlatform (true));
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000511 if (platform_sp)
Jim Ingham7508e732010-08-09 23:31:02 +0000512 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000513 ProcessInstanceInfoList process_infos;
514 ProcessInstanceInfoMatch match_info;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000515 if (partial_name)
516 {
517 match_info.GetProcessInfo().SetName(partial_name);
518 match_info.SetNameMatchType(eNameMatchStartsWith);
519 }
520 platform_sp->FindProcesses (match_info, process_infos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000521 const uint32_t num_matches = process_infos.GetSize();
522 if (num_matches > 0)
523 {
524 for (uint32_t i=0; i<num_matches; ++i)
525 {
526 matches.AppendString (process_infos.GetProcessNameAtIndex(i),
527 process_infos.GetProcessNameLengthAtIndex(i));
528 }
529 }
Jim Ingham7508e732010-08-09 23:31:02 +0000530 }
531 }
532
533 return false;
534 }
535
Chris Lattner24943d22010-06-08 16:52:24 +0000536 // Options table: Required for subclasses of Options.
537
Greg Claytonb3448432011-03-24 21:19:54 +0000538 static OptionDefinition g_option_table[];
Chris Lattner24943d22010-06-08 16:52:24 +0000539
540 // Instance variables to hold the values for command options.
541
542 lldb::pid_t pid;
543 std::string plugin_name;
544 std::string name;
545 bool waitfor;
546 };
547
Greg Clayton238c0a12010-09-18 01:14:36 +0000548 CommandObjectProcessAttach (CommandInterpreter &interpreter) :
549 CommandObject (interpreter,
550 "process attach",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000551 "Attach to a process.",
Greg Claytonf15996e2011-04-07 22:46:35 +0000552 "process attach <cmd-options>"),
553 m_options (interpreter)
Jim Ingham7508e732010-08-09 23:31:02 +0000554 {
Jim Ingham7508e732010-08-09 23:31:02 +0000555 }
556
557 ~CommandObjectProcessAttach ()
558 {
559 }
560
561 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000562 Execute (Args& command,
Jim Ingham7508e732010-08-09 23:31:02 +0000563 CommandReturnObject &result)
564 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000565 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000566 bool synchronous_execution = m_interpreter.GetSynchronous ();
567
Greg Claytonb72d0f02011-04-12 05:54:46 +0000568 Process *process = m_interpreter.GetExecutionContext().process;
Greg Claytona2f74232011-02-24 22:24:29 +0000569 StateType state = eStateInvalid;
Jim Ingham7508e732010-08-09 23:31:02 +0000570 if (process)
571 {
Greg Claytona2f74232011-02-24 22:24:29 +0000572 state = process->GetState();
573 if (process->IsAlive() && state != eStateConnected)
Jim Ingham7508e732010-08-09 23:31:02 +0000574 {
575 result.AppendErrorWithFormat ("Process %u is currently being debugged, kill the process before attaching.\n",
576 process->GetID());
577 result.SetStatus (eReturnStatusFailed);
578 return false;
579 }
580 }
581
582 if (target == NULL)
583 {
584 // If there isn't a current target create one.
585 TargetSP new_target_sp;
586 FileSpec emptyFileSpec;
587 ArchSpec emptyArchSpec;
588 Error error;
589
Greg Clayton238c0a12010-09-18 01:14:36 +0000590 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
591 emptyFileSpec,
592 emptyArchSpec,
Greg Clayton238c0a12010-09-18 01:14:36 +0000593 false,
594 new_target_sp);
Jim Ingham7508e732010-08-09 23:31:02 +0000595 target = new_target_sp.get();
596 if (target == NULL || error.Fail())
597 {
Greg Claytone71e2582011-02-04 01:58:07 +0000598 result.AppendError(error.AsCString("Error creating target"));
Jim Ingham7508e732010-08-09 23:31:02 +0000599 return false;
600 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000601 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target);
Jim Ingham7508e732010-08-09 23:31:02 +0000602 }
603
604 // Record the old executable module, we want to issue a warning if the process of attaching changed the
605 // current executable (like somebody said "file foo" then attached to a PID whose executable was bar.)
606
607 ModuleSP old_exec_module_sp = target->GetExecutableModule();
608 ArchSpec old_arch_spec = target->GetArchitecture();
609
610 if (command.GetArgumentCount())
611 {
612 result.AppendErrorWithFormat("Invalid arguments for '%s'.\nUsage: \n", m_cmd_name.c_str(), m_cmd_syntax.c_str());
613 result.SetStatus (eReturnStatusFailed);
614 }
615 else
616 {
Greg Claytona2f74232011-02-24 22:24:29 +0000617 if (state != eStateConnected)
618 {
619 const char *plugin_name = NULL;
620
621 if (!m_options.plugin_name.empty())
622 plugin_name = m_options.plugin_name.c_str();
Jim Ingham7508e732010-08-09 23:31:02 +0000623
Greg Claytona2f74232011-02-24 22:24:29 +0000624 process = target->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name).get();
625 }
Jim Ingham7508e732010-08-09 23:31:02 +0000626
627 if (process)
628 {
629 Error error;
630 int attach_pid = m_options.pid;
631
Jim Ingham4805a1c2010-09-15 01:34:14 +0000632 const char *wait_name = NULL;
633
634 if (m_options.name.empty())
635 {
636 if (old_exec_module_sp)
637 {
638 wait_name = old_exec_module_sp->GetFileSpec().GetFilename().AsCString();
639 }
640 }
641 else
642 {
643 wait_name = m_options.name.c_str();
644 }
645
Jim Ingham7508e732010-08-09 23:31:02 +0000646 // If we are waiting for a process with this name to show up, do that first.
647 if (m_options.waitfor)
648 {
Jim Ingham4805a1c2010-09-15 01:34:14 +0000649
650 if (wait_name == NULL)
Jim Ingham7508e732010-08-09 23:31:02 +0000651 {
Jim Ingham4805a1c2010-09-15 01:34:14 +0000652 result.AppendError("Invalid arguments: must have a file loaded or supply a process name with the waitfor option.\n");
Jim Ingham7508e732010-08-09 23:31:02 +0000653 result.SetStatus (eReturnStatusFailed);
654 return false;
655 }
Jim Ingham4805a1c2010-09-15 01:34:14 +0000656
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000657 result.AppendMessageWithFormat("Waiting to attach to a process named \"%s\".\n", wait_name);
Jim Ingham4805a1c2010-09-15 01:34:14 +0000658 error = process->Attach (wait_name, m_options.waitfor);
659 if (error.Success())
660 {
661 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
662 }
Jim Ingham7508e732010-08-09 23:31:02 +0000663 else
664 {
Jim Ingham4805a1c2010-09-15 01:34:14 +0000665 result.AppendErrorWithFormat ("Waiting for a process to launch named '%s': %s\n",
666 wait_name,
667 error.AsCString());
668 result.SetStatus (eReturnStatusFailed);
669 return false;
Jim Ingham7508e732010-08-09 23:31:02 +0000670 }
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000671 // If we're synchronous, wait for the stopped event and report that.
672 // Otherwise just return.
673 // FIXME: in the async case it will now be possible to get to the command
674 // interpreter with a state eStateAttaching. Make sure we handle that correctly.
675 if (synchronous_execution)
676 {
677 StateType state = process->WaitForProcessToStop (NULL);
678
679 result.SetDidChangeProcessState (true);
680 result.AppendMessageWithFormat ("Process %i %s\n", process->GetID(), StateAsCString (state));
681 result.SetStatus (eReturnStatusSuccessFinishNoResult);
682 }
683 else
684 {
685 result.SetDidChangeProcessState (true);
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000686 result.SetStatus (eReturnStatusSuccessFinishNoResult);
687 }
Jim Ingham7508e732010-08-09 23:31:02 +0000688 }
689 else
690 {
691 // If the process was specified by name look it up, so we can warn if there are multiple
692 // processes with this pid.
693
Jim Ingham4805a1c2010-09-15 01:34:14 +0000694 if (attach_pid == LLDB_INVALID_PROCESS_ID && wait_name != NULL)
Jim Ingham7508e732010-08-09 23:31:02 +0000695 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000696 ProcessInstanceInfoList process_infos;
697 PlatformSP platform_sp (m_interpreter.GetPlatform (true));
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000698 if (platform_sp)
699 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000700 ProcessInstanceInfoMatch match_info (wait_name, eNameMatchEquals);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000701 platform_sp->FindProcesses (match_info, process_infos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000702 }
703 if (process_infos.GetSize() > 1)
Jim Ingham7508e732010-08-09 23:31:02 +0000704 {
Jim Ingham4805a1c2010-09-15 01:34:14 +0000705 result.AppendErrorWithFormat("More than one process named %s\n", wait_name);
Jim Ingham7508e732010-08-09 23:31:02 +0000706 result.SetStatus (eReturnStatusFailed);
707 return false;
708 }
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000709 else if (process_infos.GetSize() == 0)
Jim Ingham7508e732010-08-09 23:31:02 +0000710 {
Jim Ingham4805a1c2010-09-15 01:34:14 +0000711 result.AppendErrorWithFormat("Could not find a process named %s\n", wait_name);
Jim Ingham7508e732010-08-09 23:31:02 +0000712 result.SetStatus (eReturnStatusFailed);
713 return false;
714 }
715 else
716 {
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000717 attach_pid = process_infos.GetProcessIDAtIndex (0);
Jim Ingham7508e732010-08-09 23:31:02 +0000718 }
Jim Ingham7508e732010-08-09 23:31:02 +0000719 }
720
721 if (attach_pid != LLDB_INVALID_PROCESS_ID)
722 {
723 error = process->Attach (attach_pid);
724 if (error.Success())
725 {
726 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
727 }
728 else
729 {
730 result.AppendErrorWithFormat ("Attaching to process %i failed: %s.\n",
731 attach_pid,
732 error.AsCString());
733 result.SetStatus (eReturnStatusFailed);
734 }
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000735 // See comment for synchronous_execution above.
736 if (synchronous_execution)
737 {
738 StateType state = process->WaitForProcessToStop (NULL);
739
740 result.SetDidChangeProcessState (true);
741 result.AppendMessageWithFormat ("Process %i %s\n", process->GetID(), StateAsCString (state));
742 result.SetStatus (eReturnStatusSuccessFinishNoResult);
743 }
744 else
745 {
746 result.SetDidChangeProcessState (true);
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000747 result.SetStatus (eReturnStatusSuccessFinishNoResult);
748 }
Jim Ingham7508e732010-08-09 23:31:02 +0000749 }
750 else
751 {
752 result.AppendErrorWithFormat ("No PID specified for attach\n",
753 attach_pid,
754 error.AsCString());
755 result.SetStatus (eReturnStatusFailed);
756
757 }
758 }
759 }
760 }
761
762 if (result.Succeeded())
763 {
764 // Okay, we're done. Last step is to warn if the executable module has changed:
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000765 char new_path[PATH_MAX];
Jim Ingham7508e732010-08-09 23:31:02 +0000766 if (!old_exec_module_sp)
767 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000768 // We might not have a module if we attached to a raw pid...
769 ModuleSP new_module_sp (target->GetExecutableModule());
770 if (new_module_sp)
771 {
772 new_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
773 result.AppendMessageWithFormat("Executable module set to \"%s\".\n", new_path);
774 }
Jim Ingham7508e732010-08-09 23:31:02 +0000775 }
776 else if (old_exec_module_sp->GetFileSpec() != target->GetExecutableModule()->GetFileSpec())
777 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000778 char old_path[PATH_MAX];
Jim Ingham7508e732010-08-09 23:31:02 +0000779
780 old_exec_module_sp->GetFileSpec().GetPath(old_path, PATH_MAX);
781 target->GetExecutableModule()->GetFileSpec().GetPath (new_path, PATH_MAX);
782
783 result.AppendWarningWithFormat("Executable module changed from \"%s\" to \"%s\".\n",
784 old_path, new_path);
785 }
786
787 if (!old_arch_spec.IsValid())
788 {
Greg Clayton940b1032011-02-23 00:35:02 +0000789 result.AppendMessageWithFormat ("Architecture set to: %s.\n", target->GetArchitecture().GetArchitectureName());
Jim Ingham7508e732010-08-09 23:31:02 +0000790 }
791 else if (old_arch_spec != target->GetArchitecture())
792 {
793 result.AppendWarningWithFormat("Architecture changed from %s to %s.\n",
Greg Clayton940b1032011-02-23 00:35:02 +0000794 old_arch_spec.GetArchitectureName(), target->GetArchitecture().GetArchitectureName());
Jim Ingham7508e732010-08-09 23:31:02 +0000795 }
796 }
797 return result.Succeeded();
798 }
799
800 Options *
801 GetOptions ()
802 {
803 return &m_options;
804 }
805
Chris Lattner24943d22010-06-08 16:52:24 +0000806protected:
807
808 CommandOptions m_options;
809};
810
811
Greg Claytonb3448432011-03-24 21:19:54 +0000812OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000813CommandObjectProcessAttach::CommandOptions::g_option_table[] =
814{
Caroline Tice4d6675c2010-10-01 19:59:14 +0000815{ LLDB_OPT_SET_ALL, false, "plugin", 'P', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
816{ LLDB_OPT_SET_1, false, "pid", 'p', required_argument, NULL, 0, eArgTypePid, "The process ID of an existing process to attach to."},
817{ LLDB_OPT_SET_2, false, "name", 'n', required_argument, NULL, 0, eArgTypeProcessName, "The name of the process to attach to."},
818{ LLDB_OPT_SET_2, false, "waitfor",'w', no_argument, NULL, 0, eArgTypeNone, "Wait for the the process with <process-name> to launch."},
819{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000820};
821
822//-------------------------------------------------------------------------
823// CommandObjectProcessContinue
824//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000825#pragma mark CommandObjectProcessContinue
Chris Lattner24943d22010-06-08 16:52:24 +0000826
827class CommandObjectProcessContinue : public CommandObject
828{
829public:
830
Greg Clayton238c0a12010-09-18 01:14:36 +0000831 CommandObjectProcessContinue (CommandInterpreter &interpreter) :
832 CommandObject (interpreter,
833 "process continue",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000834 "Continue execution of all threads in the current process.",
Chris Lattner24943d22010-06-08 16:52:24 +0000835 "process continue",
836 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
837 {
838 }
839
840
841 ~CommandObjectProcessContinue ()
842 {
843 }
844
845 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000846 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000847 CommandReturnObject &result)
848 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000849 Process *process = m_interpreter.GetExecutionContext().process;
Greg Clayton238c0a12010-09-18 01:14:36 +0000850 bool synchronous_execution = m_interpreter.GetSynchronous ();
Chris Lattner24943d22010-06-08 16:52:24 +0000851
852 if (process == NULL)
853 {
854 result.AppendError ("no process to continue");
855 result.SetStatus (eReturnStatusFailed);
856 return false;
857 }
858
859 StateType state = process->GetState();
860 if (state == eStateStopped)
861 {
862 if (command.GetArgumentCount() != 0)
863 {
864 result.AppendErrorWithFormat ("The '%s' command does not take any arguments.\n", m_cmd_name.c_str());
865 result.SetStatus (eReturnStatusFailed);
866 return false;
867 }
868
869 const uint32_t num_threads = process->GetThreadList().GetSize();
870
871 // Set the actions that the threads should each take when resuming
872 for (uint32_t idx=0; idx<num_threads; ++idx)
873 {
874 process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState (eStateRunning);
875 }
876
877 Error error(process->Resume());
878 if (error.Success())
879 {
Greg Claytonc1d37752010-10-18 01:45:30 +0000880 result.AppendMessageWithFormat ("Process %i resuming\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000881 if (synchronous_execution)
882 {
Greg Claytonbef15832010-07-14 00:18:15 +0000883 state = process->WaitForProcessToStop (NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000884
885 result.SetDidChangeProcessState (true);
886 result.AppendMessageWithFormat ("Process %i %s\n", process->GetID(), StateAsCString (state));
887 result.SetStatus (eReturnStatusSuccessFinishNoResult);
888 }
889 else
890 {
891 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
892 }
893 }
894 else
895 {
896 result.AppendErrorWithFormat("Failed to resume process: %s.\n", error.AsCString());
897 result.SetStatus (eReturnStatusFailed);
898 }
899 }
900 else
901 {
902 result.AppendErrorWithFormat ("Process cannot be continued from its current state (%s).\n",
903 StateAsCString(state));
904 result.SetStatus (eReturnStatusFailed);
905 }
906 return result.Succeeded();
907 }
908};
909
910//-------------------------------------------------------------------------
911// CommandObjectProcessDetach
912//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000913#pragma mark CommandObjectProcessDetach
Chris Lattner24943d22010-06-08 16:52:24 +0000914
915class CommandObjectProcessDetach : public CommandObject
916{
917public:
918
Greg Clayton238c0a12010-09-18 01:14:36 +0000919 CommandObjectProcessDetach (CommandInterpreter &interpreter) :
920 CommandObject (interpreter,
921 "process detach",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000922 "Detach from the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +0000923 "process detach",
924 eFlagProcessMustBeLaunched)
925 {
926 }
927
928 ~CommandObjectProcessDetach ()
929 {
930 }
931
932 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000933 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000934 CommandReturnObject &result)
935 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000936 Process *process = m_interpreter.GetExecutionContext().process;
Chris Lattner24943d22010-06-08 16:52:24 +0000937 if (process == NULL)
938 {
939 result.AppendError ("must have a valid process in order to detach");
940 result.SetStatus (eReturnStatusFailed);
941 return false;
942 }
943
Caroline Tice90b42252010-11-02 16:16:53 +0000944 result.AppendMessageWithFormat ("Detaching from process %i\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000945 Error error (process->Detach());
946 if (error.Success())
947 {
948 result.SetStatus (eReturnStatusSuccessFinishResult);
949 }
950 else
951 {
952 result.AppendErrorWithFormat ("Detach failed: %s\n", error.AsCString());
953 result.SetStatus (eReturnStatusFailed);
954 return false;
955 }
956 return result.Succeeded();
957 }
958};
959
960//-------------------------------------------------------------------------
Greg Claytone71e2582011-02-04 01:58:07 +0000961// CommandObjectProcessConnect
962//-------------------------------------------------------------------------
963#pragma mark CommandObjectProcessConnect
964
965class CommandObjectProcessConnect : public CommandObject
966{
967public:
968
969 class CommandOptions : public Options
970 {
971 public:
972
Greg Claytonf15996e2011-04-07 22:46:35 +0000973 CommandOptions (CommandInterpreter &interpreter) :
974 Options(interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000975 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000976 // Keep default values of all options in one place: OptionParsingStarting ()
977 OptionParsingStarting ();
Greg Claytone71e2582011-02-04 01:58:07 +0000978 }
979
980 ~CommandOptions ()
981 {
982 }
983
984 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000985 SetOptionValue (uint32_t option_idx, const char *option_arg)
Greg Claytone71e2582011-02-04 01:58:07 +0000986 {
987 Error error;
988 char short_option = (char) m_getopt_table[option_idx].val;
989
990 switch (short_option)
991 {
992 case 'p':
993 plugin_name.assign (option_arg);
994 break;
995
996 default:
997 error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
998 break;
999 }
1000 return error;
1001 }
1002
1003 void
Greg Clayton143fcc32011-04-13 00:18:08 +00001004 OptionParsingStarting ()
Greg Claytone71e2582011-02-04 01:58:07 +00001005 {
Greg Claytone71e2582011-02-04 01:58:07 +00001006 plugin_name.clear();
1007 }
1008
Greg Claytonb3448432011-03-24 21:19:54 +00001009 const OptionDefinition*
Greg Claytone71e2582011-02-04 01:58:07 +00001010 GetDefinitions ()
1011 {
1012 return g_option_table;
1013 }
1014
1015 // Options table: Required for subclasses of Options.
1016
Greg Claytonb3448432011-03-24 21:19:54 +00001017 static OptionDefinition g_option_table[];
Greg Claytone71e2582011-02-04 01:58:07 +00001018
1019 // Instance variables to hold the values for command options.
1020
1021 std::string plugin_name;
1022 };
1023
1024 CommandObjectProcessConnect (CommandInterpreter &interpreter) :
Greg Claytonf15996e2011-04-07 22:46:35 +00001025 CommandObject (interpreter,
1026 "process connect",
1027 "Connect to a remote debug service.",
1028 "process connect <remote-url>",
1029 0),
1030 m_options (interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +00001031 {
1032 }
1033
1034 ~CommandObjectProcessConnect ()
1035 {
1036 }
1037
1038
1039 bool
1040 Execute (Args& command,
1041 CommandReturnObject &result)
1042 {
1043
1044 TargetSP target_sp (m_interpreter.GetDebugger().GetSelectedTarget());
1045 Error error;
Greg Claytonb72d0f02011-04-12 05:54:46 +00001046 Process *process = m_interpreter.GetExecutionContext().process;
Greg Claytone71e2582011-02-04 01:58:07 +00001047 if (process)
1048 {
1049 if (process->IsAlive())
1050 {
1051 result.AppendErrorWithFormat ("Process %u is currently being debugged, kill the process before connecting.\n",
1052 process->GetID());
1053 result.SetStatus (eReturnStatusFailed);
1054 return false;
1055 }
1056 }
1057
1058 if (!target_sp)
1059 {
1060 // If there isn't a current target create one.
1061 FileSpec emptyFileSpec;
1062 ArchSpec emptyArchSpec;
1063
1064 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
1065 emptyFileSpec,
1066 emptyArchSpec,
Greg Claytone71e2582011-02-04 01:58:07 +00001067 false,
1068 target_sp);
1069 if (!target_sp || error.Fail())
1070 {
1071 result.AppendError(error.AsCString("Error creating target"));
1072 result.SetStatus (eReturnStatusFailed);
1073 return false;
1074 }
1075 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target_sp.get());
1076 }
1077
1078 if (command.GetArgumentCount() == 1)
1079 {
1080 const char *plugin_name = NULL;
1081 if (!m_options.plugin_name.empty())
1082 plugin_name = m_options.plugin_name.c_str();
1083
1084 const char *remote_url = command.GetArgumentAtIndex(0);
1085 process = target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name).get();
1086
1087 if (process)
1088 {
1089 error = process->ConnectRemote (remote_url);
1090
1091 if (error.Fail())
1092 {
1093 result.AppendError(error.AsCString("Remote connect failed"));
1094 result.SetStatus (eReturnStatusFailed);
1095 return false;
1096 }
1097 }
1098 else
1099 {
1100 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",
1101 m_cmd_name.c_str(),
1102 m_cmd_syntax.c_str());
1103 result.SetStatus (eReturnStatusFailed);
1104 }
1105 }
1106 else
1107 {
1108 result.AppendErrorWithFormat ("'%s' takes exactly one argument:\nUsage: \n",
1109 m_cmd_name.c_str(),
1110 m_cmd_syntax.c_str());
1111 result.SetStatus (eReturnStatusFailed);
1112 }
1113 return result.Succeeded();
1114 }
1115
1116 Options *
1117 GetOptions ()
1118 {
1119 return &m_options;
1120 }
1121
1122protected:
1123
1124 CommandOptions m_options;
1125};
1126
1127
Greg Claytonb3448432011-03-24 21:19:54 +00001128OptionDefinition
Greg Claytone71e2582011-02-04 01:58:07 +00001129CommandObjectProcessConnect::CommandOptions::g_option_table[] =
1130{
1131 { LLDB_OPT_SET_ALL, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
1132 { 0, false, NULL, 0 , 0, NULL, 0, eArgTypeNone, NULL }
1133};
1134
1135//-------------------------------------------------------------------------
Greg Clayton0baa3942010-11-04 01:54:29 +00001136// CommandObjectProcessLoad
1137//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001138#pragma mark CommandObjectProcessLoad
Greg Clayton0baa3942010-11-04 01:54:29 +00001139
1140class CommandObjectProcessLoad : public CommandObject
1141{
1142public:
1143
1144 CommandObjectProcessLoad (CommandInterpreter &interpreter) :
1145 CommandObject (interpreter,
1146 "process load",
1147 "Load a shared library into the current process.",
1148 "process load <filename> [<filename> ...]",
1149 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
1150 {
1151 }
1152
1153 ~CommandObjectProcessLoad ()
1154 {
1155 }
1156
1157 bool
1158 Execute (Args& command,
1159 CommandReturnObject &result)
1160 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001161 Process *process = m_interpreter.GetExecutionContext().process;
Greg Clayton0baa3942010-11-04 01:54:29 +00001162 if (process == NULL)
1163 {
1164 result.AppendError ("must have a valid process in order to load a shared library");
1165 result.SetStatus (eReturnStatusFailed);
1166 return false;
1167 }
1168
1169 const uint32_t argc = command.GetArgumentCount();
1170
1171 for (uint32_t i=0; i<argc; ++i)
1172 {
1173 Error error;
1174 const char *image_path = command.GetArgumentAtIndex(i);
1175 FileSpec image_spec (image_path, false);
1176 uint32_t image_token = process->LoadImage(image_spec, error);
1177 if (image_token != LLDB_INVALID_IMAGE_TOKEN)
1178 {
1179 result.AppendMessageWithFormat ("Loading \"%s\"...ok\nImage %u loaded.\n", image_path, image_token);
1180 result.SetStatus (eReturnStatusSuccessFinishResult);
1181 }
1182 else
1183 {
1184 result.AppendErrorWithFormat ("failed to load '%s': %s", image_path, error.AsCString());
1185 result.SetStatus (eReturnStatusFailed);
1186 }
1187 }
1188 return result.Succeeded();
1189 }
1190};
1191
1192
1193//-------------------------------------------------------------------------
1194// CommandObjectProcessUnload
1195//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001196#pragma mark CommandObjectProcessUnload
Greg Clayton0baa3942010-11-04 01:54:29 +00001197
1198class CommandObjectProcessUnload : public CommandObject
1199{
1200public:
1201
1202 CommandObjectProcessUnload (CommandInterpreter &interpreter) :
1203 CommandObject (interpreter,
1204 "process unload",
1205 "Unload a shared library from the current process using the index returned by a previous call to \"process load\".",
1206 "process unload <index>",
1207 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
1208 {
1209 }
1210
1211 ~CommandObjectProcessUnload ()
1212 {
1213 }
1214
1215 bool
1216 Execute (Args& command,
1217 CommandReturnObject &result)
1218 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001219 Process *process = m_interpreter.GetExecutionContext().process;
Greg Clayton0baa3942010-11-04 01:54:29 +00001220 if (process == NULL)
1221 {
1222 result.AppendError ("must have a valid process in order to load a shared library");
1223 result.SetStatus (eReturnStatusFailed);
1224 return false;
1225 }
1226
1227 const uint32_t argc = command.GetArgumentCount();
1228
1229 for (uint32_t i=0; i<argc; ++i)
1230 {
1231 const char *image_token_cstr = command.GetArgumentAtIndex(i);
1232 uint32_t image_token = Args::StringToUInt32(image_token_cstr, LLDB_INVALID_IMAGE_TOKEN, 0);
1233 if (image_token == LLDB_INVALID_IMAGE_TOKEN)
1234 {
1235 result.AppendErrorWithFormat ("invalid image index argument '%s'", image_token_cstr);
1236 result.SetStatus (eReturnStatusFailed);
1237 break;
1238 }
1239 else
1240 {
1241 Error error (process->UnloadImage(image_token));
1242 if (error.Success())
1243 {
1244 result.AppendMessageWithFormat ("Unloading shared library with index %u...ok\n", image_token);
1245 result.SetStatus (eReturnStatusSuccessFinishResult);
1246 }
1247 else
1248 {
1249 result.AppendErrorWithFormat ("failed to unload image: %s", error.AsCString());
1250 result.SetStatus (eReturnStatusFailed);
1251 break;
1252 }
1253 }
1254 }
1255 return result.Succeeded();
1256 }
1257};
1258
1259//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001260// CommandObjectProcessSignal
1261//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001262#pragma mark CommandObjectProcessSignal
Chris Lattner24943d22010-06-08 16:52:24 +00001263
1264class CommandObjectProcessSignal : public CommandObject
1265{
1266public:
1267
Greg Clayton238c0a12010-09-18 01:14:36 +00001268 CommandObjectProcessSignal (CommandInterpreter &interpreter) :
1269 CommandObject (interpreter,
1270 "process signal",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001271 "Send a UNIX signal to the current process being debugged.",
Caroline Tice43b014a2010-10-04 22:28:36 +00001272 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001273 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001274 CommandArgumentEntry arg;
1275 CommandArgumentData signal_arg;
1276
1277 // Define the first (and only) variant of this arg.
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001278 signal_arg.arg_type = eArgTypeUnixSignal;
Caroline Tice43b014a2010-10-04 22:28:36 +00001279 signal_arg.arg_repetition = eArgRepeatPlain;
1280
1281 // There is only one variant this argument could be; put it into the argument entry.
1282 arg.push_back (signal_arg);
1283
1284 // Push the data for the first argument into the m_arguments vector.
1285 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001286 }
1287
1288 ~CommandObjectProcessSignal ()
1289 {
1290 }
1291
1292 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001293 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001294 CommandReturnObject &result)
1295 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001296 Process *process = m_interpreter.GetExecutionContext().process;
Chris Lattner24943d22010-06-08 16:52:24 +00001297 if (process == NULL)
1298 {
1299 result.AppendError ("no process to signal");
1300 result.SetStatus (eReturnStatusFailed);
1301 return false;
1302 }
1303
1304 if (command.GetArgumentCount() == 1)
1305 {
Greg Clayton8f6be2a2010-10-09 01:40:57 +00001306 int signo = LLDB_INVALID_SIGNAL_NUMBER;
1307
1308 const char *signal_name = command.GetArgumentAtIndex(0);
1309 if (::isxdigit (signal_name[0]))
1310 signo = Args::StringToSInt32(signal_name, LLDB_INVALID_SIGNAL_NUMBER, 0);
1311 else
1312 signo = process->GetUnixSignals().GetSignalNumberFromName (signal_name);
1313
1314 if (signo == LLDB_INVALID_SIGNAL_NUMBER)
Chris Lattner24943d22010-06-08 16:52:24 +00001315 {
1316 result.AppendErrorWithFormat ("Invalid signal argument '%s'.\n", command.GetArgumentAtIndex(0));
1317 result.SetStatus (eReturnStatusFailed);
1318 }
1319 else
1320 {
1321 Error error (process->Signal (signo));
1322 if (error.Success())
1323 {
1324 result.SetStatus (eReturnStatusSuccessFinishResult);
1325 }
1326 else
1327 {
1328 result.AppendErrorWithFormat ("Failed to send signal %i: %s\n", signo, error.AsCString());
1329 result.SetStatus (eReturnStatusFailed);
1330 }
1331 }
1332 }
1333 else
1334 {
1335 result.AppendErrorWithFormat("'%s' takes exactly one signal number argument:\nUsage: \n", m_cmd_name.c_str(),
1336 m_cmd_syntax.c_str());
1337 result.SetStatus (eReturnStatusFailed);
1338 }
1339 return result.Succeeded();
1340 }
1341};
1342
1343
1344//-------------------------------------------------------------------------
1345// CommandObjectProcessInterrupt
1346//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001347#pragma mark CommandObjectProcessInterrupt
Chris Lattner24943d22010-06-08 16:52:24 +00001348
1349class CommandObjectProcessInterrupt : public CommandObject
1350{
1351public:
1352
1353
Greg Clayton238c0a12010-09-18 01:14:36 +00001354 CommandObjectProcessInterrupt (CommandInterpreter &interpreter) :
1355 CommandObject (interpreter,
1356 "process interrupt",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001357 "Interrupt the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +00001358 "process interrupt",
1359 eFlagProcessMustBeLaunched)
1360 {
1361 }
1362
1363 ~CommandObjectProcessInterrupt ()
1364 {
1365 }
1366
1367 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001368 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001369 CommandReturnObject &result)
1370 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001371 Process *process = m_interpreter.GetExecutionContext().process;
Chris Lattner24943d22010-06-08 16:52:24 +00001372 if (process == NULL)
1373 {
1374 result.AppendError ("no process to halt");
1375 result.SetStatus (eReturnStatusFailed);
1376 return false;
1377 }
1378
1379 if (command.GetArgumentCount() == 0)
1380 {
1381 Error error(process->Halt ());
1382 if (error.Success())
1383 {
1384 result.SetStatus (eReturnStatusSuccessFinishResult);
1385
1386 // Maybe we should add a "SuspendThreadPlans so we
1387 // can halt, and keep in place all the current thread plans.
1388 process->GetThreadList().DiscardThreadPlans();
1389 }
1390 else
1391 {
1392 result.AppendErrorWithFormat ("Failed to halt process: %s\n", error.AsCString());
1393 result.SetStatus (eReturnStatusFailed);
1394 }
1395 }
1396 else
1397 {
1398 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: \n",
1399 m_cmd_name.c_str(),
1400 m_cmd_syntax.c_str());
1401 result.SetStatus (eReturnStatusFailed);
1402 }
1403 return result.Succeeded();
1404 }
1405};
1406
1407//-------------------------------------------------------------------------
1408// CommandObjectProcessKill
1409//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001410#pragma mark CommandObjectProcessKill
Chris Lattner24943d22010-06-08 16:52:24 +00001411
1412class CommandObjectProcessKill : public CommandObject
1413{
1414public:
1415
Greg Clayton238c0a12010-09-18 01:14:36 +00001416 CommandObjectProcessKill (CommandInterpreter &interpreter) :
1417 CommandObject (interpreter,
1418 "process kill",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001419 "Terminate the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +00001420 "process kill",
1421 eFlagProcessMustBeLaunched)
1422 {
1423 }
1424
1425 ~CommandObjectProcessKill ()
1426 {
1427 }
1428
1429 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001430 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001431 CommandReturnObject &result)
1432 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001433 Process *process = m_interpreter.GetExecutionContext().process;
Chris Lattner24943d22010-06-08 16:52:24 +00001434 if (process == NULL)
1435 {
1436 result.AppendError ("no process to kill");
1437 result.SetStatus (eReturnStatusFailed);
1438 return false;
1439 }
1440
1441 if (command.GetArgumentCount() == 0)
1442 {
1443 Error error (process->Destroy());
1444 if (error.Success())
1445 {
1446 result.SetStatus (eReturnStatusSuccessFinishResult);
1447 }
1448 else
1449 {
1450 result.AppendErrorWithFormat ("Failed to kill process: %s\n", error.AsCString());
1451 result.SetStatus (eReturnStatusFailed);
1452 }
1453 }
1454 else
1455 {
1456 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: \n",
1457 m_cmd_name.c_str(),
1458 m_cmd_syntax.c_str());
1459 result.SetStatus (eReturnStatusFailed);
1460 }
1461 return result.Succeeded();
1462 }
1463};
1464
1465//-------------------------------------------------------------------------
Jim Ingham41313fc2010-06-18 01:23:09 +00001466// CommandObjectProcessStatus
1467//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001468#pragma mark CommandObjectProcessStatus
1469
Jim Ingham41313fc2010-06-18 01:23:09 +00001470class CommandObjectProcessStatus : public CommandObject
1471{
1472public:
Greg Clayton238c0a12010-09-18 01:14:36 +00001473 CommandObjectProcessStatus (CommandInterpreter &interpreter) :
1474 CommandObject (interpreter,
1475 "process status",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001476 "Show the current status and location of executing process.",
1477 "process status",
Jim Ingham41313fc2010-06-18 01:23:09 +00001478 0)
1479 {
1480 }
1481
1482 ~CommandObjectProcessStatus()
1483 {
1484 }
1485
1486
1487 bool
1488 Execute
1489 (
1490 Args& command,
Jim Ingham41313fc2010-06-18 01:23:09 +00001491 CommandReturnObject &result
1492 )
1493 {
Jim Ingham2e8cb8a2011-02-19 02:53:09 +00001494 Stream &output_stream = result.GetOutputStream();
Jim Ingham41313fc2010-06-18 01:23:09 +00001495 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Claytonb72d0f02011-04-12 05:54:46 +00001496 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
Jim Ingham41313fc2010-06-18 01:23:09 +00001497 if (exe_ctx.process)
1498 {
1499 const StateType state = exe_ctx.process->GetState();
1500 if (StateIsStoppedState(state))
1501 {
1502 if (state == eStateExited)
1503 {
1504 int exit_status = exe_ctx.process->GetExitStatus();
1505 const char *exit_description = exe_ctx.process->GetExitDescription();
1506 output_stream.Printf ("Process %d exited with status = %i (0x%8.8x) %s\n",
1507 exe_ctx.process->GetID(),
1508 exit_status,
1509 exit_status,
1510 exit_description ? exit_description : "");
1511 }
1512 else
1513 {
Greg Claytona2f74232011-02-24 22:24:29 +00001514 if (state == eStateConnected)
1515 output_stream.Printf ("Connected to remote target.\n");
1516 else
1517 output_stream.Printf ("Process %d %s\n", exe_ctx.process->GetID(), StateAsCString (state));
Jim Ingham41313fc2010-06-18 01:23:09 +00001518 if (exe_ctx.thread == NULL)
1519 exe_ctx.thread = exe_ctx.process->GetThreadList().GetThreadAtIndex(0).get();
1520 if (exe_ctx.thread != NULL)
1521 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001522 DisplayThreadsInfo (m_interpreter, &exe_ctx, result, true, true);
Jim Ingham41313fc2010-06-18 01:23:09 +00001523 }
1524 else
1525 {
1526 result.AppendError ("No valid thread found in current process.");
1527 result.SetStatus (eReturnStatusFailed);
1528 }
1529 }
1530 }
1531 else
1532 {
1533 output_stream.Printf ("Process %d is running.\n",
1534 exe_ctx.process->GetID());
1535 }
1536 }
1537 else
1538 {
1539 result.AppendError ("No current location or status available.");
1540 result.SetStatus (eReturnStatusFailed);
1541 }
1542 return result.Succeeded();
1543 }
1544};
1545
1546//-------------------------------------------------------------------------
Caroline Tice23d6f272010-10-13 20:44:39 +00001547// CommandObjectProcessHandle
1548//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001549#pragma mark CommandObjectProcessHandle
Caroline Tice23d6f272010-10-13 20:44:39 +00001550
1551class CommandObjectProcessHandle : public CommandObject
1552{
1553public:
1554
1555 class CommandOptions : public Options
1556 {
1557 public:
1558
Greg Claytonf15996e2011-04-07 22:46:35 +00001559 CommandOptions (CommandInterpreter &interpreter) :
1560 Options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001561 {
Greg Clayton143fcc32011-04-13 00:18:08 +00001562 OptionParsingStarting ();
Caroline Tice23d6f272010-10-13 20:44:39 +00001563 }
1564
1565 ~CommandOptions ()
1566 {
1567 }
1568
1569 Error
Greg Clayton143fcc32011-04-13 00:18:08 +00001570 SetOptionValue (uint32_t option_idx, const char *option_arg)
Caroline Tice23d6f272010-10-13 20:44:39 +00001571 {
1572 Error error;
1573 char short_option = (char) m_getopt_table[option_idx].val;
1574
1575 switch (short_option)
1576 {
1577 case 's':
1578 stop = option_arg;
1579 break;
1580 case 'n':
1581 notify = option_arg;
1582 break;
1583 case 'p':
1584 pass = option_arg;
1585 break;
1586 default:
1587 error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
1588 break;
1589 }
1590 return error;
1591 }
1592
1593 void
Greg Clayton143fcc32011-04-13 00:18:08 +00001594 OptionParsingStarting ()
Caroline Tice23d6f272010-10-13 20:44:39 +00001595 {
Caroline Tice23d6f272010-10-13 20:44:39 +00001596 stop.clear();
1597 notify.clear();
1598 pass.clear();
1599 }
1600
Greg Claytonb3448432011-03-24 21:19:54 +00001601 const OptionDefinition*
Caroline Tice23d6f272010-10-13 20:44:39 +00001602 GetDefinitions ()
1603 {
1604 return g_option_table;
1605 }
1606
1607 // Options table: Required for subclasses of Options.
1608
Greg Claytonb3448432011-03-24 21:19:54 +00001609 static OptionDefinition g_option_table[];
Caroline Tice23d6f272010-10-13 20:44:39 +00001610
1611 // Instance variables to hold the values for command options.
1612
1613 std::string stop;
1614 std::string notify;
1615 std::string pass;
1616 };
1617
1618
1619 CommandObjectProcessHandle (CommandInterpreter &interpreter) :
1620 CommandObject (interpreter,
1621 "process handle",
Caroline Ticee7471982010-10-14 21:31:13 +00001622 "Show or update what the process and debugger should do with various signals received from the OS.",
Greg Claytonf15996e2011-04-07 22:46:35 +00001623 NULL),
1624 m_options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001625 {
Caroline Ticee7471982010-10-14 21:31:13 +00001626 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 +00001627 CommandArgumentEntry arg;
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001628 CommandArgumentData signal_arg;
Caroline Tice23d6f272010-10-13 20:44:39 +00001629
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001630 signal_arg.arg_type = eArgTypeUnixSignal;
1631 signal_arg.arg_repetition = eArgRepeatStar;
Caroline Tice23d6f272010-10-13 20:44:39 +00001632
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001633 arg.push_back (signal_arg);
Caroline Tice23d6f272010-10-13 20:44:39 +00001634
1635 m_arguments.push_back (arg);
1636 }
1637
1638 ~CommandObjectProcessHandle ()
1639 {
1640 }
1641
1642 Options *
1643 GetOptions ()
1644 {
1645 return &m_options;
1646 }
1647
1648 bool
Caroline Ticee7471982010-10-14 21:31:13 +00001649 VerifyCommandOptionValue (const std::string &option, int &real_value)
Caroline Tice23d6f272010-10-13 20:44:39 +00001650 {
1651 bool okay = true;
1652
Caroline Ticee7471982010-10-14 21:31:13 +00001653 bool success = false;
1654 bool tmp_value = Args::StringToBoolean (option.c_str(), false, &success);
1655
1656 if (success && tmp_value)
1657 real_value = 1;
1658 else if (success && !tmp_value)
1659 real_value = 0;
Caroline Tice23d6f272010-10-13 20:44:39 +00001660 else
1661 {
1662 // If the value isn't 'true' or 'false', it had better be 0 or 1.
Caroline Ticee7471982010-10-14 21:31:13 +00001663 real_value = Args::StringToUInt32 (option.c_str(), 3);
1664 if (real_value != 0 && real_value != 1)
Caroline Tice23d6f272010-10-13 20:44:39 +00001665 okay = false;
1666 }
1667
1668 return okay;
1669 }
1670
Caroline Ticee7471982010-10-14 21:31:13 +00001671 void
1672 PrintSignalHeader (Stream &str)
1673 {
1674 str.Printf ("NAME PASS STOP NOTIFY\n");
1675 str.Printf ("========== ===== ===== ======\n");
1676 }
1677
1678 void
1679 PrintSignal (Stream &str, int32_t signo, const char *sig_name, UnixSignals &signals)
1680 {
1681 bool stop;
1682 bool suppress;
1683 bool notify;
1684
1685 str.Printf ("%-10s ", sig_name);
1686 if (signals.GetSignalInfo (signo, suppress, stop, notify))
1687 {
1688 bool pass = !suppress;
1689 str.Printf ("%s %s %s",
1690 (pass ? "true " : "false"),
1691 (stop ? "true " : "false"),
1692 (notify ? "true " : "false"));
1693 }
1694 str.Printf ("\n");
1695 }
1696
1697 void
1698 PrintSignalInformation (Stream &str, Args &signal_args, int num_valid_signals, UnixSignals &signals)
1699 {
1700 PrintSignalHeader (str);
1701
1702 if (num_valid_signals > 0)
1703 {
1704 size_t num_args = signal_args.GetArgumentCount();
1705 for (size_t i = 0; i < num_args; ++i)
1706 {
1707 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1708 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
1709 PrintSignal (str, signo, signal_args.GetArgumentAtIndex (i), signals);
1710 }
1711 }
1712 else // Print info for ALL signals
1713 {
1714 int32_t signo = signals.GetFirstSignalNumber();
1715 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1716 {
1717 PrintSignal (str, signo, signals.GetSignalAsCString (signo), signals);
1718 signo = signals.GetNextSignalNumber (signo);
1719 }
1720 }
1721 }
1722
Caroline Tice23d6f272010-10-13 20:44:39 +00001723 bool
1724 Execute (Args &signal_args, CommandReturnObject &result)
1725 {
1726 TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
1727
1728 if (!target_sp)
1729 {
1730 result.AppendError ("No current target;"
1731 " cannot handle signals until you have a valid target and process.\n");
1732 result.SetStatus (eReturnStatusFailed);
1733 return false;
1734 }
1735
1736 ProcessSP process_sp = target_sp->GetProcessSP();
1737
1738 if (!process_sp)
1739 {
1740 result.AppendError ("No current process; cannot handle signals until you have a valid process.\n");
1741 result.SetStatus (eReturnStatusFailed);
1742 return false;
1743 }
1744
Caroline Tice23d6f272010-10-13 20:44:39 +00001745 int stop_action = -1; // -1 means leave the current setting alone
Caroline Ticee7471982010-10-14 21:31:13 +00001746 int pass_action = -1; // -1 means leave the current setting alone
Caroline Tice23d6f272010-10-13 20:44:39 +00001747 int notify_action = -1; // -1 means leave the current setting alone
1748
1749 if (! m_options.stop.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001750 && ! VerifyCommandOptionValue (m_options.stop, stop_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001751 {
1752 result.AppendError ("Invalid argument for command option --stop; must be true or false.\n");
1753 result.SetStatus (eReturnStatusFailed);
1754 return false;
1755 }
1756
1757 if (! m_options.notify.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001758 && ! VerifyCommandOptionValue (m_options.notify, notify_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001759 {
1760 result.AppendError ("Invalid argument for command option --notify; must be true or false.\n");
1761 result.SetStatus (eReturnStatusFailed);
1762 return false;
1763 }
1764
1765 if (! m_options.pass.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001766 && ! VerifyCommandOptionValue (m_options.pass, pass_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001767 {
1768 result.AppendError ("Invalid argument for command option --pass; must be true or false.\n");
1769 result.SetStatus (eReturnStatusFailed);
1770 return false;
1771 }
1772
1773 size_t num_args = signal_args.GetArgumentCount();
1774 UnixSignals &signals = process_sp->GetUnixSignals();
1775 int num_signals_set = 0;
1776
Caroline Ticee7471982010-10-14 21:31:13 +00001777 if (num_args > 0)
Caroline Tice23d6f272010-10-13 20:44:39 +00001778 {
Caroline Ticee7471982010-10-14 21:31:13 +00001779 for (size_t i = 0; i < num_args; ++i)
Caroline Tice23d6f272010-10-13 20:44:39 +00001780 {
Caroline Ticee7471982010-10-14 21:31:13 +00001781 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1782 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
Caroline Tice23d6f272010-10-13 20:44:39 +00001783 {
Caroline Ticee7471982010-10-14 21:31:13 +00001784 // Casting the actions as bools here should be okay, because VerifyCommandOptionValue guarantees
1785 // the value is either 0 or 1.
1786 if (stop_action != -1)
1787 signals.SetShouldStop (signo, (bool) stop_action);
1788 if (pass_action != -1)
1789 {
1790 bool suppress = ! ((bool) pass_action);
1791 signals.SetShouldSuppress (signo, suppress);
1792 }
1793 if (notify_action != -1)
1794 signals.SetShouldNotify (signo, (bool) notify_action);
1795 ++num_signals_set;
Caroline Tice23d6f272010-10-13 20:44:39 +00001796 }
Caroline Ticee7471982010-10-14 21:31:13 +00001797 else
1798 {
1799 result.AppendErrorWithFormat ("Invalid signal name '%s'\n", signal_args.GetArgumentAtIndex (i));
1800 }
Caroline Tice23d6f272010-10-13 20:44:39 +00001801 }
1802 }
Caroline Ticee7471982010-10-14 21:31:13 +00001803 else
1804 {
1805 // No signal specified, if any command options were specified, update ALL signals.
1806 if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1))
1807 {
1808 if (m_interpreter.Confirm ("Do you really want to update all the signals?", false))
1809 {
1810 int32_t signo = signals.GetFirstSignalNumber();
1811 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1812 {
1813 if (notify_action != -1)
1814 signals.SetShouldNotify (signo, (bool) notify_action);
1815 if (stop_action != -1)
1816 signals.SetShouldStop (signo, (bool) stop_action);
1817 if (pass_action != -1)
1818 {
1819 bool suppress = ! ((bool) pass_action);
1820 signals.SetShouldSuppress (signo, suppress);
1821 }
1822 signo = signals.GetNextSignalNumber (signo);
1823 }
1824 }
1825 }
1826 }
1827
1828 PrintSignalInformation (result.GetOutputStream(), signal_args, num_signals_set, signals);
Caroline Tice23d6f272010-10-13 20:44:39 +00001829
1830 if (num_signals_set > 0)
1831 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1832 else
1833 result.SetStatus (eReturnStatusFailed);
1834
1835 return result.Succeeded();
1836 }
1837
1838protected:
1839
1840 CommandOptions m_options;
1841};
1842
Greg Claytonb3448432011-03-24 21:19:54 +00001843OptionDefinition
Caroline Tice23d6f272010-10-13 20:44:39 +00001844CommandObjectProcessHandle::CommandOptions::g_option_table[] =
1845{
1846{ 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." },
1847{ 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." },
1848{ LLDB_OPT_SET_1, false, "pass", 'p', required_argument, NULL, 0, eArgTypeBoolean, "Whether or not the signal should be passed to the process." },
1849{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1850};
1851
1852//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001853// CommandObjectMultiwordProcess
1854//-------------------------------------------------------------------------
1855
Greg Clayton63094e02010-06-23 01:19:29 +00001856CommandObjectMultiwordProcess::CommandObjectMultiwordProcess (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00001857 CommandObjectMultiword (interpreter,
1858 "process",
1859 "A set of commands for operating on a process.",
1860 "process <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00001861{
Greg Clayton238c0a12010-09-18 01:14:36 +00001862 LoadSubCommand ("attach", CommandObjectSP (new CommandObjectProcessAttach (interpreter)));
1863 LoadSubCommand ("launch", CommandObjectSP (new CommandObjectProcessLaunch (interpreter)));
1864 LoadSubCommand ("continue", CommandObjectSP (new CommandObjectProcessContinue (interpreter)));
Greg Claytone71e2582011-02-04 01:58:07 +00001865 LoadSubCommand ("connect", CommandObjectSP (new CommandObjectProcessConnect (interpreter)));
Greg Clayton238c0a12010-09-18 01:14:36 +00001866 LoadSubCommand ("detach", CommandObjectSP (new CommandObjectProcessDetach (interpreter)));
Greg Clayton0baa3942010-11-04 01:54:29 +00001867 LoadSubCommand ("load", CommandObjectSP (new CommandObjectProcessLoad (interpreter)));
1868 LoadSubCommand ("unload", CommandObjectSP (new CommandObjectProcessUnload (interpreter)));
Greg Clayton238c0a12010-09-18 01:14:36 +00001869 LoadSubCommand ("signal", CommandObjectSP (new CommandObjectProcessSignal (interpreter)));
Caroline Tice23d6f272010-10-13 20:44:39 +00001870 LoadSubCommand ("handle", CommandObjectSP (new CommandObjectProcessHandle (interpreter)));
Greg Clayton238c0a12010-09-18 01:14:36 +00001871 LoadSubCommand ("status", CommandObjectSP (new CommandObjectProcessStatus (interpreter)));
1872 LoadSubCommand ("interrupt", CommandObjectSP (new CommandObjectProcessInterrupt (interpreter)));
1873 LoadSubCommand ("kill", CommandObjectSP (new CommandObjectProcessKill (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00001874}
1875
1876CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess ()
1877{
1878}
1879