blob: 2f5c1b21a71dba59e2299662cd819dbf3eaf6255 [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"
Greg Claytonabe0fed2011-04-18 08:33:37 +000019#include "lldb/Host/Host.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Interpreter/CommandInterpreter.h"
21#include "lldb/Interpreter/CommandReturnObject.h"
Greg Claytone4b9c1f2011-03-08 22:40:15 +000022#include "lldb/Target/Platform.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Target/Process.h"
24#include "lldb/Target/Target.h"
25#include "lldb/Target/Thread.h"
26
27using namespace lldb;
28using namespace lldb_private;
29
30//-------------------------------------------------------------------------
31// CommandObjectProcessLaunch
32//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +000033#pragma mark CommandObjectProjectLaunch
Chris Lattner24943d22010-06-08 16:52:24 +000034class CommandObjectProcessLaunch : public CommandObject
35{
36public:
37
38 class CommandOptions : public Options
39 {
40 public:
41
Greg Claytonf15996e2011-04-07 22:46:35 +000042 CommandOptions (CommandInterpreter &interpreter) :
43 Options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +000044 {
Greg Clayton143fcc32011-04-13 00:18:08 +000045 // Keep default values of all options in one place: OptionParsingStarting ()
46 OptionParsingStarting ();
Chris Lattner24943d22010-06-08 16:52:24 +000047 }
48
49 ~CommandOptions ()
50 {
51 }
52
53 Error
Greg Clayton143fcc32011-04-13 00:18:08 +000054 SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +000055 {
56 Error error;
57 char short_option = (char) m_getopt_table[option_idx].val;
58
59 switch (short_option)
60 {
Greg Claytonde915be2011-01-23 05:56:20 +000061 case 's': stop_at_entry = true; break;
62 case 'e': stderr_path.assign (option_arg); break;
63 case 'i': stdin_path.assign (option_arg); break;
64 case 'o': stdout_path.assign (option_arg); break;
65 case 'p': plugin_name.assign (option_arg); break;
66 case 'n': no_stdio = true; break;
67 case 'w': working_dir.assign (option_arg); break;
Greg Claytonbb0c91f2010-10-19 23:16:00 +000068 case 't':
69 if (option_arg && option_arg[0])
70 tty_name.assign (option_arg);
71 in_new_tty = true;
72 break;
Chris Lattner24943d22010-06-08 16:52:24 +000073 default:
74 error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
75 break;
76
77 }
78 return error;
79 }
80
81 void
Greg Clayton143fcc32011-04-13 00:18:08 +000082 OptionParsingStarting ()
Chris Lattner24943d22010-06-08 16:52:24 +000083 {
Chris Lattner24943d22010-06-08 16:52:24 +000084 stop_at_entry = false;
Greg Claytonc1d37752010-10-18 01:45:30 +000085 in_new_tty = false;
Greg Claytonbb0c91f2010-10-19 23:16:00 +000086 tty_name.clear();
Chris Lattner24943d22010-06-08 16:52:24 +000087 stdin_path.clear();
88 stdout_path.clear();
89 stderr_path.clear();
90 plugin_name.clear();
Greg Claytonde915be2011-01-23 05:56:20 +000091 working_dir.clear();
Caroline Ticebd666012010-12-03 18:46:09 +000092 no_stdio = false;
Chris Lattner24943d22010-06-08 16:52:24 +000093 }
94
Greg Claytonb3448432011-03-24 21:19:54 +000095 const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +000096 GetDefinitions ()
97 {
98 return g_option_table;
99 }
100
101 // Options table: Required for subclasses of Options.
102
Greg Claytonb3448432011-03-24 21:19:54 +0000103 static OptionDefinition g_option_table[];
Chris Lattner24943d22010-06-08 16:52:24 +0000104
105 // Instance variables to hold the values for command options.
106
107 bool stop_at_entry;
Greg Claytonc1d37752010-10-18 01:45:30 +0000108 bool in_new_tty;
Caroline Ticebd666012010-12-03 18:46:09 +0000109 bool no_stdio;
Greg Claytonbb0c91f2010-10-19 23:16:00 +0000110 std::string tty_name;
Chris Lattner24943d22010-06-08 16:52:24 +0000111 std::string stderr_path;
112 std::string stdin_path;
113 std::string stdout_path;
114 std::string plugin_name;
Greg Claytonde915be2011-01-23 05:56:20 +0000115 std::string working_dir;
Chris Lattner24943d22010-06-08 16:52:24 +0000116
117 };
118
Greg Clayton238c0a12010-09-18 01:14:36 +0000119 CommandObjectProcessLaunch (CommandInterpreter &interpreter) :
120 CommandObject (interpreter,
121 "process launch",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000122 "Launch the executable in the debugger.",
Greg Claytonf15996e2011-04-07 22:46:35 +0000123 NULL),
124 m_options (interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000125 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000126 CommandArgumentEntry arg;
127 CommandArgumentData run_args_arg;
128
129 // Define the first (and only) variant of this arg.
130 run_args_arg.arg_type = eArgTypeRunArgs;
131 run_args_arg.arg_repetition = eArgRepeatOptional;
132
133 // There is only one variant this argument could be; put it into the argument entry.
134 arg.push_back (run_args_arg);
135
136 // Push the data for the first argument into the m_arguments vector.
137 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000138 }
139
140
141 ~CommandObjectProcessLaunch ()
142 {
143 }
144
145 Options *
146 GetOptions ()
147 {
148 return &m_options;
149 }
150
151 bool
Greg Claytond8c62532010-10-07 04:19:01 +0000152 Execute (Args& launch_args, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +0000153 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000154 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000155
156 if (target == NULL)
157 {
Greg Claytone1f50b92011-05-03 22:09:39 +0000158 result.AppendError ("invalid target, create a debug target using the 'target create' command");
Chris Lattner24943d22010-06-08 16:52:24 +0000159 result.SetStatus (eReturnStatusFailed);
160 return false;
161 }
162
163 // If our listener is NULL, users aren't allows to launch
Chris Lattner24943d22010-06-08 16:52:24 +0000164 char filename[PATH_MAX];
Greg Clayton5beb99d2011-08-11 02:48:45 +0000165 const Module *exe_module = target->GetExecutableModulePointer();
Greg Claytona2f74232011-02-24 22:24:29 +0000166
167 if (exe_module == NULL)
168 {
Greg Claytone1f50b92011-05-03 22:09:39 +0000169 result.AppendError ("no file in target, create a debug target using the 'target create' command");
Greg Claytona2f74232011-02-24 22:24:29 +0000170 result.SetStatus (eReturnStatusFailed);
171 return false;
172 }
173
Chris Lattner24943d22010-06-08 16:52:24 +0000174 exe_module->GetFileSpec().GetPath(filename, sizeof(filename));
175
Greg Claytona2f74232011-02-24 22:24:29 +0000176 StateType state = eStateInvalid;
Greg Claytonb72d0f02011-04-12 05:54:46 +0000177 Process *process = m_interpreter.GetExecutionContext().process;
Greg Claytona2f74232011-02-24 22:24:29 +0000178 if (process)
179 {
180 state = process->GetState();
181
182 if (process->IsAlive() && state != eStateConnected)
183 {
184 char message[1024];
185 if (process->GetState() == eStateAttaching)
186 ::strncpy (message, "There is a pending attach, abort it and launch a new process?", sizeof(message));
187 else
188 ::strncpy (message, "There is a running process, kill it and restart?", sizeof(message));
189
190 if (!m_interpreter.Confirm (message, true))
Jim Ingham22dc9722010-12-09 18:58:16 +0000191 {
Greg Claytona2f74232011-02-24 22:24:29 +0000192 result.SetStatus (eReturnStatusFailed);
193 return false;
Jim Ingham22dc9722010-12-09 18:58:16 +0000194 }
195 else
196 {
Greg Claytona2f74232011-02-24 22:24:29 +0000197 Error error (process->Destroy());
198 if (error.Success())
199 {
200 result.SetStatus (eReturnStatusSuccessFinishResult);
201 }
202 else
203 {
204 result.AppendErrorWithFormat ("Failed to kill process: %s\n", error.AsCString());
205 result.SetStatus (eReturnStatusFailed);
206 }
Jim Ingham22dc9722010-12-09 18:58:16 +0000207 }
208 }
Chris Lattner24943d22010-06-08 16:52:24 +0000209 }
Jim Ingham22dc9722010-12-09 18:58:16 +0000210
Greg Claytona2f74232011-02-24 22:24:29 +0000211 if (state != eStateConnected)
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000212 {
Greg Claytona2f74232011-02-24 22:24:29 +0000213 const char *plugin_name;
214 if (!m_options.plugin_name.empty())
215 plugin_name = m_options.plugin_name.c_str();
216 else
217 plugin_name = NULL;
218
219 process = target->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name).get();
220 if (process == NULL)
221 {
222 result.AppendErrorWithFormat ("Failed to find a process plugin for executable.\n");
223 result.SetStatus (eReturnStatusFailed);
224 return false;
225 }
Chris Lattner24943d22010-06-08 16:52:24 +0000226 }
227
Greg Claytona2f74232011-02-24 22:24:29 +0000228
Greg Clayton238c0a12010-09-18 01:14:36 +0000229 // If no launch args were given on the command line, then use any that
230 // might have been set using the "run-args" set variable.
231 if (launch_args.GetArgumentCount() == 0)
232 {
233 if (process->GetRunArguments().GetArgumentCount() > 0)
234 launch_args = process->GetRunArguments();
235 }
236
Greg Claytonc1d37752010-10-18 01:45:30 +0000237 if (m_options.in_new_tty)
238 {
Greg Claytona2f74232011-02-24 22:24:29 +0000239 if (state == eStateConnected)
Greg Claytonc1d37752010-10-18 01:45:30 +0000240 {
Greg Claytona2f74232011-02-24 22:24:29 +0000241 result.AppendWarning("launch in tty option is ignored when launching through a remote connection");
242 m_options.in_new_tty = false;
Greg Claytonc1d37752010-10-18 01:45:30 +0000243 }
244 else
245 {
Greg Claytona2f74232011-02-24 22:24:29 +0000246 char exec_file_path[PATH_MAX];
247 if (exe_module->GetFileSpec().GetPath(exec_file_path, sizeof(exec_file_path)))
248 {
249 launch_args.InsertArgumentAtIndex(0, exec_file_path);
250 }
251 else
252 {
253 result.AppendError("invalid executable");
254 result.SetStatus (eReturnStatusFailed);
255 return false;
256 }
Greg Claytonc1d37752010-10-18 01:45:30 +0000257 }
258 }
259
Greg Clayton238c0a12010-09-18 01:14:36 +0000260 Args environment;
261
262 process->GetEnvironmentAsArgs (environment);
263
264 uint32_t launch_flags = eLaunchFlagNone;
265
266 if (process->GetDisableASLR())
267 launch_flags |= eLaunchFlagDisableASLR;
Greg Claytonb72d0f02011-04-12 05:54:46 +0000268
269 if (m_options.in_new_tty)
270 launch_flags |= eLaunchFlagLaunchInTTY;
271
Caroline Ticebd666012010-12-03 18:46:09 +0000272 if (m_options.no_stdio)
273 launch_flags |= eLaunchFlagDisableSTDIO;
274 else if (!m_options.in_new_tty
275 && m_options.stdin_path.empty()
276 && m_options.stdout_path.empty()
277 && m_options.stderr_path.empty())
278 {
279 // Only use the settings value if the user hasn't specified any options that would override it.
280 if (process->GetDisableSTDIO())
281 launch_flags |= eLaunchFlagDisableSTDIO;
282 }
283
Greg Claytonc1d37752010-10-18 01:45:30 +0000284 const char **inferior_argv = launch_args.GetArgumentCount() ? launch_args.GetConstArgumentVector() : NULL;
285 const char **inferior_envp = environment.GetArgumentCount() ? environment.GetConstArgumentVector() : NULL;
Greg Clayton238c0a12010-09-18 01:14:36 +0000286
Greg Claytonc1d37752010-10-18 01:45:30 +0000287 Error error;
Greg Claytonde915be2011-01-23 05:56:20 +0000288 const char *working_dir = NULL;
289 if (!m_options.working_dir.empty())
290 working_dir = m_options.working_dir.c_str();
Greg Clayton238c0a12010-09-18 01:14:36 +0000291
Greg Claytonb72d0f02011-04-12 05:54:46 +0000292 const char * stdin_path = NULL;
293 const char * stdout_path = NULL;
294 const char * stderr_path = NULL;
295
296 // Were any standard input/output/error paths given on the command line?
297 if (m_options.stdin_path.empty() &&
298 m_options.stdout_path.empty() &&
299 m_options.stderr_path.empty())
Greg Clayton238c0a12010-09-18 01:14:36 +0000300 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000301 // No standard file handles were given on the command line, check
302 // with the process object in case they were give using "set settings"
303 stdin_path = process->GetStandardInputPath();
304 stdout_path = process->GetStandardOutputPath();
305 stderr_path = process->GetStandardErrorPath();
Greg Clayton238c0a12010-09-18 01:14:36 +0000306 }
307 else
308 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000309 stdin_path = m_options.stdin_path.empty() ? NULL : m_options.stdin_path.c_str();
310 stdout_path = m_options.stdout_path.empty() ? NULL : m_options.stdout_path.c_str();
311 stderr_path = m_options.stderr_path.empty() ? NULL : m_options.stderr_path.c_str();
Greg Clayton238c0a12010-09-18 01:14:36 +0000312 }
Greg Claytonb72d0f02011-04-12 05:54:46 +0000313
314 error = process->Launch (inferior_argv,
315 inferior_envp,
316 launch_flags,
317 stdin_path,
318 stdout_path,
319 stderr_path,
320 working_dir);
Greg Clayton238c0a12010-09-18 01:14:36 +0000321
322 if (error.Success())
323 {
Greg Clayton940b1032011-02-23 00:35:02 +0000324 const char *archname = exe_module->GetArchitecture().GetArchitectureName();
Greg Claytonc1d37752010-10-18 01:45:30 +0000325
326 result.AppendMessageWithFormat ("Process %i launched: '%s' (%s)\n", process->GetID(), filename, archname);
Greg Claytond8c62532010-10-07 04:19:01 +0000327 result.SetDidChangeProcessState (true);
Greg Clayton238c0a12010-09-18 01:14:36 +0000328 if (m_options.stop_at_entry == false)
329 {
Greg Claytond8c62532010-10-07 04:19:01 +0000330 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
Greg Clayton238c0a12010-09-18 01:14:36 +0000331 StateType state = process->WaitForProcessToStop (NULL);
332
333 if (state == eStateStopped)
334 {
Greg Claytond8c62532010-10-07 04:19:01 +0000335 error = process->Resume();
336 if (error.Success())
337 {
338 bool synchronous_execution = m_interpreter.GetSynchronous ();
339 if (synchronous_execution)
340 {
341 state = process->WaitForProcessToStop (NULL);
Greg Clayton940b1032011-02-23 00:35:02 +0000342 if (!StateIsStoppedState(state))
Greg Clayton395fc332011-02-15 21:59:32 +0000343 {
344 result.AppendErrorWithFormat ("Process isn't stopped: %s", StateAsCString(state));
345 }
Greg Claytond8c62532010-10-07 04:19:01 +0000346 result.SetDidChangeProcessState (true);
347 result.SetStatus (eReturnStatusSuccessFinishResult);
348 }
349 else
350 {
351 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
352 }
353 }
Greg Clayton395fc332011-02-15 21:59:32 +0000354 else
355 {
356 result.AppendErrorWithFormat ("Process resume at entry point failed: %s", error.AsCString());
357 result.SetStatus (eReturnStatusFailed);
358 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000359 }
Greg Clayton395fc332011-02-15 21:59:32 +0000360 else
361 {
362 result.AppendErrorWithFormat ("Initial process state wasn't stopped: %s", StateAsCString(state));
363 result.SetStatus (eReturnStatusFailed);
364 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000365 }
366 }
Greg Clayton395fc332011-02-15 21:59:32 +0000367 else
368 {
Greg Claytona9eb8272011-07-02 21:07:54 +0000369 result.AppendErrorWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton395fc332011-02-15 21:59:32 +0000370 result.SetStatus (eReturnStatusFailed);
371 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000372
Chris Lattner24943d22010-06-08 16:52:24 +0000373 return result.Succeeded();
374 }
375
Jim Ingham767af882010-07-07 03:36:20 +0000376 virtual const char *GetRepeatCommand (Args &current_command_args, uint32_t index)
377 {
378 // No repeat for "process launch"...
379 return "";
380 }
381
Chris Lattner24943d22010-06-08 16:52:24 +0000382protected:
383
384 CommandOptions m_options;
385};
386
387
Greg Claytonc1d37752010-10-18 01:45:30 +0000388#define SET1 LLDB_OPT_SET_1
389#define SET2 LLDB_OPT_SET_2
Caroline Ticebd666012010-12-03 18:46:09 +0000390#define SET3 LLDB_OPT_SET_3
Greg Claytonc1d37752010-10-18 01:45:30 +0000391
Greg Claytonb3448432011-03-24 21:19:54 +0000392OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000393CommandObjectProcessLaunch::CommandOptions::g_option_table[] =
394{
Caroline Ticebd666012010-12-03 18:46:09 +0000395{ 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."},
396{ SET1 , false, "stdin", 'i', required_argument, NULL, 0, eArgTypePath, "Redirect stdin for the process to <path>."},
397{ SET1 , false, "stdout", 'o', required_argument, NULL, 0, eArgTypePath, "Redirect stdout for the process to <path>."},
398{ SET1 , false, "stderr", 'e', required_argument, NULL, 0, eArgTypePath, "Redirect stderr for the process to <path>."},
399{ SET1 | SET2 | SET3, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
400{ 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."},
401{ 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 +0000402{ 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 +0000403{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000404};
405
Greg Claytonc1d37752010-10-18 01:45:30 +0000406#undef SET1
407#undef SET2
Caroline Ticebd666012010-12-03 18:46:09 +0000408#undef SET3
Chris Lattner24943d22010-06-08 16:52:24 +0000409
410//-------------------------------------------------------------------------
411// CommandObjectProcessAttach
412//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000413#pragma mark CommandObjectProcessAttach
Chris Lattner24943d22010-06-08 16:52:24 +0000414class CommandObjectProcessAttach : public CommandObject
415{
416public:
417
Chris Lattner24943d22010-06-08 16:52:24 +0000418 class CommandOptions : public Options
419 {
420 public:
421
Greg Claytonf15996e2011-04-07 22:46:35 +0000422 CommandOptions (CommandInterpreter &interpreter) :
423 Options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000424 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000425 // Keep default values of all options in one place: OptionParsingStarting ()
426 OptionParsingStarting ();
Chris Lattner24943d22010-06-08 16:52:24 +0000427 }
428
429 ~CommandOptions ()
430 {
431 }
432
433 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000434 SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +0000435 {
436 Error error;
437 char short_option = (char) m_getopt_table[option_idx].val;
438 bool success = false;
439 switch (short_option)
440 {
441 case 'p':
442 pid = Args::StringToUInt32 (option_arg, LLDB_INVALID_PROCESS_ID, 0, &success);
443 if (!success || pid == LLDB_INVALID_PROCESS_ID)
444 {
445 error.SetErrorStringWithFormat("Invalid process ID '%s'.\n", option_arg);
446 }
447 break;
448
449 case 'P':
450 plugin_name = option_arg;
451 break;
452
453 case 'n':
454 name.assign(option_arg);
455 break;
456
457 case 'w':
458 waitfor = true;
459 break;
460
461 default:
462 error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
463 break;
464 }
465 return error;
466 }
467
468 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000469 OptionParsingStarting ()
Chris Lattner24943d22010-06-08 16:52:24 +0000470 {
Chris Lattner24943d22010-06-08 16:52:24 +0000471 pid = LLDB_INVALID_PROCESS_ID;
472 name.clear();
473 waitfor = false;
474 }
475
Greg Claytonb3448432011-03-24 21:19:54 +0000476 const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +0000477 GetDefinitions ()
478 {
479 return g_option_table;
480 }
481
Jim Ingham7508e732010-08-09 23:31:02 +0000482 virtual bool
Greg Claytonf15996e2011-04-07 22:46:35 +0000483 HandleOptionArgumentCompletion (Args &input,
Jim Ingham7508e732010-08-09 23:31:02 +0000484 int cursor_index,
485 int char_pos,
486 OptionElementVector &opt_element_vector,
487 int opt_element_index,
488 int match_start_point,
489 int max_return_elements,
490 bool &word_complete,
491 StringList &matches)
492 {
493 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
494 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
495
496 // We are only completing the name option for now...
497
Greg Claytonb3448432011-03-24 21:19:54 +0000498 const OptionDefinition *opt_defs = GetDefinitions();
Jim Ingham7508e732010-08-09 23:31:02 +0000499 if (opt_defs[opt_defs_index].short_option == 'n')
500 {
501 // Are we in the name?
502
503 // Look to see if there is a -P argument provided, and if so use that plugin, otherwise
504 // use the default plugin.
Jim Ingham7508e732010-08-09 23:31:02 +0000505
506 const char *partial_name = NULL;
507 partial_name = input.GetArgumentAtIndex(opt_arg_pos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000508
Greg Claytonb72d0f02011-04-12 05:54:46 +0000509 PlatformSP platform_sp (m_interpreter.GetPlatform (true));
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000510 if (platform_sp)
Jim Ingham7508e732010-08-09 23:31:02 +0000511 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000512 ProcessInstanceInfoList process_infos;
513 ProcessInstanceInfoMatch match_info;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000514 if (partial_name)
515 {
516 match_info.GetProcessInfo().SetName(partial_name);
517 match_info.SetNameMatchType(eNameMatchStartsWith);
518 }
519 platform_sp->FindProcesses (match_info, process_infos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000520 const uint32_t num_matches = process_infos.GetSize();
521 if (num_matches > 0)
522 {
523 for (uint32_t i=0; i<num_matches; ++i)
524 {
525 matches.AppendString (process_infos.GetProcessNameAtIndex(i),
526 process_infos.GetProcessNameLengthAtIndex(i));
527 }
528 }
Jim Ingham7508e732010-08-09 23:31:02 +0000529 }
530 }
531
532 return false;
533 }
534
Chris Lattner24943d22010-06-08 16:52:24 +0000535 // Options table: Required for subclasses of Options.
536
Greg Claytonb3448432011-03-24 21:19:54 +0000537 static OptionDefinition g_option_table[];
Chris Lattner24943d22010-06-08 16:52:24 +0000538
539 // Instance variables to hold the values for command options.
540
541 lldb::pid_t pid;
542 std::string plugin_name;
543 std::string name;
544 bool waitfor;
545 };
546
Greg Clayton238c0a12010-09-18 01:14:36 +0000547 CommandObjectProcessAttach (CommandInterpreter &interpreter) :
548 CommandObject (interpreter,
549 "process attach",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000550 "Attach to a process.",
Greg Claytonf15996e2011-04-07 22:46:35 +0000551 "process attach <cmd-options>"),
552 m_options (interpreter)
Jim Ingham7508e732010-08-09 23:31:02 +0000553 {
Jim Ingham7508e732010-08-09 23:31:02 +0000554 }
555
556 ~CommandObjectProcessAttach ()
557 {
558 }
559
560 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000561 Execute (Args& command,
Jim Ingham7508e732010-08-09 23:31:02 +0000562 CommandReturnObject &result)
563 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000564 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Jim Inghamee940e22011-09-15 01:08:57 +0000565 // N.B. The attach should be synchronous. It doesn't help much to get the prompt back between initiating the attach
566 // and the target actually stopping. So even if the interpreter is set to be asynchronous, we wait for the stop
567 // ourselves here.
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000568
Greg Claytonb72d0f02011-04-12 05:54:46 +0000569 Process *process = m_interpreter.GetExecutionContext().process;
Greg Claytona2f74232011-02-24 22:24:29 +0000570 StateType state = eStateInvalid;
Jim Ingham7508e732010-08-09 23:31:02 +0000571 if (process)
572 {
Greg Claytona2f74232011-02-24 22:24:29 +0000573 state = process->GetState();
574 if (process->IsAlive() && state != eStateConnected)
Jim Ingham7508e732010-08-09 23:31:02 +0000575 {
576 result.AppendErrorWithFormat ("Process %u is currently being debugged, kill the process before attaching.\n",
577 process->GetID());
578 result.SetStatus (eReturnStatusFailed);
579 return false;
580 }
581 }
582
583 if (target == NULL)
584 {
585 // If there isn't a current target create one.
586 TargetSP new_target_sp;
587 FileSpec emptyFileSpec;
588 ArchSpec emptyArchSpec;
589 Error error;
590
Greg Clayton238c0a12010-09-18 01:14:36 +0000591 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
592 emptyFileSpec,
593 emptyArchSpec,
Greg Clayton238c0a12010-09-18 01:14:36 +0000594 false,
595 new_target_sp);
Jim Ingham7508e732010-08-09 23:31:02 +0000596 target = new_target_sp.get();
597 if (target == NULL || error.Fail())
598 {
Greg Claytone71e2582011-02-04 01:58:07 +0000599 result.AppendError(error.AsCString("Error creating target"));
Jim Ingham7508e732010-08-09 23:31:02 +0000600 return false;
601 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000602 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target);
Jim Ingham7508e732010-08-09 23:31:02 +0000603 }
604
605 // Record the old executable module, we want to issue a warning if the process of attaching changed the
606 // current executable (like somebody said "file foo" then attached to a PID whose executable was bar.)
607
608 ModuleSP old_exec_module_sp = target->GetExecutableModule();
609 ArchSpec old_arch_spec = target->GetArchitecture();
610
611 if (command.GetArgumentCount())
612 {
613 result.AppendErrorWithFormat("Invalid arguments for '%s'.\nUsage: \n", m_cmd_name.c_str(), m_cmd_syntax.c_str());
614 result.SetStatus (eReturnStatusFailed);
615 }
616 else
617 {
Greg Claytona2f74232011-02-24 22:24:29 +0000618 if (state != eStateConnected)
619 {
620 const char *plugin_name = NULL;
621
622 if (!m_options.plugin_name.empty())
623 plugin_name = m_options.plugin_name.c_str();
Jim Ingham7508e732010-08-09 23:31:02 +0000624
Greg Claytona2f74232011-02-24 22:24:29 +0000625 process = target->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name).get();
626 }
Jim Ingham7508e732010-08-09 23:31:02 +0000627
628 if (process)
629 {
630 Error error;
631 int attach_pid = m_options.pid;
632
Jim Ingham4805a1c2010-09-15 01:34:14 +0000633 const char *wait_name = NULL;
634
635 if (m_options.name.empty())
636 {
637 if (old_exec_module_sp)
638 {
639 wait_name = old_exec_module_sp->GetFileSpec().GetFilename().AsCString();
640 }
641 }
642 else
643 {
644 wait_name = m_options.name.c_str();
645 }
646
Jim Ingham7508e732010-08-09 23:31:02 +0000647 // If we are waiting for a process with this name to show up, do that first.
648 if (m_options.waitfor)
649 {
Jim Ingham4805a1c2010-09-15 01:34:14 +0000650
651 if (wait_name == NULL)
Jim Ingham7508e732010-08-09 23:31:02 +0000652 {
Jim Ingham4805a1c2010-09-15 01:34:14 +0000653 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 +0000654 result.SetStatus (eReturnStatusFailed);
655 return false;
656 }
Jim Ingham4805a1c2010-09-15 01:34:14 +0000657
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000658 result.AppendMessageWithFormat("Waiting to attach to a process named \"%s\".\n", wait_name);
Jim Ingham4805a1c2010-09-15 01:34:14 +0000659 error = process->Attach (wait_name, m_options.waitfor);
660 if (error.Success())
661 {
662 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
663 }
Jim Ingham7508e732010-08-09 23:31:02 +0000664 else
665 {
Jim Ingham4805a1c2010-09-15 01:34:14 +0000666 result.AppendErrorWithFormat ("Waiting for a process to launch named '%s': %s\n",
667 wait_name,
668 error.AsCString());
669 result.SetStatus (eReturnStatusFailed);
670 return false;
Jim Ingham7508e732010-08-09 23:31:02 +0000671 }
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000672 // If we're synchronous, wait for the stopped event and report that.
673 // Otherwise just return.
674 // FIXME: in the async case it will now be possible to get to the command
675 // interpreter with a state eStateAttaching. Make sure we handle that correctly.
Jim Inghamee940e22011-09-15 01:08:57 +0000676 StateType state = process->WaitForProcessToStop (NULL);
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000677
Jim Inghamee940e22011-09-15 01:08:57 +0000678 result.SetDidChangeProcessState (true);
679 result.AppendMessageWithFormat ("Process %i %s\n", process->GetID(), StateAsCString (state));
680 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Jim Ingham7508e732010-08-09 23:31:02 +0000681 }
682 else
683 {
684 // If the process was specified by name look it up, so we can warn if there are multiple
685 // processes with this pid.
686
Jim Ingham4805a1c2010-09-15 01:34:14 +0000687 if (attach_pid == LLDB_INVALID_PROCESS_ID && wait_name != NULL)
Jim Ingham7508e732010-08-09 23:31:02 +0000688 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000689 ProcessInstanceInfoList process_infos;
690 PlatformSP platform_sp (m_interpreter.GetPlatform (true));
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000691 if (platform_sp)
692 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000693 ProcessInstanceInfoMatch match_info (wait_name, eNameMatchEquals);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000694 platform_sp->FindProcesses (match_info, process_infos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000695 }
696 if (process_infos.GetSize() > 1)
Jim Ingham7508e732010-08-09 23:31:02 +0000697 {
Jim Ingham4805a1c2010-09-15 01:34:14 +0000698 result.AppendErrorWithFormat("More than one process named %s\n", wait_name);
Jim Ingham7508e732010-08-09 23:31:02 +0000699 result.SetStatus (eReturnStatusFailed);
700 return false;
701 }
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000702 else if (process_infos.GetSize() == 0)
Jim Ingham7508e732010-08-09 23:31:02 +0000703 {
Jim Ingham4805a1c2010-09-15 01:34:14 +0000704 result.AppendErrorWithFormat("Could not find a process named %s\n", wait_name);
Jim Ingham7508e732010-08-09 23:31:02 +0000705 result.SetStatus (eReturnStatusFailed);
706 return false;
707 }
708 else
709 {
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000710 attach_pid = process_infos.GetProcessIDAtIndex (0);
Jim Ingham7508e732010-08-09 23:31:02 +0000711 }
Jim Ingham7508e732010-08-09 23:31:02 +0000712 }
713
714 if (attach_pid != LLDB_INVALID_PROCESS_ID)
715 {
716 error = process->Attach (attach_pid);
717 if (error.Success())
718 {
719 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
720 }
721 else
722 {
723 result.AppendErrorWithFormat ("Attaching to process %i failed: %s.\n",
724 attach_pid,
725 error.AsCString());
726 result.SetStatus (eReturnStatusFailed);
727 }
Jim Inghamee940e22011-09-15 01:08:57 +0000728 StateType state = process->WaitForProcessToStop (NULL);
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000729
Jim Inghamee940e22011-09-15 01:08:57 +0000730 result.SetDidChangeProcessState (true);
731 result.AppendMessageWithFormat ("Process %i %s\n", process->GetID(), StateAsCString (state));
732 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Jim Ingham7508e732010-08-09 23:31:02 +0000733 }
734 else
735 {
736 result.AppendErrorWithFormat ("No PID specified for attach\n",
737 attach_pid,
738 error.AsCString());
739 result.SetStatus (eReturnStatusFailed);
740
741 }
742 }
743 }
744 }
745
746 if (result.Succeeded())
747 {
748 // Okay, we're done. Last step is to warn if the executable module has changed:
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000749 char new_path[PATH_MAX];
Greg Clayton5beb99d2011-08-11 02:48:45 +0000750 ModuleSP new_exec_module_sp (target->GetExecutableModule());
Jim Ingham7508e732010-08-09 23:31:02 +0000751 if (!old_exec_module_sp)
752 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000753 // We might not have a module if we attached to a raw pid...
Greg Clayton5beb99d2011-08-11 02:48:45 +0000754 if (new_exec_module_sp)
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000755 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000756 new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000757 result.AppendMessageWithFormat("Executable module set to \"%s\".\n", new_path);
758 }
Jim Ingham7508e732010-08-09 23:31:02 +0000759 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000760 else if (old_exec_module_sp->GetFileSpec() != new_exec_module_sp->GetFileSpec())
Jim Ingham7508e732010-08-09 23:31:02 +0000761 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000762 char old_path[PATH_MAX];
Jim Ingham7508e732010-08-09 23:31:02 +0000763
Greg Clayton5beb99d2011-08-11 02:48:45 +0000764 old_exec_module_sp->GetFileSpec().GetPath (old_path, PATH_MAX);
765 new_exec_module_sp->GetFileSpec().GetPath (new_path, PATH_MAX);
Jim Ingham7508e732010-08-09 23:31:02 +0000766
767 result.AppendWarningWithFormat("Executable module changed from \"%s\" to \"%s\".\n",
768 old_path, new_path);
769 }
770
771 if (!old_arch_spec.IsValid())
772 {
Greg Clayton940b1032011-02-23 00:35:02 +0000773 result.AppendMessageWithFormat ("Architecture set to: %s.\n", target->GetArchitecture().GetArchitectureName());
Jim Ingham7508e732010-08-09 23:31:02 +0000774 }
775 else if (old_arch_spec != target->GetArchitecture())
776 {
777 result.AppendWarningWithFormat("Architecture changed from %s to %s.\n",
Greg Clayton940b1032011-02-23 00:35:02 +0000778 old_arch_spec.GetArchitectureName(), target->GetArchitecture().GetArchitectureName());
Jim Ingham7508e732010-08-09 23:31:02 +0000779 }
780 }
781 return result.Succeeded();
782 }
783
784 Options *
785 GetOptions ()
786 {
787 return &m_options;
788 }
789
Chris Lattner24943d22010-06-08 16:52:24 +0000790protected:
791
792 CommandOptions m_options;
793};
794
795
Greg Claytonb3448432011-03-24 21:19:54 +0000796OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000797CommandObjectProcessAttach::CommandOptions::g_option_table[] =
798{
Caroline Tice4d6675c2010-10-01 19:59:14 +0000799{ LLDB_OPT_SET_ALL, false, "plugin", 'P', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
800{ LLDB_OPT_SET_1, false, "pid", 'p', required_argument, NULL, 0, eArgTypePid, "The process ID of an existing process to attach to."},
801{ LLDB_OPT_SET_2, false, "name", 'n', required_argument, NULL, 0, eArgTypeProcessName, "The name of the process to attach to."},
802{ LLDB_OPT_SET_2, false, "waitfor",'w', no_argument, NULL, 0, eArgTypeNone, "Wait for the the process with <process-name> to launch."},
803{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000804};
805
806//-------------------------------------------------------------------------
807// CommandObjectProcessContinue
808//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000809#pragma mark CommandObjectProcessContinue
Chris Lattner24943d22010-06-08 16:52:24 +0000810
811class CommandObjectProcessContinue : public CommandObject
812{
813public:
814
Greg Clayton238c0a12010-09-18 01:14:36 +0000815 CommandObjectProcessContinue (CommandInterpreter &interpreter) :
816 CommandObject (interpreter,
817 "process continue",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000818 "Continue execution of all threads in the current process.",
Chris Lattner24943d22010-06-08 16:52:24 +0000819 "process continue",
820 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
821 {
822 }
823
824
825 ~CommandObjectProcessContinue ()
826 {
827 }
828
829 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000830 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000831 CommandReturnObject &result)
832 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000833 Process *process = m_interpreter.GetExecutionContext().process;
Greg Clayton238c0a12010-09-18 01:14:36 +0000834 bool synchronous_execution = m_interpreter.GetSynchronous ();
Chris Lattner24943d22010-06-08 16:52:24 +0000835
836 if (process == NULL)
837 {
838 result.AppendError ("no process to continue");
839 result.SetStatus (eReturnStatusFailed);
840 return false;
841 }
842
843 StateType state = process->GetState();
844 if (state == eStateStopped)
845 {
846 if (command.GetArgumentCount() != 0)
847 {
848 result.AppendErrorWithFormat ("The '%s' command does not take any arguments.\n", m_cmd_name.c_str());
849 result.SetStatus (eReturnStatusFailed);
850 return false;
851 }
852
853 const uint32_t num_threads = process->GetThreadList().GetSize();
854
855 // Set the actions that the threads should each take when resuming
856 for (uint32_t idx=0; idx<num_threads; ++idx)
857 {
858 process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState (eStateRunning);
859 }
860
861 Error error(process->Resume());
862 if (error.Success())
863 {
Greg Claytonc1d37752010-10-18 01:45:30 +0000864 result.AppendMessageWithFormat ("Process %i resuming\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000865 if (synchronous_execution)
866 {
Greg Claytonbef15832010-07-14 00:18:15 +0000867 state = process->WaitForProcessToStop (NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000868
869 result.SetDidChangeProcessState (true);
870 result.AppendMessageWithFormat ("Process %i %s\n", process->GetID(), StateAsCString (state));
871 result.SetStatus (eReturnStatusSuccessFinishNoResult);
872 }
873 else
874 {
875 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
876 }
877 }
878 else
879 {
880 result.AppendErrorWithFormat("Failed to resume process: %s.\n", error.AsCString());
881 result.SetStatus (eReturnStatusFailed);
882 }
883 }
884 else
885 {
886 result.AppendErrorWithFormat ("Process cannot be continued from its current state (%s).\n",
887 StateAsCString(state));
888 result.SetStatus (eReturnStatusFailed);
889 }
890 return result.Succeeded();
891 }
892};
893
894//-------------------------------------------------------------------------
895// CommandObjectProcessDetach
896//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000897#pragma mark CommandObjectProcessDetach
Chris Lattner24943d22010-06-08 16:52:24 +0000898
899class CommandObjectProcessDetach : public CommandObject
900{
901public:
902
Greg Clayton238c0a12010-09-18 01:14:36 +0000903 CommandObjectProcessDetach (CommandInterpreter &interpreter) :
904 CommandObject (interpreter,
905 "process detach",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000906 "Detach from the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +0000907 "process detach",
908 eFlagProcessMustBeLaunched)
909 {
910 }
911
912 ~CommandObjectProcessDetach ()
913 {
914 }
915
916 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000917 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000918 CommandReturnObject &result)
919 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000920 Process *process = m_interpreter.GetExecutionContext().process;
Chris Lattner24943d22010-06-08 16:52:24 +0000921 if (process == NULL)
922 {
923 result.AppendError ("must have a valid process in order to detach");
924 result.SetStatus (eReturnStatusFailed);
925 return false;
926 }
927
Caroline Tice90b42252010-11-02 16:16:53 +0000928 result.AppendMessageWithFormat ("Detaching from process %i\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000929 Error error (process->Detach());
930 if (error.Success())
931 {
932 result.SetStatus (eReturnStatusSuccessFinishResult);
933 }
934 else
935 {
936 result.AppendErrorWithFormat ("Detach failed: %s\n", error.AsCString());
937 result.SetStatus (eReturnStatusFailed);
938 return false;
939 }
940 return result.Succeeded();
941 }
942};
943
944//-------------------------------------------------------------------------
Greg Claytone71e2582011-02-04 01:58:07 +0000945// CommandObjectProcessConnect
946//-------------------------------------------------------------------------
947#pragma mark CommandObjectProcessConnect
948
949class CommandObjectProcessConnect : public CommandObject
950{
951public:
952
953 class CommandOptions : public Options
954 {
955 public:
956
Greg Claytonf15996e2011-04-07 22:46:35 +0000957 CommandOptions (CommandInterpreter &interpreter) :
958 Options(interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000959 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000960 // Keep default values of all options in one place: OptionParsingStarting ()
961 OptionParsingStarting ();
Greg Claytone71e2582011-02-04 01:58:07 +0000962 }
963
964 ~CommandOptions ()
965 {
966 }
967
968 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000969 SetOptionValue (uint32_t option_idx, const char *option_arg)
Greg Claytone71e2582011-02-04 01:58:07 +0000970 {
971 Error error;
972 char short_option = (char) m_getopt_table[option_idx].val;
973
974 switch (short_option)
975 {
976 case 'p':
977 plugin_name.assign (option_arg);
978 break;
979
980 default:
981 error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
982 break;
983 }
984 return error;
985 }
986
987 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000988 OptionParsingStarting ()
Greg Claytone71e2582011-02-04 01:58:07 +0000989 {
Greg Claytone71e2582011-02-04 01:58:07 +0000990 plugin_name.clear();
991 }
992
Greg Claytonb3448432011-03-24 21:19:54 +0000993 const OptionDefinition*
Greg Claytone71e2582011-02-04 01:58:07 +0000994 GetDefinitions ()
995 {
996 return g_option_table;
997 }
998
999 // Options table: Required for subclasses of Options.
1000
Greg Claytonb3448432011-03-24 21:19:54 +00001001 static OptionDefinition g_option_table[];
Greg Claytone71e2582011-02-04 01:58:07 +00001002
1003 // Instance variables to hold the values for command options.
1004
1005 std::string plugin_name;
1006 };
1007
1008 CommandObjectProcessConnect (CommandInterpreter &interpreter) :
Greg Claytonf15996e2011-04-07 22:46:35 +00001009 CommandObject (interpreter,
1010 "process connect",
1011 "Connect to a remote debug service.",
1012 "process connect <remote-url>",
1013 0),
1014 m_options (interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +00001015 {
1016 }
1017
1018 ~CommandObjectProcessConnect ()
1019 {
1020 }
1021
1022
1023 bool
1024 Execute (Args& command,
1025 CommandReturnObject &result)
1026 {
1027
1028 TargetSP target_sp (m_interpreter.GetDebugger().GetSelectedTarget());
1029 Error error;
Greg Claytonb72d0f02011-04-12 05:54:46 +00001030 Process *process = m_interpreter.GetExecutionContext().process;
Greg Claytone71e2582011-02-04 01:58:07 +00001031 if (process)
1032 {
1033 if (process->IsAlive())
1034 {
1035 result.AppendErrorWithFormat ("Process %u is currently being debugged, kill the process before connecting.\n",
1036 process->GetID());
1037 result.SetStatus (eReturnStatusFailed);
1038 return false;
1039 }
1040 }
1041
1042 if (!target_sp)
1043 {
1044 // If there isn't a current target create one.
1045 FileSpec emptyFileSpec;
1046 ArchSpec emptyArchSpec;
1047
1048 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
1049 emptyFileSpec,
1050 emptyArchSpec,
Greg Claytone71e2582011-02-04 01:58:07 +00001051 false,
1052 target_sp);
1053 if (!target_sp || error.Fail())
1054 {
1055 result.AppendError(error.AsCString("Error creating target"));
1056 result.SetStatus (eReturnStatusFailed);
1057 return false;
1058 }
1059 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target_sp.get());
1060 }
1061
1062 if (command.GetArgumentCount() == 1)
1063 {
1064 const char *plugin_name = NULL;
1065 if (!m_options.plugin_name.empty())
1066 plugin_name = m_options.plugin_name.c_str();
1067
1068 const char *remote_url = command.GetArgumentAtIndex(0);
1069 process = target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name).get();
1070
1071 if (process)
1072 {
1073 error = process->ConnectRemote (remote_url);
1074
1075 if (error.Fail())
1076 {
1077 result.AppendError(error.AsCString("Remote connect failed"));
1078 result.SetStatus (eReturnStatusFailed);
1079 return false;
1080 }
1081 }
1082 else
1083 {
1084 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",
1085 m_cmd_name.c_str(),
1086 m_cmd_syntax.c_str());
1087 result.SetStatus (eReturnStatusFailed);
1088 }
1089 }
1090 else
1091 {
1092 result.AppendErrorWithFormat ("'%s' takes exactly one argument:\nUsage: \n",
1093 m_cmd_name.c_str(),
1094 m_cmd_syntax.c_str());
1095 result.SetStatus (eReturnStatusFailed);
1096 }
1097 return result.Succeeded();
1098 }
1099
1100 Options *
1101 GetOptions ()
1102 {
1103 return &m_options;
1104 }
1105
1106protected:
1107
1108 CommandOptions m_options;
1109};
1110
1111
Greg Claytonb3448432011-03-24 21:19:54 +00001112OptionDefinition
Greg Claytone71e2582011-02-04 01:58:07 +00001113CommandObjectProcessConnect::CommandOptions::g_option_table[] =
1114{
1115 { LLDB_OPT_SET_ALL, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
1116 { 0, false, NULL, 0 , 0, NULL, 0, eArgTypeNone, NULL }
1117};
1118
1119//-------------------------------------------------------------------------
Greg Clayton0baa3942010-11-04 01:54:29 +00001120// CommandObjectProcessLoad
1121//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001122#pragma mark CommandObjectProcessLoad
Greg Clayton0baa3942010-11-04 01:54:29 +00001123
1124class CommandObjectProcessLoad : public CommandObject
1125{
1126public:
1127
1128 CommandObjectProcessLoad (CommandInterpreter &interpreter) :
1129 CommandObject (interpreter,
1130 "process load",
1131 "Load a shared library into the current process.",
1132 "process load <filename> [<filename> ...]",
1133 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
1134 {
1135 }
1136
1137 ~CommandObjectProcessLoad ()
1138 {
1139 }
1140
1141 bool
1142 Execute (Args& command,
1143 CommandReturnObject &result)
1144 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001145 Process *process = m_interpreter.GetExecutionContext().process;
Greg Clayton0baa3942010-11-04 01:54:29 +00001146 if (process == NULL)
1147 {
1148 result.AppendError ("must have a valid process in order to load a shared library");
1149 result.SetStatus (eReturnStatusFailed);
1150 return false;
1151 }
1152
1153 const uint32_t argc = command.GetArgumentCount();
1154
1155 for (uint32_t i=0; i<argc; ++i)
1156 {
1157 Error error;
1158 const char *image_path = command.GetArgumentAtIndex(i);
1159 FileSpec image_spec (image_path, false);
Greg Claytonf2bf8702011-08-11 16:25:18 +00001160 process->GetTarget().GetPlatform()->ResolveRemotePath(image_spec, image_spec);
Greg Clayton0baa3942010-11-04 01:54:29 +00001161 uint32_t image_token = process->LoadImage(image_spec, error);
1162 if (image_token != LLDB_INVALID_IMAGE_TOKEN)
1163 {
1164 result.AppendMessageWithFormat ("Loading \"%s\"...ok\nImage %u loaded.\n", image_path, image_token);
1165 result.SetStatus (eReturnStatusSuccessFinishResult);
1166 }
1167 else
1168 {
1169 result.AppendErrorWithFormat ("failed to load '%s': %s", image_path, error.AsCString());
1170 result.SetStatus (eReturnStatusFailed);
1171 }
1172 }
1173 return result.Succeeded();
1174 }
1175};
1176
1177
1178//-------------------------------------------------------------------------
1179// CommandObjectProcessUnload
1180//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001181#pragma mark CommandObjectProcessUnload
Greg Clayton0baa3942010-11-04 01:54:29 +00001182
1183class CommandObjectProcessUnload : public CommandObject
1184{
1185public:
1186
1187 CommandObjectProcessUnload (CommandInterpreter &interpreter) :
1188 CommandObject (interpreter,
1189 "process unload",
1190 "Unload a shared library from the current process using the index returned by a previous call to \"process load\".",
1191 "process unload <index>",
1192 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
1193 {
1194 }
1195
1196 ~CommandObjectProcessUnload ()
1197 {
1198 }
1199
1200 bool
1201 Execute (Args& command,
1202 CommandReturnObject &result)
1203 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001204 Process *process = m_interpreter.GetExecutionContext().process;
Greg Clayton0baa3942010-11-04 01:54:29 +00001205 if (process == NULL)
1206 {
1207 result.AppendError ("must have a valid process in order to load a shared library");
1208 result.SetStatus (eReturnStatusFailed);
1209 return false;
1210 }
1211
1212 const uint32_t argc = command.GetArgumentCount();
1213
1214 for (uint32_t i=0; i<argc; ++i)
1215 {
1216 const char *image_token_cstr = command.GetArgumentAtIndex(i);
1217 uint32_t image_token = Args::StringToUInt32(image_token_cstr, LLDB_INVALID_IMAGE_TOKEN, 0);
1218 if (image_token == LLDB_INVALID_IMAGE_TOKEN)
1219 {
1220 result.AppendErrorWithFormat ("invalid image index argument '%s'", image_token_cstr);
1221 result.SetStatus (eReturnStatusFailed);
1222 break;
1223 }
1224 else
1225 {
1226 Error error (process->UnloadImage(image_token));
1227 if (error.Success())
1228 {
1229 result.AppendMessageWithFormat ("Unloading shared library with index %u...ok\n", image_token);
1230 result.SetStatus (eReturnStatusSuccessFinishResult);
1231 }
1232 else
1233 {
1234 result.AppendErrorWithFormat ("failed to unload image: %s", error.AsCString());
1235 result.SetStatus (eReturnStatusFailed);
1236 break;
1237 }
1238 }
1239 }
1240 return result.Succeeded();
1241 }
1242};
1243
1244//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001245// CommandObjectProcessSignal
1246//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001247#pragma mark CommandObjectProcessSignal
Chris Lattner24943d22010-06-08 16:52:24 +00001248
1249class CommandObjectProcessSignal : public CommandObject
1250{
1251public:
1252
Greg Clayton238c0a12010-09-18 01:14:36 +00001253 CommandObjectProcessSignal (CommandInterpreter &interpreter) :
1254 CommandObject (interpreter,
1255 "process signal",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001256 "Send a UNIX signal to the current process being debugged.",
Caroline Tice43b014a2010-10-04 22:28:36 +00001257 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001258 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001259 CommandArgumentEntry arg;
1260 CommandArgumentData signal_arg;
1261
1262 // Define the first (and only) variant of this arg.
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001263 signal_arg.arg_type = eArgTypeUnixSignal;
Caroline Tice43b014a2010-10-04 22:28:36 +00001264 signal_arg.arg_repetition = eArgRepeatPlain;
1265
1266 // There is only one variant this argument could be; put it into the argument entry.
1267 arg.push_back (signal_arg);
1268
1269 // Push the data for the first argument into the m_arguments vector.
1270 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001271 }
1272
1273 ~CommandObjectProcessSignal ()
1274 {
1275 }
1276
1277 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001278 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001279 CommandReturnObject &result)
1280 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001281 Process *process = m_interpreter.GetExecutionContext().process;
Chris Lattner24943d22010-06-08 16:52:24 +00001282 if (process == NULL)
1283 {
1284 result.AppendError ("no process to signal");
1285 result.SetStatus (eReturnStatusFailed);
1286 return false;
1287 }
1288
1289 if (command.GetArgumentCount() == 1)
1290 {
Greg Clayton8f6be2a2010-10-09 01:40:57 +00001291 int signo = LLDB_INVALID_SIGNAL_NUMBER;
1292
1293 const char *signal_name = command.GetArgumentAtIndex(0);
1294 if (::isxdigit (signal_name[0]))
1295 signo = Args::StringToSInt32(signal_name, LLDB_INVALID_SIGNAL_NUMBER, 0);
1296 else
1297 signo = process->GetUnixSignals().GetSignalNumberFromName (signal_name);
1298
1299 if (signo == LLDB_INVALID_SIGNAL_NUMBER)
Chris Lattner24943d22010-06-08 16:52:24 +00001300 {
1301 result.AppendErrorWithFormat ("Invalid signal argument '%s'.\n", command.GetArgumentAtIndex(0));
1302 result.SetStatus (eReturnStatusFailed);
1303 }
1304 else
1305 {
1306 Error error (process->Signal (signo));
1307 if (error.Success())
1308 {
1309 result.SetStatus (eReturnStatusSuccessFinishResult);
1310 }
1311 else
1312 {
1313 result.AppendErrorWithFormat ("Failed to send signal %i: %s\n", signo, error.AsCString());
1314 result.SetStatus (eReturnStatusFailed);
1315 }
1316 }
1317 }
1318 else
1319 {
1320 result.AppendErrorWithFormat("'%s' takes exactly one signal number argument:\nUsage: \n", m_cmd_name.c_str(),
1321 m_cmd_syntax.c_str());
1322 result.SetStatus (eReturnStatusFailed);
1323 }
1324 return result.Succeeded();
1325 }
1326};
1327
1328
1329//-------------------------------------------------------------------------
1330// CommandObjectProcessInterrupt
1331//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001332#pragma mark CommandObjectProcessInterrupt
Chris Lattner24943d22010-06-08 16:52:24 +00001333
1334class CommandObjectProcessInterrupt : public CommandObject
1335{
1336public:
1337
1338
Greg Clayton238c0a12010-09-18 01:14:36 +00001339 CommandObjectProcessInterrupt (CommandInterpreter &interpreter) :
1340 CommandObject (interpreter,
1341 "process interrupt",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001342 "Interrupt the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +00001343 "process interrupt",
1344 eFlagProcessMustBeLaunched)
1345 {
1346 }
1347
1348 ~CommandObjectProcessInterrupt ()
1349 {
1350 }
1351
1352 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001353 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001354 CommandReturnObject &result)
1355 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001356 Process *process = m_interpreter.GetExecutionContext().process;
Chris Lattner24943d22010-06-08 16:52:24 +00001357 if (process == NULL)
1358 {
1359 result.AppendError ("no process to halt");
1360 result.SetStatus (eReturnStatusFailed);
1361 return false;
1362 }
1363
1364 if (command.GetArgumentCount() == 0)
1365 {
1366 Error error(process->Halt ());
1367 if (error.Success())
1368 {
1369 result.SetStatus (eReturnStatusSuccessFinishResult);
1370
1371 // Maybe we should add a "SuspendThreadPlans so we
1372 // can halt, and keep in place all the current thread plans.
1373 process->GetThreadList().DiscardThreadPlans();
1374 }
1375 else
1376 {
1377 result.AppendErrorWithFormat ("Failed to halt process: %s\n", error.AsCString());
1378 result.SetStatus (eReturnStatusFailed);
1379 }
1380 }
1381 else
1382 {
1383 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: \n",
1384 m_cmd_name.c_str(),
1385 m_cmd_syntax.c_str());
1386 result.SetStatus (eReturnStatusFailed);
1387 }
1388 return result.Succeeded();
1389 }
1390};
1391
1392//-------------------------------------------------------------------------
1393// CommandObjectProcessKill
1394//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001395#pragma mark CommandObjectProcessKill
Chris Lattner24943d22010-06-08 16:52:24 +00001396
1397class CommandObjectProcessKill : public CommandObject
1398{
1399public:
1400
Greg Clayton238c0a12010-09-18 01:14:36 +00001401 CommandObjectProcessKill (CommandInterpreter &interpreter) :
1402 CommandObject (interpreter,
1403 "process kill",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001404 "Terminate the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +00001405 "process kill",
1406 eFlagProcessMustBeLaunched)
1407 {
1408 }
1409
1410 ~CommandObjectProcessKill ()
1411 {
1412 }
1413
1414 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001415 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001416 CommandReturnObject &result)
1417 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001418 Process *process = m_interpreter.GetExecutionContext().process;
Chris Lattner24943d22010-06-08 16:52:24 +00001419 if (process == NULL)
1420 {
1421 result.AppendError ("no process to kill");
1422 result.SetStatus (eReturnStatusFailed);
1423 return false;
1424 }
1425
1426 if (command.GetArgumentCount() == 0)
1427 {
1428 Error error (process->Destroy());
1429 if (error.Success())
1430 {
1431 result.SetStatus (eReturnStatusSuccessFinishResult);
1432 }
1433 else
1434 {
1435 result.AppendErrorWithFormat ("Failed to kill process: %s\n", error.AsCString());
1436 result.SetStatus (eReturnStatusFailed);
1437 }
1438 }
1439 else
1440 {
1441 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: \n",
1442 m_cmd_name.c_str(),
1443 m_cmd_syntax.c_str());
1444 result.SetStatus (eReturnStatusFailed);
1445 }
1446 return result.Succeeded();
1447 }
1448};
1449
1450//-------------------------------------------------------------------------
Jim Ingham41313fc2010-06-18 01:23:09 +00001451// CommandObjectProcessStatus
1452//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001453#pragma mark CommandObjectProcessStatus
1454
Jim Ingham41313fc2010-06-18 01:23:09 +00001455class CommandObjectProcessStatus : public CommandObject
1456{
1457public:
Greg Clayton238c0a12010-09-18 01:14:36 +00001458 CommandObjectProcessStatus (CommandInterpreter &interpreter) :
1459 CommandObject (interpreter,
1460 "process status",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001461 "Show the current status and location of executing process.",
1462 "process status",
Jim Ingham41313fc2010-06-18 01:23:09 +00001463 0)
1464 {
1465 }
1466
1467 ~CommandObjectProcessStatus()
1468 {
1469 }
1470
1471
1472 bool
1473 Execute
1474 (
1475 Args& command,
Jim Ingham41313fc2010-06-18 01:23:09 +00001476 CommandReturnObject &result
1477 )
1478 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001479 Stream &strm = result.GetOutputStream();
Jim Ingham41313fc2010-06-18 01:23:09 +00001480 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Claytonb72d0f02011-04-12 05:54:46 +00001481 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
Jim Ingham41313fc2010-06-18 01:23:09 +00001482 if (exe_ctx.process)
1483 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001484 const bool only_threads_with_stop_reason = true;
1485 const uint32_t start_frame = 0;
1486 const uint32_t num_frames = 1;
1487 const uint32_t num_frames_with_source = 1;
1488 exe_ctx.process->GetStatus(strm);
1489 exe_ctx.process->GetThreadStatus (strm,
1490 only_threads_with_stop_reason,
1491 start_frame,
1492 num_frames,
1493 num_frames_with_source);
1494
Jim Ingham41313fc2010-06-18 01:23:09 +00001495 }
1496 else
1497 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001498 result.AppendError ("No process.");
Jim Ingham41313fc2010-06-18 01:23:09 +00001499 result.SetStatus (eReturnStatusFailed);
1500 }
1501 return result.Succeeded();
1502 }
1503};
1504
1505//-------------------------------------------------------------------------
Caroline Tice23d6f272010-10-13 20:44:39 +00001506// CommandObjectProcessHandle
1507//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001508#pragma mark CommandObjectProcessHandle
Caroline Tice23d6f272010-10-13 20:44:39 +00001509
1510class CommandObjectProcessHandle : public CommandObject
1511{
1512public:
1513
1514 class CommandOptions : public Options
1515 {
1516 public:
1517
Greg Claytonf15996e2011-04-07 22:46:35 +00001518 CommandOptions (CommandInterpreter &interpreter) :
1519 Options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001520 {
Greg Clayton143fcc32011-04-13 00:18:08 +00001521 OptionParsingStarting ();
Caroline Tice23d6f272010-10-13 20:44:39 +00001522 }
1523
1524 ~CommandOptions ()
1525 {
1526 }
1527
1528 Error
Greg Clayton143fcc32011-04-13 00:18:08 +00001529 SetOptionValue (uint32_t option_idx, const char *option_arg)
Caroline Tice23d6f272010-10-13 20:44:39 +00001530 {
1531 Error error;
1532 char short_option = (char) m_getopt_table[option_idx].val;
1533
1534 switch (short_option)
1535 {
1536 case 's':
1537 stop = option_arg;
1538 break;
1539 case 'n':
1540 notify = option_arg;
1541 break;
1542 case 'p':
1543 pass = option_arg;
1544 break;
1545 default:
1546 error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
1547 break;
1548 }
1549 return error;
1550 }
1551
1552 void
Greg Clayton143fcc32011-04-13 00:18:08 +00001553 OptionParsingStarting ()
Caroline Tice23d6f272010-10-13 20:44:39 +00001554 {
Caroline Tice23d6f272010-10-13 20:44:39 +00001555 stop.clear();
1556 notify.clear();
1557 pass.clear();
1558 }
1559
Greg Claytonb3448432011-03-24 21:19:54 +00001560 const OptionDefinition*
Caroline Tice23d6f272010-10-13 20:44:39 +00001561 GetDefinitions ()
1562 {
1563 return g_option_table;
1564 }
1565
1566 // Options table: Required for subclasses of Options.
1567
Greg Claytonb3448432011-03-24 21:19:54 +00001568 static OptionDefinition g_option_table[];
Caroline Tice23d6f272010-10-13 20:44:39 +00001569
1570 // Instance variables to hold the values for command options.
1571
1572 std::string stop;
1573 std::string notify;
1574 std::string pass;
1575 };
1576
1577
1578 CommandObjectProcessHandle (CommandInterpreter &interpreter) :
1579 CommandObject (interpreter,
1580 "process handle",
Caroline Ticee7471982010-10-14 21:31:13 +00001581 "Show or update what the process and debugger should do with various signals received from the OS.",
Greg Claytonf15996e2011-04-07 22:46:35 +00001582 NULL),
1583 m_options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001584 {
Caroline Ticee7471982010-10-14 21:31:13 +00001585 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 +00001586 CommandArgumentEntry arg;
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001587 CommandArgumentData signal_arg;
Caroline Tice23d6f272010-10-13 20:44:39 +00001588
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001589 signal_arg.arg_type = eArgTypeUnixSignal;
1590 signal_arg.arg_repetition = eArgRepeatStar;
Caroline Tice23d6f272010-10-13 20:44:39 +00001591
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001592 arg.push_back (signal_arg);
Caroline Tice23d6f272010-10-13 20:44:39 +00001593
1594 m_arguments.push_back (arg);
1595 }
1596
1597 ~CommandObjectProcessHandle ()
1598 {
1599 }
1600
1601 Options *
1602 GetOptions ()
1603 {
1604 return &m_options;
1605 }
1606
1607 bool
Caroline Ticee7471982010-10-14 21:31:13 +00001608 VerifyCommandOptionValue (const std::string &option, int &real_value)
Caroline Tice23d6f272010-10-13 20:44:39 +00001609 {
1610 bool okay = true;
1611
Caroline Ticee7471982010-10-14 21:31:13 +00001612 bool success = false;
1613 bool tmp_value = Args::StringToBoolean (option.c_str(), false, &success);
1614
1615 if (success && tmp_value)
1616 real_value = 1;
1617 else if (success && !tmp_value)
1618 real_value = 0;
Caroline Tice23d6f272010-10-13 20:44:39 +00001619 else
1620 {
1621 // If the value isn't 'true' or 'false', it had better be 0 or 1.
Caroline Ticee7471982010-10-14 21:31:13 +00001622 real_value = Args::StringToUInt32 (option.c_str(), 3);
1623 if (real_value != 0 && real_value != 1)
Caroline Tice23d6f272010-10-13 20:44:39 +00001624 okay = false;
1625 }
1626
1627 return okay;
1628 }
1629
Caroline Ticee7471982010-10-14 21:31:13 +00001630 void
1631 PrintSignalHeader (Stream &str)
1632 {
1633 str.Printf ("NAME PASS STOP NOTIFY\n");
1634 str.Printf ("========== ===== ===== ======\n");
1635 }
1636
1637 void
1638 PrintSignal (Stream &str, int32_t signo, const char *sig_name, UnixSignals &signals)
1639 {
1640 bool stop;
1641 bool suppress;
1642 bool notify;
1643
1644 str.Printf ("%-10s ", sig_name);
1645 if (signals.GetSignalInfo (signo, suppress, stop, notify))
1646 {
1647 bool pass = !suppress;
1648 str.Printf ("%s %s %s",
1649 (pass ? "true " : "false"),
1650 (stop ? "true " : "false"),
1651 (notify ? "true " : "false"));
1652 }
1653 str.Printf ("\n");
1654 }
1655
1656 void
1657 PrintSignalInformation (Stream &str, Args &signal_args, int num_valid_signals, UnixSignals &signals)
1658 {
1659 PrintSignalHeader (str);
1660
1661 if (num_valid_signals > 0)
1662 {
1663 size_t num_args = signal_args.GetArgumentCount();
1664 for (size_t i = 0; i < num_args; ++i)
1665 {
1666 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1667 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
1668 PrintSignal (str, signo, signal_args.GetArgumentAtIndex (i), signals);
1669 }
1670 }
1671 else // Print info for ALL signals
1672 {
1673 int32_t signo = signals.GetFirstSignalNumber();
1674 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1675 {
1676 PrintSignal (str, signo, signals.GetSignalAsCString (signo), signals);
1677 signo = signals.GetNextSignalNumber (signo);
1678 }
1679 }
1680 }
1681
Caroline Tice23d6f272010-10-13 20:44:39 +00001682 bool
1683 Execute (Args &signal_args, CommandReturnObject &result)
1684 {
1685 TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
1686
1687 if (!target_sp)
1688 {
1689 result.AppendError ("No current target;"
1690 " cannot handle signals until you have a valid target and process.\n");
1691 result.SetStatus (eReturnStatusFailed);
1692 return false;
1693 }
1694
1695 ProcessSP process_sp = target_sp->GetProcessSP();
1696
1697 if (!process_sp)
1698 {
1699 result.AppendError ("No current process; cannot handle signals until you have a valid process.\n");
1700 result.SetStatus (eReturnStatusFailed);
1701 return false;
1702 }
1703
Caroline Tice23d6f272010-10-13 20:44:39 +00001704 int stop_action = -1; // -1 means leave the current setting alone
Caroline Ticee7471982010-10-14 21:31:13 +00001705 int pass_action = -1; // -1 means leave the current setting alone
Caroline Tice23d6f272010-10-13 20:44:39 +00001706 int notify_action = -1; // -1 means leave the current setting alone
1707
1708 if (! m_options.stop.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001709 && ! VerifyCommandOptionValue (m_options.stop, stop_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001710 {
1711 result.AppendError ("Invalid argument for command option --stop; must be true or false.\n");
1712 result.SetStatus (eReturnStatusFailed);
1713 return false;
1714 }
1715
1716 if (! m_options.notify.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001717 && ! VerifyCommandOptionValue (m_options.notify, notify_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001718 {
1719 result.AppendError ("Invalid argument for command option --notify; must be true or false.\n");
1720 result.SetStatus (eReturnStatusFailed);
1721 return false;
1722 }
1723
1724 if (! m_options.pass.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001725 && ! VerifyCommandOptionValue (m_options.pass, pass_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001726 {
1727 result.AppendError ("Invalid argument for command option --pass; must be true or false.\n");
1728 result.SetStatus (eReturnStatusFailed);
1729 return false;
1730 }
1731
1732 size_t num_args = signal_args.GetArgumentCount();
1733 UnixSignals &signals = process_sp->GetUnixSignals();
1734 int num_signals_set = 0;
1735
Caroline Ticee7471982010-10-14 21:31:13 +00001736 if (num_args > 0)
Caroline Tice23d6f272010-10-13 20:44:39 +00001737 {
Caroline Ticee7471982010-10-14 21:31:13 +00001738 for (size_t i = 0; i < num_args; ++i)
Caroline Tice23d6f272010-10-13 20:44:39 +00001739 {
Caroline Ticee7471982010-10-14 21:31:13 +00001740 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1741 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
Caroline Tice23d6f272010-10-13 20:44:39 +00001742 {
Caroline Ticee7471982010-10-14 21:31:13 +00001743 // Casting the actions as bools here should be okay, because VerifyCommandOptionValue guarantees
1744 // the value is either 0 or 1.
1745 if (stop_action != -1)
1746 signals.SetShouldStop (signo, (bool) stop_action);
1747 if (pass_action != -1)
1748 {
1749 bool suppress = ! ((bool) pass_action);
1750 signals.SetShouldSuppress (signo, suppress);
1751 }
1752 if (notify_action != -1)
1753 signals.SetShouldNotify (signo, (bool) notify_action);
1754 ++num_signals_set;
Caroline Tice23d6f272010-10-13 20:44:39 +00001755 }
Caroline Ticee7471982010-10-14 21:31:13 +00001756 else
1757 {
1758 result.AppendErrorWithFormat ("Invalid signal name '%s'\n", signal_args.GetArgumentAtIndex (i));
1759 }
Caroline Tice23d6f272010-10-13 20:44:39 +00001760 }
1761 }
Caroline Ticee7471982010-10-14 21:31:13 +00001762 else
1763 {
1764 // No signal specified, if any command options were specified, update ALL signals.
1765 if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1))
1766 {
1767 if (m_interpreter.Confirm ("Do you really want to update all the signals?", false))
1768 {
1769 int32_t signo = signals.GetFirstSignalNumber();
1770 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1771 {
1772 if (notify_action != -1)
1773 signals.SetShouldNotify (signo, (bool) notify_action);
1774 if (stop_action != -1)
1775 signals.SetShouldStop (signo, (bool) stop_action);
1776 if (pass_action != -1)
1777 {
1778 bool suppress = ! ((bool) pass_action);
1779 signals.SetShouldSuppress (signo, suppress);
1780 }
1781 signo = signals.GetNextSignalNumber (signo);
1782 }
1783 }
1784 }
1785 }
1786
1787 PrintSignalInformation (result.GetOutputStream(), signal_args, num_signals_set, signals);
Caroline Tice23d6f272010-10-13 20:44:39 +00001788
1789 if (num_signals_set > 0)
1790 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1791 else
1792 result.SetStatus (eReturnStatusFailed);
1793
1794 return result.Succeeded();
1795 }
1796
1797protected:
1798
1799 CommandOptions m_options;
1800};
1801
Greg Claytonb3448432011-03-24 21:19:54 +00001802OptionDefinition
Caroline Tice23d6f272010-10-13 20:44:39 +00001803CommandObjectProcessHandle::CommandOptions::g_option_table[] =
1804{
1805{ 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." },
1806{ 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." },
1807{ LLDB_OPT_SET_1, false, "pass", 'p', required_argument, NULL, 0, eArgTypeBoolean, "Whether or not the signal should be passed to the process." },
1808{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1809};
1810
1811//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001812// CommandObjectMultiwordProcess
1813//-------------------------------------------------------------------------
1814
Greg Clayton63094e02010-06-23 01:19:29 +00001815CommandObjectMultiwordProcess::CommandObjectMultiwordProcess (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00001816 CommandObjectMultiword (interpreter,
1817 "process",
1818 "A set of commands for operating on a process.",
1819 "process <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00001820{
Greg Claytona9eb8272011-07-02 21:07:54 +00001821 LoadSubCommand ("attach", CommandObjectSP (new CommandObjectProcessAttach (interpreter)));
1822 LoadSubCommand ("launch", CommandObjectSP (new CommandObjectProcessLaunch (interpreter)));
1823 LoadSubCommand ("continue", CommandObjectSP (new CommandObjectProcessContinue (interpreter)));
1824 LoadSubCommand ("connect", CommandObjectSP (new CommandObjectProcessConnect (interpreter)));
1825 LoadSubCommand ("detach", CommandObjectSP (new CommandObjectProcessDetach (interpreter)));
1826 LoadSubCommand ("load", CommandObjectSP (new CommandObjectProcessLoad (interpreter)));
1827 LoadSubCommand ("unload", CommandObjectSP (new CommandObjectProcessUnload (interpreter)));
1828 LoadSubCommand ("signal", CommandObjectSP (new CommandObjectProcessSignal (interpreter)));
1829 LoadSubCommand ("handle", CommandObjectSP (new CommandObjectProcessHandle (interpreter)));
1830 LoadSubCommand ("status", CommandObjectSP (new CommandObjectProcessStatus (interpreter)));
Greg Clayton238c0a12010-09-18 01:14:36 +00001831 LoadSubCommand ("interrupt", CommandObjectSP (new CommandObjectProcessInterrupt (interpreter)));
Greg Claytona9eb8272011-07-02 21:07:54 +00001832 LoadSubCommand ("kill", CommandObjectSP (new CommandObjectProcessKill (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00001833}
1834
1835CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess ()
1836{
1837}
1838