blob: 0dbc9b82873b10ae7bb69de011e4e418ae85b8e9 [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
43 CommandOptions () :
44 Options()
45 {
46 // Keep default values of all options in one place: ResetOptionValues ()
47 ResetOptionValues ();
48 }
49
50 ~CommandOptions ()
51 {
52 }
53
54 Error
55 SetOptionValue (int option_idx, const char *option_arg)
56 {
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
83 ResetOptionValues ()
84 {
85 Options::ResetOptionValues();
86 stop_at_entry = false;
Greg Claytonc1d37752010-10-18 01:45:30 +000087 in_new_tty = false;
Greg Claytonbb0c91f2010-10-19 23:16:00 +000088 tty_name.clear();
Chris Lattner24943d22010-06-08 16:52:24 +000089 stdin_path.clear();
90 stdout_path.clear();
91 stderr_path.clear();
92 plugin_name.clear();
Greg Claytonde915be2011-01-23 05:56:20 +000093 working_dir.clear();
Caroline Ticebd666012010-12-03 18:46:09 +000094 no_stdio = false;
Chris Lattner24943d22010-06-08 16:52:24 +000095 }
96
97 const lldb::OptionDefinition*
98 GetDefinitions ()
99 {
100 return g_option_table;
101 }
102
103 // Options table: Required for subclasses of Options.
104
105 static lldb::OptionDefinition g_option_table[];
106
107 // Instance variables to hold the values for command options.
108
109 bool stop_at_entry;
Greg Claytonc1d37752010-10-18 01:45:30 +0000110 bool in_new_tty;
Caroline Ticebd666012010-12-03 18:46:09 +0000111 bool no_stdio;
Greg Claytonbb0c91f2010-10-19 23:16:00 +0000112 std::string tty_name;
Chris Lattner24943d22010-06-08 16:52:24 +0000113 std::string stderr_path;
114 std::string stdin_path;
115 std::string stdout_path;
116 std::string plugin_name;
Greg Claytonde915be2011-01-23 05:56:20 +0000117 std::string working_dir;
Chris Lattner24943d22010-06-08 16:52:24 +0000118
119 };
120
Greg Clayton238c0a12010-09-18 01:14:36 +0000121 CommandObjectProcessLaunch (CommandInterpreter &interpreter) :
122 CommandObject (interpreter,
123 "process launch",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000124 "Launch the executable in the debugger.",
Caroline Tice43b014a2010-10-04 22:28:36 +0000125 NULL)
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 Clayton238c0a12010-09-18 01:14:36 +0000178 Process *process = m_interpreter.GetDebugger().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 Claytonc1d37752010-10-18 01:45:30 +0000269
Caroline Ticebd666012010-12-03 18:46:09 +0000270 if (m_options.no_stdio)
271 launch_flags |= eLaunchFlagDisableSTDIO;
272 else if (!m_options.in_new_tty
273 && m_options.stdin_path.empty()
274 && m_options.stdout_path.empty()
275 && m_options.stderr_path.empty())
276 {
277 // Only use the settings value if the user hasn't specified any options that would override it.
278 if (process->GetDisableSTDIO())
279 launch_flags |= eLaunchFlagDisableSTDIO;
280 }
281
Greg Claytonc1d37752010-10-18 01:45:30 +0000282 const char **inferior_argv = launch_args.GetArgumentCount() ? launch_args.GetConstArgumentVector() : NULL;
283 const char **inferior_envp = environment.GetArgumentCount() ? environment.GetConstArgumentVector() : NULL;
Greg Clayton238c0a12010-09-18 01:14:36 +0000284
Greg Claytonc1d37752010-10-18 01:45:30 +0000285 Error error;
Greg Claytonde915be2011-01-23 05:56:20 +0000286 const char *working_dir = NULL;
287 if (!m_options.working_dir.empty())
288 working_dir = m_options.working_dir.c_str();
Greg Clayton238c0a12010-09-18 01:14:36 +0000289
Greg Claytonc1d37752010-10-18 01:45:30 +0000290 if (m_options.in_new_tty)
Greg Clayton238c0a12010-09-18 01:14:36 +0000291 {
Greg Claytonc1d37752010-10-18 01:45:30 +0000292
Greg Claytonbb0c91f2010-10-19 23:16:00 +0000293 lldb::pid_t pid = Host::LaunchInNewTerminal (m_options.tty_name.c_str(),
294 inferior_argv,
Greg Clayton36f63a92010-10-19 03:25:40 +0000295 inferior_envp,
Greg Claytonde915be2011-01-23 05:56:20 +0000296 working_dir,
Greg Clayton36f63a92010-10-19 03:25:40 +0000297 &exe_module->GetArchitecture(),
298 true,
299 process->GetDisableASLR());
Greg Claytonc1d37752010-10-18 01:45:30 +0000300
Greg Clayton36f63a92010-10-19 03:25:40 +0000301 if (pid != LLDB_INVALID_PROCESS_ID)
302 error = process->Attach (pid);
Greg Clayton238c0a12010-09-18 01:14:36 +0000303 }
304 else
305 {
Greg Claytonc1d37752010-10-18 01:45:30 +0000306 const char * stdin_path = NULL;
307 const char * stdout_path = NULL;
308 const char * stderr_path = NULL;
309
310 // Were any standard input/output/error paths given on the command line?
311 if (m_options.stdin_path.empty() &&
312 m_options.stdout_path.empty() &&
313 m_options.stderr_path.empty())
314 {
315 // No standard file handles were given on the command line, check
316 // with the process object in case they were give using "set settings"
317 stdin_path = process->GetStandardInputPath();
318 stdout_path = process->GetStandardOutputPath();
319 stderr_path = process->GetStandardErrorPath();
320 }
321 else
322 {
323 stdin_path = m_options.stdin_path.empty() ? NULL : m_options.stdin_path.c_str();
324 stdout_path = m_options.stdout_path.empty() ? NULL : m_options.stdout_path.c_str();
325 stderr_path = m_options.stderr_path.empty() ? NULL : m_options.stderr_path.c_str();
326 }
327
Greg Claytonc1d37752010-10-18 01:45:30 +0000328 error = process->Launch (inferior_argv,
329 inferior_envp,
330 launch_flags,
331 stdin_path,
332 stdout_path,
Greg Claytonde915be2011-01-23 05:56:20 +0000333 stderr_path,
334 working_dir);
Greg Clayton238c0a12010-09-18 01:14:36 +0000335 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000336
337 if (error.Success())
338 {
Greg Clayton940b1032011-02-23 00:35:02 +0000339 const char *archname = exe_module->GetArchitecture().GetArchitectureName();
Greg Claytonc1d37752010-10-18 01:45:30 +0000340
341 result.AppendMessageWithFormat ("Process %i launched: '%s' (%s)\n", process->GetID(), filename, archname);
Greg Claytond8c62532010-10-07 04:19:01 +0000342 result.SetDidChangeProcessState (true);
Greg Clayton238c0a12010-09-18 01:14:36 +0000343 if (m_options.stop_at_entry == false)
344 {
Greg Claytond8c62532010-10-07 04:19:01 +0000345 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
Greg Clayton238c0a12010-09-18 01:14:36 +0000346 StateType state = process->WaitForProcessToStop (NULL);
347
348 if (state == eStateStopped)
349 {
Greg Claytond8c62532010-10-07 04:19:01 +0000350 error = process->Resume();
351 if (error.Success())
352 {
353 bool synchronous_execution = m_interpreter.GetSynchronous ();
354 if (synchronous_execution)
355 {
356 state = process->WaitForProcessToStop (NULL);
Greg Clayton940b1032011-02-23 00:35:02 +0000357 if (!StateIsStoppedState(state))
Greg Clayton395fc332011-02-15 21:59:32 +0000358 {
359 result.AppendErrorWithFormat ("Process isn't stopped: %s", StateAsCString(state));
360 }
Greg Claytond8c62532010-10-07 04:19:01 +0000361 result.SetDidChangeProcessState (true);
362 result.SetStatus (eReturnStatusSuccessFinishResult);
363 }
364 else
365 {
366 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
367 }
368 }
Greg Clayton395fc332011-02-15 21:59:32 +0000369 else
370 {
371 result.AppendErrorWithFormat ("Process resume at entry point failed: %s", error.AsCString());
372 result.SetStatus (eReturnStatusFailed);
373 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000374 }
Greg Clayton395fc332011-02-15 21:59:32 +0000375 else
376 {
377 result.AppendErrorWithFormat ("Initial process state wasn't stopped: %s", StateAsCString(state));
378 result.SetStatus (eReturnStatusFailed);
379 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000380 }
381 }
Greg Clayton395fc332011-02-15 21:59:32 +0000382 else
383 {
384 result.AppendErrorWithFormat ("Process launch failed: %s", error.AsCString());
385 result.SetStatus (eReturnStatusFailed);
386 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000387
Chris Lattner24943d22010-06-08 16:52:24 +0000388 return result.Succeeded();
389 }
390
Jim Ingham767af882010-07-07 03:36:20 +0000391 virtual const char *GetRepeatCommand (Args &current_command_args, uint32_t index)
392 {
393 // No repeat for "process launch"...
394 return "";
395 }
396
Chris Lattner24943d22010-06-08 16:52:24 +0000397protected:
398
399 CommandOptions m_options;
400};
401
402
Greg Claytonc1d37752010-10-18 01:45:30 +0000403#define SET1 LLDB_OPT_SET_1
404#define SET2 LLDB_OPT_SET_2
Caroline Ticebd666012010-12-03 18:46:09 +0000405#define SET3 LLDB_OPT_SET_3
Greg Claytonc1d37752010-10-18 01:45:30 +0000406
Chris Lattner24943d22010-06-08 16:52:24 +0000407lldb::OptionDefinition
408CommandObjectProcessLaunch::CommandOptions::g_option_table[] =
409{
Caroline Ticebd666012010-12-03 18:46:09 +0000410{ 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."},
411{ SET1 , false, "stdin", 'i', required_argument, NULL, 0, eArgTypePath, "Redirect stdin for the process to <path>."},
412{ SET1 , false, "stdout", 'o', required_argument, NULL, 0, eArgTypePath, "Redirect stdout for the process to <path>."},
413{ SET1 , false, "stderr", 'e', required_argument, NULL, 0, eArgTypePath, "Redirect stderr for the process to <path>."},
414{ SET1 | SET2 | SET3, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
415{ 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."},
416{ 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 +0000417{ 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 +0000418{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000419};
420
Greg Claytonc1d37752010-10-18 01:45:30 +0000421#undef SET1
422#undef SET2
Caroline Ticebd666012010-12-03 18:46:09 +0000423#undef SET3
Chris Lattner24943d22010-06-08 16:52:24 +0000424
425//-------------------------------------------------------------------------
426// CommandObjectProcessAttach
427//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000428#pragma mark CommandObjectProcessAttach
Chris Lattner24943d22010-06-08 16:52:24 +0000429class CommandObjectProcessAttach : public CommandObject
430{
431public:
432
Chris Lattner24943d22010-06-08 16:52:24 +0000433 class CommandOptions : public Options
434 {
435 public:
436
437 CommandOptions () :
438 Options()
439 {
440 // Keep default values of all options in one place: ResetOptionValues ()
441 ResetOptionValues ();
442 }
443
444 ~CommandOptions ()
445 {
446 }
447
448 Error
449 SetOptionValue (int option_idx, const char *option_arg)
450 {
451 Error error;
452 char short_option = (char) m_getopt_table[option_idx].val;
453 bool success = false;
454 switch (short_option)
455 {
456 case 'p':
457 pid = Args::StringToUInt32 (option_arg, LLDB_INVALID_PROCESS_ID, 0, &success);
458 if (!success || pid == LLDB_INVALID_PROCESS_ID)
459 {
460 error.SetErrorStringWithFormat("Invalid process ID '%s'.\n", option_arg);
461 }
462 break;
463
464 case 'P':
465 plugin_name = option_arg;
466 break;
467
468 case 'n':
469 name.assign(option_arg);
470 break;
471
472 case 'w':
473 waitfor = true;
474 break;
475
476 default:
477 error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
478 break;
479 }
480 return error;
481 }
482
483 void
484 ResetOptionValues ()
485 {
486 Options::ResetOptionValues();
487 pid = LLDB_INVALID_PROCESS_ID;
488 name.clear();
489 waitfor = false;
490 }
491
492 const lldb::OptionDefinition*
493 GetDefinitions ()
494 {
495 return g_option_table;
496 }
497
Jim Ingham7508e732010-08-09 23:31:02 +0000498 virtual bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000499 HandleOptionArgumentCompletion (CommandInterpreter &interpeter,
Jim Ingham7508e732010-08-09 23:31:02 +0000500 Args &input,
501 int cursor_index,
502 int char_pos,
503 OptionElementVector &opt_element_vector,
504 int opt_element_index,
505 int match_start_point,
506 int max_return_elements,
507 bool &word_complete,
508 StringList &matches)
509 {
510 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
511 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
512
513 // We are only completing the name option for now...
514
515 const lldb::OptionDefinition *opt_defs = GetDefinitions();
516 if (opt_defs[opt_defs_index].short_option == 'n')
517 {
518 // Are we in the name?
519
520 // Look to see if there is a -P argument provided, and if so use that plugin, otherwise
521 // use the default plugin.
Jim Ingham7508e732010-08-09 23:31:02 +0000522
523 const char *partial_name = NULL;
524 partial_name = input.GetArgumentAtIndex(opt_arg_pos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000525
526 PlatformSP platform_sp (Platform::GetSelectedPlatform ());
527 if (platform_sp)
Jim Ingham7508e732010-08-09 23:31:02 +0000528 {
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000529 ProcessInfoList process_infos;
530 platform_sp->FindProcessesByName (partial_name, partial_name ? lldb::eNameMatchStartsWith : lldb::eNameMatchIgnore, process_infos);
531 const uint32_t num_matches = process_infos.GetSize();
532 if (num_matches > 0)
533 {
534 for (uint32_t i=0; i<num_matches; ++i)
535 {
536 matches.AppendString (process_infos.GetProcessNameAtIndex(i),
537 process_infos.GetProcessNameLengthAtIndex(i));
538 }
539 }
Jim Ingham7508e732010-08-09 23:31:02 +0000540 }
541 }
542
543 return false;
544 }
545
Chris Lattner24943d22010-06-08 16:52:24 +0000546 // Options table: Required for subclasses of Options.
547
548 static lldb::OptionDefinition g_option_table[];
549
550 // Instance variables to hold the values for command options.
551
552 lldb::pid_t pid;
553 std::string plugin_name;
554 std::string name;
555 bool waitfor;
556 };
557
Greg Clayton238c0a12010-09-18 01:14:36 +0000558 CommandObjectProcessAttach (CommandInterpreter &interpreter) :
559 CommandObject (interpreter,
560 "process attach",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000561 "Attach to a process.",
Jim Ingham7508e732010-08-09 23:31:02 +0000562 "process attach <cmd-options>")
563 {
Jim Ingham7508e732010-08-09 23:31:02 +0000564 }
565
566 ~CommandObjectProcessAttach ()
567 {
568 }
569
570 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000571 Execute (Args& command,
Jim Ingham7508e732010-08-09 23:31:02 +0000572 CommandReturnObject &result)
573 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000574 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000575 bool synchronous_execution = m_interpreter.GetSynchronous ();
576
Greg Clayton238c0a12010-09-18 01:14:36 +0000577 Process *process = m_interpreter.GetDebugger().GetExecutionContext().process;
Greg Claytona2f74232011-02-24 22:24:29 +0000578 StateType state = eStateInvalid;
Jim Ingham7508e732010-08-09 23:31:02 +0000579 if (process)
580 {
Greg Claytona2f74232011-02-24 22:24:29 +0000581 state = process->GetState();
582 if (process->IsAlive() && state != eStateConnected)
Jim Ingham7508e732010-08-09 23:31:02 +0000583 {
584 result.AppendErrorWithFormat ("Process %u is currently being debugged, kill the process before attaching.\n",
585 process->GetID());
586 result.SetStatus (eReturnStatusFailed);
587 return false;
588 }
589 }
590
591 if (target == NULL)
592 {
593 // If there isn't a current target create one.
594 TargetSP new_target_sp;
595 FileSpec emptyFileSpec;
596 ArchSpec emptyArchSpec;
597 Error error;
598
Greg Clayton238c0a12010-09-18 01:14:36 +0000599 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
600 emptyFileSpec,
601 emptyArchSpec,
Greg Clayton238c0a12010-09-18 01:14:36 +0000602 false,
603 new_target_sp);
Jim Ingham7508e732010-08-09 23:31:02 +0000604 target = new_target_sp.get();
605 if (target == NULL || error.Fail())
606 {
Greg Claytone71e2582011-02-04 01:58:07 +0000607 result.AppendError(error.AsCString("Error creating target"));
Jim Ingham7508e732010-08-09 23:31:02 +0000608 return false;
609 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000610 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target);
Jim Ingham7508e732010-08-09 23:31:02 +0000611 }
612
613 // Record the old executable module, we want to issue a warning if the process of attaching changed the
614 // current executable (like somebody said "file foo" then attached to a PID whose executable was bar.)
615
616 ModuleSP old_exec_module_sp = target->GetExecutableModule();
617 ArchSpec old_arch_spec = target->GetArchitecture();
618
619 if (command.GetArgumentCount())
620 {
621 result.AppendErrorWithFormat("Invalid arguments for '%s'.\nUsage: \n", m_cmd_name.c_str(), m_cmd_syntax.c_str());
622 result.SetStatus (eReturnStatusFailed);
623 }
624 else
625 {
Greg Claytona2f74232011-02-24 22:24:29 +0000626 if (state != eStateConnected)
627 {
628 const char *plugin_name = NULL;
629
630 if (!m_options.plugin_name.empty())
631 plugin_name = m_options.plugin_name.c_str();
Jim Ingham7508e732010-08-09 23:31:02 +0000632
Greg Claytona2f74232011-02-24 22:24:29 +0000633 process = target->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name).get();
634 }
Jim Ingham7508e732010-08-09 23:31:02 +0000635
636 if (process)
637 {
638 Error error;
639 int attach_pid = m_options.pid;
640
Jim Ingham4805a1c2010-09-15 01:34:14 +0000641 const char *wait_name = NULL;
642
643 if (m_options.name.empty())
644 {
645 if (old_exec_module_sp)
646 {
647 wait_name = old_exec_module_sp->GetFileSpec().GetFilename().AsCString();
648 }
649 }
650 else
651 {
652 wait_name = m_options.name.c_str();
653 }
654
Jim Ingham7508e732010-08-09 23:31:02 +0000655 // If we are waiting for a process with this name to show up, do that first.
656 if (m_options.waitfor)
657 {
Jim Ingham4805a1c2010-09-15 01:34:14 +0000658
659 if (wait_name == NULL)
Jim Ingham7508e732010-08-09 23:31:02 +0000660 {
Jim Ingham4805a1c2010-09-15 01:34:14 +0000661 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 +0000662 result.SetStatus (eReturnStatusFailed);
663 return false;
664 }
Jim Ingham4805a1c2010-09-15 01:34:14 +0000665
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000666 result.AppendMessageWithFormat("Waiting to attach to a process named \"%s\".\n", wait_name);
Jim Ingham4805a1c2010-09-15 01:34:14 +0000667 error = process->Attach (wait_name, m_options.waitfor);
668 if (error.Success())
669 {
670 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
671 }
Jim Ingham7508e732010-08-09 23:31:02 +0000672 else
673 {
Jim Ingham4805a1c2010-09-15 01:34:14 +0000674 result.AppendErrorWithFormat ("Waiting for a process to launch named '%s': %s\n",
675 wait_name,
676 error.AsCString());
677 result.SetStatus (eReturnStatusFailed);
678 return false;
Jim Ingham7508e732010-08-09 23:31:02 +0000679 }
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000680 // If we're synchronous, wait for the stopped event and report that.
681 // Otherwise just return.
682 // FIXME: in the async case it will now be possible to get to the command
683 // interpreter with a state eStateAttaching. Make sure we handle that correctly.
684 if (synchronous_execution)
685 {
686 StateType state = process->WaitForProcessToStop (NULL);
687
688 result.SetDidChangeProcessState (true);
689 result.AppendMessageWithFormat ("Process %i %s\n", process->GetID(), StateAsCString (state));
690 result.SetStatus (eReturnStatusSuccessFinishNoResult);
691 }
692 else
693 {
694 result.SetDidChangeProcessState (true);
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000695 result.SetStatus (eReturnStatusSuccessFinishNoResult);
696 }
Jim Ingham7508e732010-08-09 23:31:02 +0000697 }
698 else
699 {
700 // If the process was specified by name look it up, so we can warn if there are multiple
701 // processes with this pid.
702
Jim Ingham4805a1c2010-09-15 01:34:14 +0000703 if (attach_pid == LLDB_INVALID_PROCESS_ID && wait_name != NULL)
Jim Ingham7508e732010-08-09 23:31:02 +0000704 {
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000705 ProcessInfoList process_infos;
706 PlatformSP platform_sp (Platform::GetSelectedPlatform ());
707 if (platform_sp)
708 {
709 platform_sp->FindProcessesByName (wait_name, eNameMatchEquals, process_infos);
710 }
711 if (process_infos.GetSize() > 1)
Jim Ingham7508e732010-08-09 23:31:02 +0000712 {
Jim Ingham4805a1c2010-09-15 01:34:14 +0000713 result.AppendErrorWithFormat("More than one process named %s\n", wait_name);
Jim Ingham7508e732010-08-09 23:31:02 +0000714 result.SetStatus (eReturnStatusFailed);
715 return false;
716 }
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000717 else if (process_infos.GetSize() == 0)
Jim Ingham7508e732010-08-09 23:31:02 +0000718 {
Jim Ingham4805a1c2010-09-15 01:34:14 +0000719 result.AppendErrorWithFormat("Could not find a process named %s\n", wait_name);
Jim Ingham7508e732010-08-09 23:31:02 +0000720 result.SetStatus (eReturnStatusFailed);
721 return false;
722 }
723 else
724 {
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000725 attach_pid = process_infos.GetProcessIDAtIndex (0);
Jim Ingham7508e732010-08-09 23:31:02 +0000726 }
Jim Ingham7508e732010-08-09 23:31:02 +0000727 }
728
729 if (attach_pid != LLDB_INVALID_PROCESS_ID)
730 {
731 error = process->Attach (attach_pid);
732 if (error.Success())
733 {
734 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
735 }
736 else
737 {
738 result.AppendErrorWithFormat ("Attaching to process %i failed: %s.\n",
739 attach_pid,
740 error.AsCString());
741 result.SetStatus (eReturnStatusFailed);
742 }
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000743 // See comment for synchronous_execution above.
744 if (synchronous_execution)
745 {
746 StateType state = process->WaitForProcessToStop (NULL);
747
748 result.SetDidChangeProcessState (true);
749 result.AppendMessageWithFormat ("Process %i %s\n", process->GetID(), StateAsCString (state));
750 result.SetStatus (eReturnStatusSuccessFinishNoResult);
751 }
752 else
753 {
754 result.SetDidChangeProcessState (true);
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000755 result.SetStatus (eReturnStatusSuccessFinishNoResult);
756 }
Jim Ingham7508e732010-08-09 23:31:02 +0000757 }
758 else
759 {
760 result.AppendErrorWithFormat ("No PID specified for attach\n",
761 attach_pid,
762 error.AsCString());
763 result.SetStatus (eReturnStatusFailed);
764
765 }
766 }
767 }
768 }
769
770 if (result.Succeeded())
771 {
772 // Okay, we're done. Last step is to warn if the executable module has changed:
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000773 char new_path[PATH_MAX];
Jim Ingham7508e732010-08-09 23:31:02 +0000774 if (!old_exec_module_sp)
775 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000776 // We might not have a module if we attached to a raw pid...
777 ModuleSP new_module_sp (target->GetExecutableModule());
778 if (new_module_sp)
779 {
780 new_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
781 result.AppendMessageWithFormat("Executable module set to \"%s\".\n", new_path);
782 }
Jim Ingham7508e732010-08-09 23:31:02 +0000783 }
784 else if (old_exec_module_sp->GetFileSpec() != target->GetExecutableModule()->GetFileSpec())
785 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000786 char old_path[PATH_MAX];
Jim Ingham7508e732010-08-09 23:31:02 +0000787
788 old_exec_module_sp->GetFileSpec().GetPath(old_path, PATH_MAX);
789 target->GetExecutableModule()->GetFileSpec().GetPath (new_path, PATH_MAX);
790
791 result.AppendWarningWithFormat("Executable module changed from \"%s\" to \"%s\".\n",
792 old_path, new_path);
793 }
794
795 if (!old_arch_spec.IsValid())
796 {
Greg Clayton940b1032011-02-23 00:35:02 +0000797 result.AppendMessageWithFormat ("Architecture set to: %s.\n", target->GetArchitecture().GetArchitectureName());
Jim Ingham7508e732010-08-09 23:31:02 +0000798 }
799 else if (old_arch_spec != target->GetArchitecture())
800 {
801 result.AppendWarningWithFormat("Architecture changed from %s to %s.\n",
Greg Clayton940b1032011-02-23 00:35:02 +0000802 old_arch_spec.GetArchitectureName(), target->GetArchitecture().GetArchitectureName());
Jim Ingham7508e732010-08-09 23:31:02 +0000803 }
804 }
805 return result.Succeeded();
806 }
807
808 Options *
809 GetOptions ()
810 {
811 return &m_options;
812 }
813
Chris Lattner24943d22010-06-08 16:52:24 +0000814protected:
815
816 CommandOptions m_options;
817};
818
819
820lldb::OptionDefinition
821CommandObjectProcessAttach::CommandOptions::g_option_table[] =
822{
Caroline Tice4d6675c2010-10-01 19:59:14 +0000823{ LLDB_OPT_SET_ALL, false, "plugin", 'P', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
824{ LLDB_OPT_SET_1, false, "pid", 'p', required_argument, NULL, 0, eArgTypePid, "The process ID of an existing process to attach to."},
825{ LLDB_OPT_SET_2, false, "name", 'n', required_argument, NULL, 0, eArgTypeProcessName, "The name of the process to attach to."},
826{ LLDB_OPT_SET_2, false, "waitfor",'w', no_argument, NULL, 0, eArgTypeNone, "Wait for the the process with <process-name> to launch."},
827{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000828};
829
830//-------------------------------------------------------------------------
831// CommandObjectProcessContinue
832//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000833#pragma mark CommandObjectProcessContinue
Chris Lattner24943d22010-06-08 16:52:24 +0000834
835class CommandObjectProcessContinue : public CommandObject
836{
837public:
838
Greg Clayton238c0a12010-09-18 01:14:36 +0000839 CommandObjectProcessContinue (CommandInterpreter &interpreter) :
840 CommandObject (interpreter,
841 "process continue",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000842 "Continue execution of all threads in the current process.",
Chris Lattner24943d22010-06-08 16:52:24 +0000843 "process continue",
844 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
845 {
846 }
847
848
849 ~CommandObjectProcessContinue ()
850 {
851 }
852
853 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000854 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000855 CommandReturnObject &result)
856 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000857 Process *process = m_interpreter.GetDebugger().GetExecutionContext().process;
858 bool synchronous_execution = m_interpreter.GetSynchronous ();
Chris Lattner24943d22010-06-08 16:52:24 +0000859
860 if (process == NULL)
861 {
862 result.AppendError ("no process to continue");
863 result.SetStatus (eReturnStatusFailed);
864 return false;
865 }
866
867 StateType state = process->GetState();
868 if (state == eStateStopped)
869 {
870 if (command.GetArgumentCount() != 0)
871 {
872 result.AppendErrorWithFormat ("The '%s' command does not take any arguments.\n", m_cmd_name.c_str());
873 result.SetStatus (eReturnStatusFailed);
874 return false;
875 }
876
877 const uint32_t num_threads = process->GetThreadList().GetSize();
878
879 // Set the actions that the threads should each take when resuming
880 for (uint32_t idx=0; idx<num_threads; ++idx)
881 {
882 process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState (eStateRunning);
883 }
884
885 Error error(process->Resume());
886 if (error.Success())
887 {
Greg Claytonc1d37752010-10-18 01:45:30 +0000888 result.AppendMessageWithFormat ("Process %i resuming\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000889 if (synchronous_execution)
890 {
Greg Claytonbef15832010-07-14 00:18:15 +0000891 state = process->WaitForProcessToStop (NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000892
893 result.SetDidChangeProcessState (true);
894 result.AppendMessageWithFormat ("Process %i %s\n", process->GetID(), StateAsCString (state));
895 result.SetStatus (eReturnStatusSuccessFinishNoResult);
896 }
897 else
898 {
899 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
900 }
901 }
902 else
903 {
904 result.AppendErrorWithFormat("Failed to resume process: %s.\n", error.AsCString());
905 result.SetStatus (eReturnStatusFailed);
906 }
907 }
908 else
909 {
910 result.AppendErrorWithFormat ("Process cannot be continued from its current state (%s).\n",
911 StateAsCString(state));
912 result.SetStatus (eReturnStatusFailed);
913 }
914 return result.Succeeded();
915 }
916};
917
918//-------------------------------------------------------------------------
919// CommandObjectProcessDetach
920//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000921#pragma mark CommandObjectProcessDetach
Chris Lattner24943d22010-06-08 16:52:24 +0000922
923class CommandObjectProcessDetach : public CommandObject
924{
925public:
926
Greg Clayton238c0a12010-09-18 01:14:36 +0000927 CommandObjectProcessDetach (CommandInterpreter &interpreter) :
928 CommandObject (interpreter,
929 "process detach",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000930 "Detach from the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +0000931 "process detach",
932 eFlagProcessMustBeLaunched)
933 {
934 }
935
936 ~CommandObjectProcessDetach ()
937 {
938 }
939
940 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000941 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000942 CommandReturnObject &result)
943 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000944 Process *process = m_interpreter.GetDebugger().GetExecutionContext().process;
Chris Lattner24943d22010-06-08 16:52:24 +0000945 if (process == NULL)
946 {
947 result.AppendError ("must have a valid process in order to detach");
948 result.SetStatus (eReturnStatusFailed);
949 return false;
950 }
951
Caroline Tice90b42252010-11-02 16:16:53 +0000952 result.AppendMessageWithFormat ("Detaching from process %i\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000953 Error error (process->Detach());
954 if (error.Success())
955 {
956 result.SetStatus (eReturnStatusSuccessFinishResult);
957 }
958 else
959 {
960 result.AppendErrorWithFormat ("Detach failed: %s\n", error.AsCString());
961 result.SetStatus (eReturnStatusFailed);
962 return false;
963 }
964 return result.Succeeded();
965 }
966};
967
968//-------------------------------------------------------------------------
Greg Claytone71e2582011-02-04 01:58:07 +0000969// CommandObjectProcessConnect
970//-------------------------------------------------------------------------
971#pragma mark CommandObjectProcessConnect
972
973class CommandObjectProcessConnect : public CommandObject
974{
975public:
976
977 class CommandOptions : public Options
978 {
979 public:
980
981 CommandOptions () :
982 Options()
983 {
984 // Keep default values of all options in one place: ResetOptionValues ()
985 ResetOptionValues ();
986 }
987
988 ~CommandOptions ()
989 {
990 }
991
992 Error
993 SetOptionValue (int option_idx, const char *option_arg)
994 {
995 Error error;
996 char short_option = (char) m_getopt_table[option_idx].val;
997
998 switch (short_option)
999 {
1000 case 'p':
1001 plugin_name.assign (option_arg);
1002 break;
1003
1004 default:
1005 error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
1006 break;
1007 }
1008 return error;
1009 }
1010
1011 void
1012 ResetOptionValues ()
1013 {
1014 Options::ResetOptionValues();
1015 plugin_name.clear();
1016 }
1017
1018 const lldb::OptionDefinition*
1019 GetDefinitions ()
1020 {
1021 return g_option_table;
1022 }
1023
1024 // Options table: Required for subclasses of Options.
1025
1026 static lldb::OptionDefinition g_option_table[];
1027
1028 // Instance variables to hold the values for command options.
1029
1030 std::string plugin_name;
1031 };
1032
1033 CommandObjectProcessConnect (CommandInterpreter &interpreter) :
1034 CommandObject (interpreter,
1035 "process connect",
1036 "Connect to a remote debug service.",
1037 "process connect <remote-url>",
1038 0)
1039 {
1040 }
1041
1042 ~CommandObjectProcessConnect ()
1043 {
1044 }
1045
1046
1047 bool
1048 Execute (Args& command,
1049 CommandReturnObject &result)
1050 {
1051
1052 TargetSP target_sp (m_interpreter.GetDebugger().GetSelectedTarget());
1053 Error error;
1054 Process *process = m_interpreter.GetDebugger().GetExecutionContext().process;
1055 if (process)
1056 {
1057 if (process->IsAlive())
1058 {
1059 result.AppendErrorWithFormat ("Process %u is currently being debugged, kill the process before connecting.\n",
1060 process->GetID());
1061 result.SetStatus (eReturnStatusFailed);
1062 return false;
1063 }
1064 }
1065
1066 if (!target_sp)
1067 {
1068 // If there isn't a current target create one.
1069 FileSpec emptyFileSpec;
1070 ArchSpec emptyArchSpec;
1071
1072 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
1073 emptyFileSpec,
1074 emptyArchSpec,
Greg Claytone71e2582011-02-04 01:58:07 +00001075 false,
1076 target_sp);
1077 if (!target_sp || error.Fail())
1078 {
1079 result.AppendError(error.AsCString("Error creating target"));
1080 result.SetStatus (eReturnStatusFailed);
1081 return false;
1082 }
1083 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target_sp.get());
1084 }
1085
1086 if (command.GetArgumentCount() == 1)
1087 {
1088 const char *plugin_name = NULL;
1089 if (!m_options.plugin_name.empty())
1090 plugin_name = m_options.plugin_name.c_str();
1091
1092 const char *remote_url = command.GetArgumentAtIndex(0);
1093 process = target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name).get();
1094
1095 if (process)
1096 {
1097 error = process->ConnectRemote (remote_url);
1098
1099 if (error.Fail())
1100 {
1101 result.AppendError(error.AsCString("Remote connect failed"));
1102 result.SetStatus (eReturnStatusFailed);
1103 return false;
1104 }
1105 }
1106 else
1107 {
1108 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",
1109 m_cmd_name.c_str(),
1110 m_cmd_syntax.c_str());
1111 result.SetStatus (eReturnStatusFailed);
1112 }
1113 }
1114 else
1115 {
1116 result.AppendErrorWithFormat ("'%s' takes exactly one argument:\nUsage: \n",
1117 m_cmd_name.c_str(),
1118 m_cmd_syntax.c_str());
1119 result.SetStatus (eReturnStatusFailed);
1120 }
1121 return result.Succeeded();
1122 }
1123
1124 Options *
1125 GetOptions ()
1126 {
1127 return &m_options;
1128 }
1129
1130protected:
1131
1132 CommandOptions m_options;
1133};
1134
1135
1136lldb::OptionDefinition
1137CommandObjectProcessConnect::CommandOptions::g_option_table[] =
1138{
1139 { LLDB_OPT_SET_ALL, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
1140 { 0, false, NULL, 0 , 0, NULL, 0, eArgTypeNone, NULL }
1141};
1142
1143//-------------------------------------------------------------------------
Greg Clayton0baa3942010-11-04 01:54:29 +00001144// CommandObjectProcessLoad
1145//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001146#pragma mark CommandObjectProcessLoad
Greg Clayton0baa3942010-11-04 01:54:29 +00001147
1148class CommandObjectProcessLoad : public CommandObject
1149{
1150public:
1151
1152 CommandObjectProcessLoad (CommandInterpreter &interpreter) :
1153 CommandObject (interpreter,
1154 "process load",
1155 "Load a shared library into the current process.",
1156 "process load <filename> [<filename> ...]",
1157 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
1158 {
1159 }
1160
1161 ~CommandObjectProcessLoad ()
1162 {
1163 }
1164
1165 bool
1166 Execute (Args& command,
1167 CommandReturnObject &result)
1168 {
1169 Process *process = m_interpreter.GetDebugger().GetExecutionContext().process;
1170 if (process == NULL)
1171 {
1172 result.AppendError ("must have a valid process in order to load a shared library");
1173 result.SetStatus (eReturnStatusFailed);
1174 return false;
1175 }
1176
1177 const uint32_t argc = command.GetArgumentCount();
1178
1179 for (uint32_t i=0; i<argc; ++i)
1180 {
1181 Error error;
1182 const char *image_path = command.GetArgumentAtIndex(i);
1183 FileSpec image_spec (image_path, false);
1184 uint32_t image_token = process->LoadImage(image_spec, error);
1185 if (image_token != LLDB_INVALID_IMAGE_TOKEN)
1186 {
1187 result.AppendMessageWithFormat ("Loading \"%s\"...ok\nImage %u loaded.\n", image_path, image_token);
1188 result.SetStatus (eReturnStatusSuccessFinishResult);
1189 }
1190 else
1191 {
1192 result.AppendErrorWithFormat ("failed to load '%s': %s", image_path, error.AsCString());
1193 result.SetStatus (eReturnStatusFailed);
1194 }
1195 }
1196 return result.Succeeded();
1197 }
1198};
1199
1200
1201//-------------------------------------------------------------------------
1202// CommandObjectProcessUnload
1203//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001204#pragma mark CommandObjectProcessUnload
Greg Clayton0baa3942010-11-04 01:54:29 +00001205
1206class CommandObjectProcessUnload : public CommandObject
1207{
1208public:
1209
1210 CommandObjectProcessUnload (CommandInterpreter &interpreter) :
1211 CommandObject (interpreter,
1212 "process unload",
1213 "Unload a shared library from the current process using the index returned by a previous call to \"process load\".",
1214 "process unload <index>",
1215 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
1216 {
1217 }
1218
1219 ~CommandObjectProcessUnload ()
1220 {
1221 }
1222
1223 bool
1224 Execute (Args& command,
1225 CommandReturnObject &result)
1226 {
1227 Process *process = m_interpreter.GetDebugger().GetExecutionContext().process;
1228 if (process == NULL)
1229 {
1230 result.AppendError ("must have a valid process in order to load a shared library");
1231 result.SetStatus (eReturnStatusFailed);
1232 return false;
1233 }
1234
1235 const uint32_t argc = command.GetArgumentCount();
1236
1237 for (uint32_t i=0; i<argc; ++i)
1238 {
1239 const char *image_token_cstr = command.GetArgumentAtIndex(i);
1240 uint32_t image_token = Args::StringToUInt32(image_token_cstr, LLDB_INVALID_IMAGE_TOKEN, 0);
1241 if (image_token == LLDB_INVALID_IMAGE_TOKEN)
1242 {
1243 result.AppendErrorWithFormat ("invalid image index argument '%s'", image_token_cstr);
1244 result.SetStatus (eReturnStatusFailed);
1245 break;
1246 }
1247 else
1248 {
1249 Error error (process->UnloadImage(image_token));
1250 if (error.Success())
1251 {
1252 result.AppendMessageWithFormat ("Unloading shared library with index %u...ok\n", image_token);
1253 result.SetStatus (eReturnStatusSuccessFinishResult);
1254 }
1255 else
1256 {
1257 result.AppendErrorWithFormat ("failed to unload image: %s", error.AsCString());
1258 result.SetStatus (eReturnStatusFailed);
1259 break;
1260 }
1261 }
1262 }
1263 return result.Succeeded();
1264 }
1265};
1266
1267//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001268// CommandObjectProcessSignal
1269//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001270#pragma mark CommandObjectProcessSignal
Chris Lattner24943d22010-06-08 16:52:24 +00001271
1272class CommandObjectProcessSignal : public CommandObject
1273{
1274public:
1275
Greg Clayton238c0a12010-09-18 01:14:36 +00001276 CommandObjectProcessSignal (CommandInterpreter &interpreter) :
1277 CommandObject (interpreter,
1278 "process signal",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001279 "Send a UNIX signal to the current process being debugged.",
Caroline Tice43b014a2010-10-04 22:28:36 +00001280 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001281 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001282 CommandArgumentEntry arg;
1283 CommandArgumentData signal_arg;
1284
1285 // Define the first (and only) variant of this arg.
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001286 signal_arg.arg_type = eArgTypeUnixSignal;
Caroline Tice43b014a2010-10-04 22:28:36 +00001287 signal_arg.arg_repetition = eArgRepeatPlain;
1288
1289 // There is only one variant this argument could be; put it into the argument entry.
1290 arg.push_back (signal_arg);
1291
1292 // Push the data for the first argument into the m_arguments vector.
1293 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001294 }
1295
1296 ~CommandObjectProcessSignal ()
1297 {
1298 }
1299
1300 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001301 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001302 CommandReturnObject &result)
1303 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001304 Process *process = m_interpreter.GetDebugger().GetExecutionContext().process;
Chris Lattner24943d22010-06-08 16:52:24 +00001305 if (process == NULL)
1306 {
1307 result.AppendError ("no process to signal");
1308 result.SetStatus (eReturnStatusFailed);
1309 return false;
1310 }
1311
1312 if (command.GetArgumentCount() == 1)
1313 {
Greg Clayton8f6be2a2010-10-09 01:40:57 +00001314 int signo = LLDB_INVALID_SIGNAL_NUMBER;
1315
1316 const char *signal_name = command.GetArgumentAtIndex(0);
1317 if (::isxdigit (signal_name[0]))
1318 signo = Args::StringToSInt32(signal_name, LLDB_INVALID_SIGNAL_NUMBER, 0);
1319 else
1320 signo = process->GetUnixSignals().GetSignalNumberFromName (signal_name);
1321
1322 if (signo == LLDB_INVALID_SIGNAL_NUMBER)
Chris Lattner24943d22010-06-08 16:52:24 +00001323 {
1324 result.AppendErrorWithFormat ("Invalid signal argument '%s'.\n", command.GetArgumentAtIndex(0));
1325 result.SetStatus (eReturnStatusFailed);
1326 }
1327 else
1328 {
1329 Error error (process->Signal (signo));
1330 if (error.Success())
1331 {
1332 result.SetStatus (eReturnStatusSuccessFinishResult);
1333 }
1334 else
1335 {
1336 result.AppendErrorWithFormat ("Failed to send signal %i: %s\n", signo, error.AsCString());
1337 result.SetStatus (eReturnStatusFailed);
1338 }
1339 }
1340 }
1341 else
1342 {
1343 result.AppendErrorWithFormat("'%s' takes exactly one signal number argument:\nUsage: \n", m_cmd_name.c_str(),
1344 m_cmd_syntax.c_str());
1345 result.SetStatus (eReturnStatusFailed);
1346 }
1347 return result.Succeeded();
1348 }
1349};
1350
1351
1352//-------------------------------------------------------------------------
1353// CommandObjectProcessInterrupt
1354//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001355#pragma mark CommandObjectProcessInterrupt
Chris Lattner24943d22010-06-08 16:52:24 +00001356
1357class CommandObjectProcessInterrupt : public CommandObject
1358{
1359public:
1360
1361
Greg Clayton238c0a12010-09-18 01:14:36 +00001362 CommandObjectProcessInterrupt (CommandInterpreter &interpreter) :
1363 CommandObject (interpreter,
1364 "process interrupt",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001365 "Interrupt the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +00001366 "process interrupt",
1367 eFlagProcessMustBeLaunched)
1368 {
1369 }
1370
1371 ~CommandObjectProcessInterrupt ()
1372 {
1373 }
1374
1375 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001376 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001377 CommandReturnObject &result)
1378 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001379 Process *process = m_interpreter.GetDebugger().GetExecutionContext().process;
Chris Lattner24943d22010-06-08 16:52:24 +00001380 if (process == NULL)
1381 {
1382 result.AppendError ("no process to halt");
1383 result.SetStatus (eReturnStatusFailed);
1384 return false;
1385 }
1386
1387 if (command.GetArgumentCount() == 0)
1388 {
1389 Error error(process->Halt ());
1390 if (error.Success())
1391 {
1392 result.SetStatus (eReturnStatusSuccessFinishResult);
1393
1394 // Maybe we should add a "SuspendThreadPlans so we
1395 // can halt, and keep in place all the current thread plans.
1396 process->GetThreadList().DiscardThreadPlans();
1397 }
1398 else
1399 {
1400 result.AppendErrorWithFormat ("Failed to halt process: %s\n", error.AsCString());
1401 result.SetStatus (eReturnStatusFailed);
1402 }
1403 }
1404 else
1405 {
1406 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: \n",
1407 m_cmd_name.c_str(),
1408 m_cmd_syntax.c_str());
1409 result.SetStatus (eReturnStatusFailed);
1410 }
1411 return result.Succeeded();
1412 }
1413};
1414
1415//-------------------------------------------------------------------------
1416// CommandObjectProcessKill
1417//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001418#pragma mark CommandObjectProcessKill
Chris Lattner24943d22010-06-08 16:52:24 +00001419
1420class CommandObjectProcessKill : public CommandObject
1421{
1422public:
1423
Greg Clayton238c0a12010-09-18 01:14:36 +00001424 CommandObjectProcessKill (CommandInterpreter &interpreter) :
1425 CommandObject (interpreter,
1426 "process kill",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001427 "Terminate the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +00001428 "process kill",
1429 eFlagProcessMustBeLaunched)
1430 {
1431 }
1432
1433 ~CommandObjectProcessKill ()
1434 {
1435 }
1436
1437 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001438 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001439 CommandReturnObject &result)
1440 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001441 Process *process = m_interpreter.GetDebugger().GetExecutionContext().process;
Chris Lattner24943d22010-06-08 16:52:24 +00001442 if (process == NULL)
1443 {
1444 result.AppendError ("no process to kill");
1445 result.SetStatus (eReturnStatusFailed);
1446 return false;
1447 }
1448
1449 if (command.GetArgumentCount() == 0)
1450 {
1451 Error error (process->Destroy());
1452 if (error.Success())
1453 {
1454 result.SetStatus (eReturnStatusSuccessFinishResult);
1455 }
1456 else
1457 {
1458 result.AppendErrorWithFormat ("Failed to kill process: %s\n", error.AsCString());
1459 result.SetStatus (eReturnStatusFailed);
1460 }
1461 }
1462 else
1463 {
1464 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: \n",
1465 m_cmd_name.c_str(),
1466 m_cmd_syntax.c_str());
1467 result.SetStatus (eReturnStatusFailed);
1468 }
1469 return result.Succeeded();
1470 }
1471};
1472
1473//-------------------------------------------------------------------------
Jim Ingham41313fc2010-06-18 01:23:09 +00001474// CommandObjectProcessStatus
1475//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001476#pragma mark CommandObjectProcessStatus
1477
Jim Ingham41313fc2010-06-18 01:23:09 +00001478class CommandObjectProcessStatus : public CommandObject
1479{
1480public:
Greg Clayton238c0a12010-09-18 01:14:36 +00001481 CommandObjectProcessStatus (CommandInterpreter &interpreter) :
1482 CommandObject (interpreter,
1483 "process status",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001484 "Show the current status and location of executing process.",
1485 "process status",
Jim Ingham41313fc2010-06-18 01:23:09 +00001486 0)
1487 {
1488 }
1489
1490 ~CommandObjectProcessStatus()
1491 {
1492 }
1493
1494
1495 bool
1496 Execute
1497 (
1498 Args& command,
Jim Ingham41313fc2010-06-18 01:23:09 +00001499 CommandReturnObject &result
1500 )
1501 {
Jim Ingham2e8cb8a2011-02-19 02:53:09 +00001502 Stream &output_stream = result.GetOutputStream();
Jim Ingham41313fc2010-06-18 01:23:09 +00001503 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Clayton238c0a12010-09-18 01:14:36 +00001504 ExecutionContext exe_ctx(m_interpreter.GetDebugger().GetExecutionContext());
Jim Ingham41313fc2010-06-18 01:23:09 +00001505 if (exe_ctx.process)
1506 {
1507 const StateType state = exe_ctx.process->GetState();
1508 if (StateIsStoppedState(state))
1509 {
1510 if (state == eStateExited)
1511 {
1512 int exit_status = exe_ctx.process->GetExitStatus();
1513 const char *exit_description = exe_ctx.process->GetExitDescription();
1514 output_stream.Printf ("Process %d exited with status = %i (0x%8.8x) %s\n",
1515 exe_ctx.process->GetID(),
1516 exit_status,
1517 exit_status,
1518 exit_description ? exit_description : "");
1519 }
1520 else
1521 {
Greg Claytona2f74232011-02-24 22:24:29 +00001522 if (state == eStateConnected)
1523 output_stream.Printf ("Connected to remote target.\n");
1524 else
1525 output_stream.Printf ("Process %d %s\n", exe_ctx.process->GetID(), StateAsCString (state));
Jim Ingham41313fc2010-06-18 01:23:09 +00001526 if (exe_ctx.thread == NULL)
1527 exe_ctx.thread = exe_ctx.process->GetThreadList().GetThreadAtIndex(0).get();
1528 if (exe_ctx.thread != NULL)
1529 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001530 DisplayThreadsInfo (m_interpreter, &exe_ctx, result, true, true);
Jim Ingham41313fc2010-06-18 01:23:09 +00001531 }
1532 else
1533 {
1534 result.AppendError ("No valid thread found in current process.");
1535 result.SetStatus (eReturnStatusFailed);
1536 }
1537 }
1538 }
1539 else
1540 {
1541 output_stream.Printf ("Process %d is running.\n",
1542 exe_ctx.process->GetID());
1543 }
1544 }
1545 else
1546 {
1547 result.AppendError ("No current location or status available.");
1548 result.SetStatus (eReturnStatusFailed);
1549 }
1550 return result.Succeeded();
1551 }
1552};
1553
1554//-------------------------------------------------------------------------
Caroline Tice23d6f272010-10-13 20:44:39 +00001555// CommandObjectProcessHandle
1556//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001557#pragma mark CommandObjectProcessHandle
Caroline Tice23d6f272010-10-13 20:44:39 +00001558
1559class CommandObjectProcessHandle : public CommandObject
1560{
1561public:
1562
1563 class CommandOptions : public Options
1564 {
1565 public:
1566
1567 CommandOptions () :
1568 Options ()
1569 {
1570 ResetOptionValues ();
1571 }
1572
1573 ~CommandOptions ()
1574 {
1575 }
1576
1577 Error
1578 SetOptionValue (int option_idx, const char *option_arg)
1579 {
1580 Error error;
1581 char short_option = (char) m_getopt_table[option_idx].val;
1582
1583 switch (short_option)
1584 {
1585 case 's':
1586 stop = option_arg;
1587 break;
1588 case 'n':
1589 notify = option_arg;
1590 break;
1591 case 'p':
1592 pass = option_arg;
1593 break;
1594 default:
1595 error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
1596 break;
1597 }
1598 return error;
1599 }
1600
1601 void
1602 ResetOptionValues ()
1603 {
1604 Options::ResetOptionValues();
1605 stop.clear();
1606 notify.clear();
1607 pass.clear();
1608 }
1609
1610 const lldb::OptionDefinition*
1611 GetDefinitions ()
1612 {
1613 return g_option_table;
1614 }
1615
1616 // Options table: Required for subclasses of Options.
1617
1618 static lldb::OptionDefinition g_option_table[];
1619
1620 // Instance variables to hold the values for command options.
1621
1622 std::string stop;
1623 std::string notify;
1624 std::string pass;
1625 };
1626
1627
1628 CommandObjectProcessHandle (CommandInterpreter &interpreter) :
1629 CommandObject (interpreter,
1630 "process handle",
Caroline Ticee7471982010-10-14 21:31:13 +00001631 "Show or update what the process and debugger should do with various signals received from the OS.",
Caroline Tice23d6f272010-10-13 20:44:39 +00001632 NULL)
1633 {
Caroline Ticee7471982010-10-14 21:31:13 +00001634 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 +00001635 CommandArgumentEntry arg;
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001636 CommandArgumentData signal_arg;
Caroline Tice23d6f272010-10-13 20:44:39 +00001637
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001638 signal_arg.arg_type = eArgTypeUnixSignal;
1639 signal_arg.arg_repetition = eArgRepeatStar;
Caroline Tice23d6f272010-10-13 20:44:39 +00001640
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001641 arg.push_back (signal_arg);
Caroline Tice23d6f272010-10-13 20:44:39 +00001642
1643 m_arguments.push_back (arg);
1644 }
1645
1646 ~CommandObjectProcessHandle ()
1647 {
1648 }
1649
1650 Options *
1651 GetOptions ()
1652 {
1653 return &m_options;
1654 }
1655
1656 bool
Caroline Ticee7471982010-10-14 21:31:13 +00001657 VerifyCommandOptionValue (const std::string &option, int &real_value)
Caroline Tice23d6f272010-10-13 20:44:39 +00001658 {
1659 bool okay = true;
1660
Caroline Ticee7471982010-10-14 21:31:13 +00001661 bool success = false;
1662 bool tmp_value = Args::StringToBoolean (option.c_str(), false, &success);
1663
1664 if (success && tmp_value)
1665 real_value = 1;
1666 else if (success && !tmp_value)
1667 real_value = 0;
Caroline Tice23d6f272010-10-13 20:44:39 +00001668 else
1669 {
1670 // If the value isn't 'true' or 'false', it had better be 0 or 1.
Caroline Ticee7471982010-10-14 21:31:13 +00001671 real_value = Args::StringToUInt32 (option.c_str(), 3);
1672 if (real_value != 0 && real_value != 1)
Caroline Tice23d6f272010-10-13 20:44:39 +00001673 okay = false;
1674 }
1675
1676 return okay;
1677 }
1678
Caroline Ticee7471982010-10-14 21:31:13 +00001679 void
1680 PrintSignalHeader (Stream &str)
1681 {
1682 str.Printf ("NAME PASS STOP NOTIFY\n");
1683 str.Printf ("========== ===== ===== ======\n");
1684 }
1685
1686 void
1687 PrintSignal (Stream &str, int32_t signo, const char *sig_name, UnixSignals &signals)
1688 {
1689 bool stop;
1690 bool suppress;
1691 bool notify;
1692
1693 str.Printf ("%-10s ", sig_name);
1694 if (signals.GetSignalInfo (signo, suppress, stop, notify))
1695 {
1696 bool pass = !suppress;
1697 str.Printf ("%s %s %s",
1698 (pass ? "true " : "false"),
1699 (stop ? "true " : "false"),
1700 (notify ? "true " : "false"));
1701 }
1702 str.Printf ("\n");
1703 }
1704
1705 void
1706 PrintSignalInformation (Stream &str, Args &signal_args, int num_valid_signals, UnixSignals &signals)
1707 {
1708 PrintSignalHeader (str);
1709
1710 if (num_valid_signals > 0)
1711 {
1712 size_t num_args = signal_args.GetArgumentCount();
1713 for (size_t i = 0; i < num_args; ++i)
1714 {
1715 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1716 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
1717 PrintSignal (str, signo, signal_args.GetArgumentAtIndex (i), signals);
1718 }
1719 }
1720 else // Print info for ALL signals
1721 {
1722 int32_t signo = signals.GetFirstSignalNumber();
1723 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1724 {
1725 PrintSignal (str, signo, signals.GetSignalAsCString (signo), signals);
1726 signo = signals.GetNextSignalNumber (signo);
1727 }
1728 }
1729 }
1730
Caroline Tice23d6f272010-10-13 20:44:39 +00001731 bool
1732 Execute (Args &signal_args, CommandReturnObject &result)
1733 {
1734 TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
1735
1736 if (!target_sp)
1737 {
1738 result.AppendError ("No current target;"
1739 " cannot handle signals until you have a valid target and process.\n");
1740 result.SetStatus (eReturnStatusFailed);
1741 return false;
1742 }
1743
1744 ProcessSP process_sp = target_sp->GetProcessSP();
1745
1746 if (!process_sp)
1747 {
1748 result.AppendError ("No current process; cannot handle signals until you have a valid process.\n");
1749 result.SetStatus (eReturnStatusFailed);
1750 return false;
1751 }
1752
Caroline Tice23d6f272010-10-13 20:44:39 +00001753 int stop_action = -1; // -1 means leave the current setting alone
Caroline Ticee7471982010-10-14 21:31:13 +00001754 int pass_action = -1; // -1 means leave the current setting alone
Caroline Tice23d6f272010-10-13 20:44:39 +00001755 int notify_action = -1; // -1 means leave the current setting alone
1756
1757 if (! m_options.stop.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001758 && ! VerifyCommandOptionValue (m_options.stop, stop_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001759 {
1760 result.AppendError ("Invalid argument for command option --stop; must be true or false.\n");
1761 result.SetStatus (eReturnStatusFailed);
1762 return false;
1763 }
1764
1765 if (! m_options.notify.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001766 && ! VerifyCommandOptionValue (m_options.notify, notify_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001767 {
1768 result.AppendError ("Invalid argument for command option --notify; must be true or false.\n");
1769 result.SetStatus (eReturnStatusFailed);
1770 return false;
1771 }
1772
1773 if (! m_options.pass.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001774 && ! VerifyCommandOptionValue (m_options.pass, pass_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001775 {
1776 result.AppendError ("Invalid argument for command option --pass; must be true or false.\n");
1777 result.SetStatus (eReturnStatusFailed);
1778 return false;
1779 }
1780
1781 size_t num_args = signal_args.GetArgumentCount();
1782 UnixSignals &signals = process_sp->GetUnixSignals();
1783 int num_signals_set = 0;
1784
Caroline Ticee7471982010-10-14 21:31:13 +00001785 if (num_args > 0)
Caroline Tice23d6f272010-10-13 20:44:39 +00001786 {
Caroline Ticee7471982010-10-14 21:31:13 +00001787 for (size_t i = 0; i < num_args; ++i)
Caroline Tice23d6f272010-10-13 20:44:39 +00001788 {
Caroline Ticee7471982010-10-14 21:31:13 +00001789 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1790 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
Caroline Tice23d6f272010-10-13 20:44:39 +00001791 {
Caroline Ticee7471982010-10-14 21:31:13 +00001792 // Casting the actions as bools here should be okay, because VerifyCommandOptionValue guarantees
1793 // the value is either 0 or 1.
1794 if (stop_action != -1)
1795 signals.SetShouldStop (signo, (bool) stop_action);
1796 if (pass_action != -1)
1797 {
1798 bool suppress = ! ((bool) pass_action);
1799 signals.SetShouldSuppress (signo, suppress);
1800 }
1801 if (notify_action != -1)
1802 signals.SetShouldNotify (signo, (bool) notify_action);
1803 ++num_signals_set;
Caroline Tice23d6f272010-10-13 20:44:39 +00001804 }
Caroline Ticee7471982010-10-14 21:31:13 +00001805 else
1806 {
1807 result.AppendErrorWithFormat ("Invalid signal name '%s'\n", signal_args.GetArgumentAtIndex (i));
1808 }
Caroline Tice23d6f272010-10-13 20:44:39 +00001809 }
1810 }
Caroline Ticee7471982010-10-14 21:31:13 +00001811 else
1812 {
1813 // No signal specified, if any command options were specified, update ALL signals.
1814 if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1))
1815 {
1816 if (m_interpreter.Confirm ("Do you really want to update all the signals?", false))
1817 {
1818 int32_t signo = signals.GetFirstSignalNumber();
1819 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1820 {
1821 if (notify_action != -1)
1822 signals.SetShouldNotify (signo, (bool) notify_action);
1823 if (stop_action != -1)
1824 signals.SetShouldStop (signo, (bool) stop_action);
1825 if (pass_action != -1)
1826 {
1827 bool suppress = ! ((bool) pass_action);
1828 signals.SetShouldSuppress (signo, suppress);
1829 }
1830 signo = signals.GetNextSignalNumber (signo);
1831 }
1832 }
1833 }
1834 }
1835
1836 PrintSignalInformation (result.GetOutputStream(), signal_args, num_signals_set, signals);
Caroline Tice23d6f272010-10-13 20:44:39 +00001837
1838 if (num_signals_set > 0)
1839 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1840 else
1841 result.SetStatus (eReturnStatusFailed);
1842
1843 return result.Succeeded();
1844 }
1845
1846protected:
1847
1848 CommandOptions m_options;
1849};
1850
1851lldb::OptionDefinition
1852CommandObjectProcessHandle::CommandOptions::g_option_table[] =
1853{
1854{ 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." },
1855{ 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." },
1856{ LLDB_OPT_SET_1, false, "pass", 'p', required_argument, NULL, 0, eArgTypeBoolean, "Whether or not the signal should be passed to the process." },
1857{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1858};
1859
1860//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001861// CommandObjectMultiwordProcess
1862//-------------------------------------------------------------------------
1863
Greg Clayton63094e02010-06-23 01:19:29 +00001864CommandObjectMultiwordProcess::CommandObjectMultiwordProcess (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00001865 CommandObjectMultiword (interpreter,
1866 "process",
1867 "A set of commands for operating on a process.",
1868 "process <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00001869{
Greg Clayton238c0a12010-09-18 01:14:36 +00001870 LoadSubCommand ("attach", CommandObjectSP (new CommandObjectProcessAttach (interpreter)));
1871 LoadSubCommand ("launch", CommandObjectSP (new CommandObjectProcessLaunch (interpreter)));
1872 LoadSubCommand ("continue", CommandObjectSP (new CommandObjectProcessContinue (interpreter)));
Greg Claytone71e2582011-02-04 01:58:07 +00001873 LoadSubCommand ("connect", CommandObjectSP (new CommandObjectProcessConnect (interpreter)));
Greg Clayton238c0a12010-09-18 01:14:36 +00001874 LoadSubCommand ("detach", CommandObjectSP (new CommandObjectProcessDetach (interpreter)));
Greg Clayton0baa3942010-11-04 01:54:29 +00001875 LoadSubCommand ("load", CommandObjectSP (new CommandObjectProcessLoad (interpreter)));
1876 LoadSubCommand ("unload", CommandObjectSP (new CommandObjectProcessUnload (interpreter)));
Greg Clayton238c0a12010-09-18 01:14:36 +00001877 LoadSubCommand ("signal", CommandObjectSP (new CommandObjectProcessSignal (interpreter)));
Caroline Tice23d6f272010-10-13 20:44:39 +00001878 LoadSubCommand ("handle", CommandObjectSP (new CommandObjectProcessHandle (interpreter)));
Greg Clayton238c0a12010-09-18 01:14:36 +00001879 LoadSubCommand ("status", CommandObjectSP (new CommandObjectProcessStatus (interpreter)));
1880 LoadSubCommand ("interrupt", CommandObjectSP (new CommandObjectProcessInterrupt (interpreter)));
1881 LoadSubCommand ("kill", CommandObjectSP (new CommandObjectProcessKill (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00001882}
1883
1884CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess ()
1885{
1886}
1887