blob: e7d392da030e82f1d146ee890a9873cbadd241e3 [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
Greg Clayton36bc5ea2011-11-03 21:22:33 +000038// class CommandOptions : public Options
39// {
40// public:
41//
42// CommandOptions (CommandInterpreter &interpreter) :
43// Options(interpreter)
44// {
45// // Keep default values of all options in one place: OptionParsingStarting ()
46// OptionParsingStarting ();
47// }
48//
49// ~CommandOptions ()
50// {
51// }
52//
53// Error
54// SetOptionValue (uint32_t option_idx, const char *option_arg)
55// {
56// Error error;
57// char short_option = (char) m_getopt_table[option_idx].val;
58//
59// switch (short_option)
60// {
61// 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;
68// case 't':
69// if (option_arg && option_arg[0])
70// tty_name.assign (option_arg);
71// in_new_tty = true;
72// break;
73// default:
74// error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
75// break;
76//
77// }
78// return error;
79// }
80//
81// void
82// OptionParsingStarting ()
83// {
84// stop_at_entry = false;
85// in_new_tty = false;
86// tty_name.clear();
87// stdin_path.clear();
88// stdout_path.clear();
89// stderr_path.clear();
90// plugin_name.clear();
91// working_dir.clear();
92// no_stdio = false;
93// }
94//
95// const OptionDefinition*
96// GetDefinitions ()
97// {
98// return g_option_table;
99// }
100//
101// // Options table: Required for subclasses of Options.
102//
103// static OptionDefinition g_option_table[];
104//
105// // Instance variables to hold the values for command options.
106//
107// bool stop_at_entry;
108// bool in_new_tty;
109// bool no_stdio;
110// std::string tty_name;
111// std::string stderr_path;
112// std::string stdin_path;
113// std::string stdout_path;
114// std::string plugin_name;
115// std::string working_dir;
116//
117// };
Chris Lattner24943d22010-06-08 16:52:24 +0000118
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 Claytonabb33022011-11-08 02:43:13 +0000154 Debugger &debugger = m_interpreter.GetDebugger();
155 Target *target = debugger.GetSelectedTarget().get();
156 Error error;
Chris Lattner24943d22010-06-08 16:52:24 +0000157
158 if (target == NULL)
159 {
Greg Claytone1f50b92011-05-03 22:09:39 +0000160 result.AppendError ("invalid target, create a debug target using the 'target create' command");
Chris Lattner24943d22010-06-08 16:52:24 +0000161 result.SetStatus (eReturnStatusFailed);
162 return false;
163 }
Chris Lattner24943d22010-06-08 16:52:24 +0000164 // If our listener is NULL, users aren't allows to launch
Chris Lattner24943d22010-06-08 16:52:24 +0000165 char filename[PATH_MAX];
Greg Clayton5beb99d2011-08-11 02:48:45 +0000166 const Module *exe_module = target->GetExecutableModulePointer();
Greg Claytona2f74232011-02-24 22:24:29 +0000167
168 if (exe_module == NULL)
169 {
Greg Claytone1f50b92011-05-03 22:09:39 +0000170 result.AppendError ("no file in target, create a debug target using the 'target create' command");
Greg Claytona2f74232011-02-24 22:24:29 +0000171 result.SetStatus (eReturnStatusFailed);
172 return false;
173 }
174
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000175 exe_module->GetFileSpec().GetPath (filename, sizeof(filename));
Chris Lattner24943d22010-06-08 16:52:24 +0000176
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000177 const bool add_exe_file_as_first_arg = true;
178 m_options.launch_info.SetExecutableFile(exe_module->GetFileSpec(), add_exe_file_as_first_arg);
179
Greg Claytona2f74232011-02-24 22:24:29 +0000180 StateType state = eStateInvalid;
Greg Clayton567e7f32011-09-22 04:58:26 +0000181 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytona2f74232011-02-24 22:24:29 +0000182 if (process)
183 {
184 state = process->GetState();
185
186 if (process->IsAlive() && state != eStateConnected)
187 {
188 char message[1024];
189 if (process->GetState() == eStateAttaching)
190 ::strncpy (message, "There is a pending attach, abort it and launch a new process?", sizeof(message));
191 else
192 ::strncpy (message, "There is a running process, kill it and restart?", sizeof(message));
193
194 if (!m_interpreter.Confirm (message, true))
Jim Ingham22dc9722010-12-09 18:58:16 +0000195 {
Greg Claytona2f74232011-02-24 22:24:29 +0000196 result.SetStatus (eReturnStatusFailed);
197 return false;
Jim Ingham22dc9722010-12-09 18:58:16 +0000198 }
199 else
200 {
Greg Claytonabb33022011-11-08 02:43:13 +0000201 Error destroy_error (process->Destroy());
202 if (destroy_error.Success())
Greg Claytona2f74232011-02-24 22:24:29 +0000203 {
204 result.SetStatus (eReturnStatusSuccessFinishResult);
205 }
206 else
207 {
Greg Claytonabb33022011-11-08 02:43:13 +0000208 result.AppendErrorWithFormat ("Failed to kill process: %s\n", destroy_error.AsCString());
Greg Claytona2f74232011-02-24 22:24:29 +0000209 result.SetStatus (eReturnStatusFailed);
210 }
Jim Ingham22dc9722010-12-09 18:58:16 +0000211 }
212 }
Chris Lattner24943d22010-06-08 16:52:24 +0000213 }
Jim Ingham22dc9722010-12-09 18:58:16 +0000214
Greg Claytonabb33022011-11-08 02:43:13 +0000215 if (launch_args.GetArgumentCount() > 0)
216 {
217 m_options.launch_info.GetArguments().AppendArguments (launch_args);
218 }
219
220
221 if (state == eStateConnected)
222 {
223 if (m_options.launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
224 {
225 result.AppendWarning("can't launch in tty when launching through a remote connection");
226 m_options.launch_info.GetFlags().Clear (eLaunchFlagLaunchInTTY);
227 }
228 }
229 else
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000230 {
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000231 const char *plugin_name = m_options.launch_info.GetProcessPluginName();
Greg Claytona2f74232011-02-24 22:24:29 +0000232
Greg Claytonabb33022011-11-08 02:43:13 +0000233 if (m_options.launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
234 {
Greg Clayton2d9adb72011-11-12 02:10:56 +0000235 m_options.launch_info.GetArchitecture() = target->GetArchitecture();
236
Greg Claytonabb33022011-11-08 02:43:13 +0000237 process = target->GetPlatform()->DebugProcess (m_options.launch_info,
238 debugger,
239 target,
240 debugger.GetListener(),
241 error).get();
242 }
243 else
244 {
245 process = target->CreateProcess (debugger.GetListener(), plugin_name).get();
246
247 if (launch_args.GetArgumentCount() == 0)
248 {
249 const Args &process_args = target->GetRunArguments();
250 if (process_args.GetArgumentCount() > 0)
251 m_options.launch_info.GetArguments().AppendArguments (process_args);
252 }
253
254 Args environment;
255 target->GetEnvironmentAsArgs (environment);
256 m_options.launch_info.GetEnvironmentEntries ().AppendArguments (environment);
257
258 if (target->GetDisableASLR())
259 m_options.launch_info.GetFlags().Set (eLaunchFlagDisableASLR);
260
261 if (m_options.launch_info.GetNumFileActions() == 0)
262 {
263 // Only use the settings value if the user hasn't specified any options that would override it.
264 if (target->GetDisableSTDIO())
265 m_options.launch_info.GetFlags().Set (eLaunchFlagDisableSTDIO);
266
267 const char *path;
268 path = target->GetStandardErrorPath();
269 if (path)
270 {
271 ProcessLaunchInfo::FileAction file_action;
272 const bool read = true;
273 const bool write = true;
274 if (file_action.Open(STDERR_FILENO, path, read, write))
275 m_options.launch_info.AppendFileAction (file_action);
276 }
277 path = target->GetStandardInputPath();
278 if (path)
279 {
280 ProcessLaunchInfo::FileAction file_action;
281 const bool read = true;
282 const bool write = false;
283 if (file_action.Open(STDIN_FILENO, path, read, write))
284 m_options.launch_info.AppendFileAction (file_action);
285 }
286
287 path = target->GetStandardOutputPath();
288 if (path)
289 {
290 ProcessLaunchInfo::FileAction file_action;
291 const bool read = false;
292 const bool write = true;
293 if (file_action.Open(STDOUT_FILENO, path, read, write))
294 m_options.launch_info.AppendFileAction (file_action);
295 }
296 }
297 error = process->Launch (m_options.launch_info);
298 }
Greg Claytona2f74232011-02-24 22:24:29 +0000299 if (process == NULL)
300 {
301 result.AppendErrorWithFormat ("Failed to find a process plugin for executable.\n");
302 result.SetStatus (eReturnStatusFailed);
303 return false;
304 }
Chris Lattner24943d22010-06-08 16:52:24 +0000305 }
Greg Claytonabb33022011-11-08 02:43:13 +0000306
Greg Clayton238c0a12010-09-18 01:14:36 +0000307 if (error.Success())
308 {
Greg Clayton940b1032011-02-23 00:35:02 +0000309 const char *archname = exe_module->GetArchitecture().GetArchitectureName();
Greg Claytonc1d37752010-10-18 01:45:30 +0000310
Greg Clayton444e35b2011-10-19 18:09:39 +0000311 result.AppendMessageWithFormat ("Process %llu launched: '%s' (%s)\n", process->GetID(), filename, archname);
Greg Claytond8c62532010-10-07 04:19:01 +0000312 result.SetDidChangeProcessState (true);
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000313 if (m_options.launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
Greg Clayton238c0a12010-09-18 01:14:36 +0000314 {
Greg Claytond8c62532010-10-07 04:19:01 +0000315 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
Greg Clayton238c0a12010-09-18 01:14:36 +0000316 StateType state = process->WaitForProcessToStop (NULL);
317
318 if (state == eStateStopped)
319 {
Greg Claytond8c62532010-10-07 04:19:01 +0000320 error = process->Resume();
321 if (error.Success())
322 {
323 bool synchronous_execution = m_interpreter.GetSynchronous ();
324 if (synchronous_execution)
325 {
326 state = process->WaitForProcessToStop (NULL);
Greg Clayton940b1032011-02-23 00:35:02 +0000327 if (!StateIsStoppedState(state))
Greg Clayton395fc332011-02-15 21:59:32 +0000328 {
329 result.AppendErrorWithFormat ("Process isn't stopped: %s", StateAsCString(state));
330 }
Greg Claytond8c62532010-10-07 04:19:01 +0000331 result.SetDidChangeProcessState (true);
332 result.SetStatus (eReturnStatusSuccessFinishResult);
333 }
334 else
335 {
336 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
337 }
338 }
Greg Clayton395fc332011-02-15 21:59:32 +0000339 else
340 {
341 result.AppendErrorWithFormat ("Process resume at entry point failed: %s", error.AsCString());
342 result.SetStatus (eReturnStatusFailed);
343 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000344 }
Greg Clayton395fc332011-02-15 21:59:32 +0000345 else
346 {
347 result.AppendErrorWithFormat ("Initial process state wasn't stopped: %s", StateAsCString(state));
348 result.SetStatus (eReturnStatusFailed);
349 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000350 }
351 }
Greg Clayton395fc332011-02-15 21:59:32 +0000352 else
353 {
Greg Claytona9eb8272011-07-02 21:07:54 +0000354 result.AppendErrorWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton395fc332011-02-15 21:59:32 +0000355 result.SetStatus (eReturnStatusFailed);
356 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000357
Chris Lattner24943d22010-06-08 16:52:24 +0000358 return result.Succeeded();
359 }
360
Jim Ingham767af882010-07-07 03:36:20 +0000361 virtual const char *GetRepeatCommand (Args &current_command_args, uint32_t index)
362 {
363 // No repeat for "process launch"...
364 return "";
365 }
366
Chris Lattner24943d22010-06-08 16:52:24 +0000367protected:
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000368 ProcessLaunchCommandOptions m_options;
Chris Lattner24943d22010-06-08 16:52:24 +0000369};
370
371
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000372//#define SET1 LLDB_OPT_SET_1
373//#define SET2 LLDB_OPT_SET_2
374//#define SET3 LLDB_OPT_SET_3
375//
376//OptionDefinition
377//CommandObjectProcessLaunch::CommandOptions::g_option_table[] =
378//{
379//{ 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."},
380//{ SET1 , false, "stdin", 'i', required_argument, NULL, 0, eArgTypePath, "Redirect stdin for the process to <path>."},
381//{ SET1 , false, "stdout", 'o', required_argument, NULL, 0, eArgTypePath, "Redirect stdout for the process to <path>."},
382//{ SET1 , false, "stderr", 'e', required_argument, NULL, 0, eArgTypePath, "Redirect stderr for the process to <path>."},
383//{ SET1 | SET2 | SET3, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
384//{ 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."},
385//{ SET3, false, "no-stdio", 'n', no_argument, NULL, 0, eArgTypeNone, "Do not set up for terminal I/O to go to running process."},
386//{ SET1 | SET2 | SET3, false, "working-dir", 'w', required_argument, NULL, 0, eArgTypePath, "Set the current working directory to <path> when running the inferior."},
387//{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
388//};
389//
390//#undef SET1
391//#undef SET2
392//#undef SET3
Chris Lattner24943d22010-06-08 16:52:24 +0000393
394//-------------------------------------------------------------------------
395// CommandObjectProcessAttach
396//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000397#pragma mark CommandObjectProcessAttach
Chris Lattner24943d22010-06-08 16:52:24 +0000398class CommandObjectProcessAttach : public CommandObject
399{
400public:
401
Chris Lattner24943d22010-06-08 16:52:24 +0000402 class CommandOptions : public Options
403 {
404 public:
405
Greg Claytonf15996e2011-04-07 22:46:35 +0000406 CommandOptions (CommandInterpreter &interpreter) :
407 Options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000408 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000409 // Keep default values of all options in one place: OptionParsingStarting ()
410 OptionParsingStarting ();
Chris Lattner24943d22010-06-08 16:52:24 +0000411 }
412
413 ~CommandOptions ()
414 {
415 }
416
417 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000418 SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +0000419 {
420 Error error;
421 char short_option = (char) m_getopt_table[option_idx].val;
422 bool success = false;
423 switch (short_option)
424 {
425 case 'p':
426 pid = Args::StringToUInt32 (option_arg, LLDB_INVALID_PROCESS_ID, 0, &success);
427 if (!success || pid == LLDB_INVALID_PROCESS_ID)
428 {
Greg Clayton9c236732011-10-26 00:56:27 +0000429 error.SetErrorStringWithFormat("invalid process ID '%s'", option_arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000430 }
431 break;
432
433 case 'P':
434 plugin_name = option_arg;
435 break;
436
437 case 'n':
438 name.assign(option_arg);
439 break;
440
441 case 'w':
442 waitfor = true;
443 break;
444
445 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000446 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner24943d22010-06-08 16:52:24 +0000447 break;
448 }
449 return error;
450 }
451
452 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000453 OptionParsingStarting ()
Chris Lattner24943d22010-06-08 16:52:24 +0000454 {
Chris Lattner24943d22010-06-08 16:52:24 +0000455 pid = LLDB_INVALID_PROCESS_ID;
456 name.clear();
457 waitfor = false;
458 }
459
Greg Claytonb3448432011-03-24 21:19:54 +0000460 const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +0000461 GetDefinitions ()
462 {
463 return g_option_table;
464 }
465
Jim Ingham7508e732010-08-09 23:31:02 +0000466 virtual bool
Greg Claytonf15996e2011-04-07 22:46:35 +0000467 HandleOptionArgumentCompletion (Args &input,
Jim Ingham7508e732010-08-09 23:31:02 +0000468 int cursor_index,
469 int char_pos,
470 OptionElementVector &opt_element_vector,
471 int opt_element_index,
472 int match_start_point,
473 int max_return_elements,
474 bool &word_complete,
475 StringList &matches)
476 {
477 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
478 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
479
480 // We are only completing the name option for now...
481
Greg Claytonb3448432011-03-24 21:19:54 +0000482 const OptionDefinition *opt_defs = GetDefinitions();
Jim Ingham7508e732010-08-09 23:31:02 +0000483 if (opt_defs[opt_defs_index].short_option == 'n')
484 {
485 // Are we in the name?
486
487 // Look to see if there is a -P argument provided, and if so use that plugin, otherwise
488 // use the default plugin.
Jim Ingham7508e732010-08-09 23:31:02 +0000489
490 const char *partial_name = NULL;
491 partial_name = input.GetArgumentAtIndex(opt_arg_pos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000492
Greg Claytonb72d0f02011-04-12 05:54:46 +0000493 PlatformSP platform_sp (m_interpreter.GetPlatform (true));
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000494 if (platform_sp)
Jim Ingham7508e732010-08-09 23:31:02 +0000495 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000496 ProcessInstanceInfoList process_infos;
497 ProcessInstanceInfoMatch match_info;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000498 if (partial_name)
499 {
500 match_info.GetProcessInfo().SetName(partial_name);
501 match_info.SetNameMatchType(eNameMatchStartsWith);
502 }
503 platform_sp->FindProcesses (match_info, process_infos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000504 const uint32_t num_matches = process_infos.GetSize();
505 if (num_matches > 0)
506 {
507 for (uint32_t i=0; i<num_matches; ++i)
508 {
509 matches.AppendString (process_infos.GetProcessNameAtIndex(i),
510 process_infos.GetProcessNameLengthAtIndex(i));
511 }
512 }
Jim Ingham7508e732010-08-09 23:31:02 +0000513 }
514 }
515
516 return false;
517 }
518
Chris Lattner24943d22010-06-08 16:52:24 +0000519 // Options table: Required for subclasses of Options.
520
Greg Claytonb3448432011-03-24 21:19:54 +0000521 static OptionDefinition g_option_table[];
Chris Lattner24943d22010-06-08 16:52:24 +0000522
523 // Instance variables to hold the values for command options.
524
525 lldb::pid_t pid;
526 std::string plugin_name;
527 std::string name;
528 bool waitfor;
529 };
530
Greg Clayton238c0a12010-09-18 01:14:36 +0000531 CommandObjectProcessAttach (CommandInterpreter &interpreter) :
532 CommandObject (interpreter,
533 "process attach",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000534 "Attach to a process.",
Greg Claytonf15996e2011-04-07 22:46:35 +0000535 "process attach <cmd-options>"),
536 m_options (interpreter)
Jim Ingham7508e732010-08-09 23:31:02 +0000537 {
Jim Ingham7508e732010-08-09 23:31:02 +0000538 }
539
540 ~CommandObjectProcessAttach ()
541 {
542 }
543
544 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000545 Execute (Args& command,
Jim Ingham7508e732010-08-09 23:31:02 +0000546 CommandReturnObject &result)
547 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000548 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Jim Inghamee940e22011-09-15 01:08:57 +0000549 // N.B. The attach should be synchronous. It doesn't help much to get the prompt back between initiating the attach
550 // and the target actually stopping. So even if the interpreter is set to be asynchronous, we wait for the stop
551 // ourselves here.
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000552
Greg Clayton567e7f32011-09-22 04:58:26 +0000553 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytona2f74232011-02-24 22:24:29 +0000554 StateType state = eStateInvalid;
Jim Ingham7508e732010-08-09 23:31:02 +0000555 if (process)
556 {
Greg Claytona2f74232011-02-24 22:24:29 +0000557 state = process->GetState();
558 if (process->IsAlive() && state != eStateConnected)
Jim Ingham7508e732010-08-09 23:31:02 +0000559 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000560 result.AppendErrorWithFormat ("Process %llu is currently being debugged, kill the process before attaching.\n",
Jim Ingham7508e732010-08-09 23:31:02 +0000561 process->GetID());
562 result.SetStatus (eReturnStatusFailed);
563 return false;
564 }
565 }
566
567 if (target == NULL)
568 {
569 // If there isn't a current target create one.
570 TargetSP new_target_sp;
571 FileSpec emptyFileSpec;
Jim Ingham7508e732010-08-09 23:31:02 +0000572 Error error;
573
Greg Clayton238c0a12010-09-18 01:14:36 +0000574 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
575 emptyFileSpec,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000576 NULL,
Greg Clayton238c0a12010-09-18 01:14:36 +0000577 false,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000578 NULL, // No platform options
Greg Clayton238c0a12010-09-18 01:14:36 +0000579 new_target_sp);
Jim Ingham7508e732010-08-09 23:31:02 +0000580 target = new_target_sp.get();
581 if (target == NULL || error.Fail())
582 {
Greg Claytone71e2582011-02-04 01:58:07 +0000583 result.AppendError(error.AsCString("Error creating target"));
Jim Ingham7508e732010-08-09 23:31:02 +0000584 return false;
585 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000586 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target);
Jim Ingham7508e732010-08-09 23:31:02 +0000587 }
588
589 // Record the old executable module, we want to issue a warning if the process of attaching changed the
590 // current executable (like somebody said "file foo" then attached to a PID whose executable was bar.)
591
592 ModuleSP old_exec_module_sp = target->GetExecutableModule();
593 ArchSpec old_arch_spec = target->GetArchitecture();
594
595 if (command.GetArgumentCount())
596 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000597 result.AppendErrorWithFormat("Invalid arguments for '%s'.\nUsage: %s\n", m_cmd_name.c_str(), m_cmd_syntax.c_str());
Jim Ingham7508e732010-08-09 23:31:02 +0000598 result.SetStatus (eReturnStatusFailed);
599 }
600 else
601 {
Greg Claytona2f74232011-02-24 22:24:29 +0000602 if (state != eStateConnected)
603 {
604 const char *plugin_name = NULL;
605
606 if (!m_options.plugin_name.empty())
607 plugin_name = m_options.plugin_name.c_str();
Jim Ingham7508e732010-08-09 23:31:02 +0000608
Greg Claytona2f74232011-02-24 22:24:29 +0000609 process = target->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name).get();
610 }
Jim Ingham7508e732010-08-09 23:31:02 +0000611
612 if (process)
613 {
614 Error error;
615 int attach_pid = m_options.pid;
616
Jim Ingham4805a1c2010-09-15 01:34:14 +0000617 const char *wait_name = NULL;
618
619 if (m_options.name.empty())
620 {
621 if (old_exec_module_sp)
622 {
623 wait_name = old_exec_module_sp->GetFileSpec().GetFilename().AsCString();
624 }
625 }
626 else
627 {
628 wait_name = m_options.name.c_str();
629 }
630
Jim Ingham7508e732010-08-09 23:31:02 +0000631 // If we are waiting for a process with this name to show up, do that first.
632 if (m_options.waitfor)
633 {
Jim Ingham4805a1c2010-09-15 01:34:14 +0000634
635 if (wait_name == NULL)
Jim Ingham7508e732010-08-09 23:31:02 +0000636 {
Jim Ingham4805a1c2010-09-15 01:34:14 +0000637 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 +0000638 result.SetStatus (eReturnStatusFailed);
639 return false;
640 }
Jim Ingham4805a1c2010-09-15 01:34:14 +0000641
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000642 result.AppendMessageWithFormat("Waiting to attach to a process named \"%s\".\n", wait_name);
Jim Ingham4805a1c2010-09-15 01:34:14 +0000643 error = process->Attach (wait_name, m_options.waitfor);
644 if (error.Success())
645 {
646 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
647 }
Jim Ingham7508e732010-08-09 23:31:02 +0000648 else
649 {
Jim Ingham4805a1c2010-09-15 01:34:14 +0000650 result.AppendErrorWithFormat ("Waiting for a process to launch named '%s': %s\n",
651 wait_name,
652 error.AsCString());
653 result.SetStatus (eReturnStatusFailed);
654 return false;
Jim Ingham7508e732010-08-09 23:31:02 +0000655 }
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000656 // If we're synchronous, wait for the stopped event and report that.
657 // Otherwise just return.
658 // FIXME: in the async case it will now be possible to get to the command
659 // interpreter with a state eStateAttaching. Make sure we handle that correctly.
Jim Inghamee940e22011-09-15 01:08:57 +0000660 StateType state = process->WaitForProcessToStop (NULL);
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000661
Jim Inghamee940e22011-09-15 01:08:57 +0000662 result.SetDidChangeProcessState (true);
Greg Clayton444e35b2011-10-19 18:09:39 +0000663 result.AppendMessageWithFormat ("Process %llu %s\n", process->GetID(), StateAsCString (state));
Jim Inghamee940e22011-09-15 01:08:57 +0000664 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Jim Ingham7508e732010-08-09 23:31:02 +0000665 }
666 else
667 {
668 // If the process was specified by name look it up, so we can warn if there are multiple
669 // processes with this pid.
670
Jim Ingham4805a1c2010-09-15 01:34:14 +0000671 if (attach_pid == LLDB_INVALID_PROCESS_ID && wait_name != NULL)
Jim Ingham7508e732010-08-09 23:31:02 +0000672 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000673 ProcessInstanceInfoList process_infos;
674 PlatformSP platform_sp (m_interpreter.GetPlatform (true));
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000675 if (platform_sp)
676 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000677 ProcessInstanceInfoMatch match_info (wait_name, eNameMatchEquals);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000678 platform_sp->FindProcesses (match_info, process_infos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000679 }
680 if (process_infos.GetSize() > 1)
Jim Ingham7508e732010-08-09 23:31:02 +0000681 {
Jim Ingham4805a1c2010-09-15 01:34:14 +0000682 result.AppendErrorWithFormat("More than one process named %s\n", wait_name);
Jim Ingham7508e732010-08-09 23:31:02 +0000683 result.SetStatus (eReturnStatusFailed);
684 return false;
685 }
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000686 else if (process_infos.GetSize() == 0)
Jim Ingham7508e732010-08-09 23:31:02 +0000687 {
Jim Ingham4805a1c2010-09-15 01:34:14 +0000688 result.AppendErrorWithFormat("Could not find a process named %s\n", wait_name);
Jim Ingham7508e732010-08-09 23:31:02 +0000689 result.SetStatus (eReturnStatusFailed);
690 return false;
691 }
692 else
693 {
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000694 attach_pid = process_infos.GetProcessIDAtIndex (0);
Jim Ingham7508e732010-08-09 23:31:02 +0000695 }
Jim Ingham7508e732010-08-09 23:31:02 +0000696 }
697
698 if (attach_pid != LLDB_INVALID_PROCESS_ID)
699 {
Greg Clayton2d9adb72011-11-12 02:10:56 +0000700 error = process->Attach (attach_pid, 0);
Jim Ingham7508e732010-08-09 23:31:02 +0000701 if (error.Success())
702 {
703 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
704 }
705 else
706 {
707 result.AppendErrorWithFormat ("Attaching to process %i failed: %s.\n",
708 attach_pid,
709 error.AsCString());
710 result.SetStatus (eReturnStatusFailed);
711 }
Jim Inghamee940e22011-09-15 01:08:57 +0000712 StateType state = process->WaitForProcessToStop (NULL);
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000713
Jim Inghamee940e22011-09-15 01:08:57 +0000714 result.SetDidChangeProcessState (true);
Greg Clayton444e35b2011-10-19 18:09:39 +0000715 result.AppendMessageWithFormat ("Process %llu %s\n", process->GetID(), StateAsCString (state));
Jim Inghamee940e22011-09-15 01:08:57 +0000716 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Jim Ingham7508e732010-08-09 23:31:02 +0000717 }
718 else
719 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000720 result.AppendErrorWithFormat ("No PID specified for attach\n");
Jim Ingham7508e732010-08-09 23:31:02 +0000721 result.SetStatus (eReturnStatusFailed);
722
723 }
724 }
725 }
726 }
727
728 if (result.Succeeded())
729 {
730 // Okay, we're done. Last step is to warn if the executable module has changed:
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000731 char new_path[PATH_MAX];
Greg Clayton5beb99d2011-08-11 02:48:45 +0000732 ModuleSP new_exec_module_sp (target->GetExecutableModule());
Jim Ingham7508e732010-08-09 23:31:02 +0000733 if (!old_exec_module_sp)
734 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000735 // We might not have a module if we attached to a raw pid...
Greg Clayton5beb99d2011-08-11 02:48:45 +0000736 if (new_exec_module_sp)
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000737 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000738 new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000739 result.AppendMessageWithFormat("Executable module set to \"%s\".\n", new_path);
740 }
Jim Ingham7508e732010-08-09 23:31:02 +0000741 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000742 else if (old_exec_module_sp->GetFileSpec() != new_exec_module_sp->GetFileSpec())
Jim Ingham7508e732010-08-09 23:31:02 +0000743 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000744 char old_path[PATH_MAX];
Jim Ingham7508e732010-08-09 23:31:02 +0000745
Greg Clayton5beb99d2011-08-11 02:48:45 +0000746 old_exec_module_sp->GetFileSpec().GetPath (old_path, PATH_MAX);
747 new_exec_module_sp->GetFileSpec().GetPath (new_path, PATH_MAX);
Jim Ingham7508e732010-08-09 23:31:02 +0000748
749 result.AppendWarningWithFormat("Executable module changed from \"%s\" to \"%s\".\n",
750 old_path, new_path);
751 }
752
753 if (!old_arch_spec.IsValid())
754 {
Greg Clayton940b1032011-02-23 00:35:02 +0000755 result.AppendMessageWithFormat ("Architecture set to: %s.\n", target->GetArchitecture().GetArchitectureName());
Jim Ingham7508e732010-08-09 23:31:02 +0000756 }
757 else if (old_arch_spec != target->GetArchitecture())
758 {
759 result.AppendWarningWithFormat("Architecture changed from %s to %s.\n",
Greg Clayton940b1032011-02-23 00:35:02 +0000760 old_arch_spec.GetArchitectureName(), target->GetArchitecture().GetArchitectureName());
Jim Ingham7508e732010-08-09 23:31:02 +0000761 }
762 }
763 return result.Succeeded();
764 }
765
766 Options *
767 GetOptions ()
768 {
769 return &m_options;
770 }
771
Chris Lattner24943d22010-06-08 16:52:24 +0000772protected:
773
774 CommandOptions m_options;
775};
776
777
Greg Claytonb3448432011-03-24 21:19:54 +0000778OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000779CommandObjectProcessAttach::CommandOptions::g_option_table[] =
780{
Caroline Tice4d6675c2010-10-01 19:59:14 +0000781{ LLDB_OPT_SET_ALL, false, "plugin", 'P', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
782{ LLDB_OPT_SET_1, false, "pid", 'p', required_argument, NULL, 0, eArgTypePid, "The process ID of an existing process to attach to."},
783{ LLDB_OPT_SET_2, false, "name", 'n', required_argument, NULL, 0, eArgTypeProcessName, "The name of the process to attach to."},
784{ LLDB_OPT_SET_2, false, "waitfor",'w', no_argument, NULL, 0, eArgTypeNone, "Wait for the the process with <process-name> to launch."},
785{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000786};
787
788//-------------------------------------------------------------------------
789// CommandObjectProcessContinue
790//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000791#pragma mark CommandObjectProcessContinue
Chris Lattner24943d22010-06-08 16:52:24 +0000792
793class CommandObjectProcessContinue : public CommandObject
794{
795public:
796
Greg Clayton238c0a12010-09-18 01:14:36 +0000797 CommandObjectProcessContinue (CommandInterpreter &interpreter) :
798 CommandObject (interpreter,
799 "process continue",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000800 "Continue execution of all threads in the current process.",
Chris Lattner24943d22010-06-08 16:52:24 +0000801 "process continue",
802 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
803 {
804 }
805
806
807 ~CommandObjectProcessContinue ()
808 {
809 }
810
811 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000812 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000813 CommandReturnObject &result)
814 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000815 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton238c0a12010-09-18 01:14:36 +0000816 bool synchronous_execution = m_interpreter.GetSynchronous ();
Chris Lattner24943d22010-06-08 16:52:24 +0000817
818 if (process == NULL)
819 {
820 result.AppendError ("no process to continue");
821 result.SetStatus (eReturnStatusFailed);
822 return false;
823 }
824
825 StateType state = process->GetState();
826 if (state == eStateStopped)
827 {
828 if (command.GetArgumentCount() != 0)
829 {
830 result.AppendErrorWithFormat ("The '%s' command does not take any arguments.\n", m_cmd_name.c_str());
831 result.SetStatus (eReturnStatusFailed);
832 return false;
833 }
834
835 const uint32_t num_threads = process->GetThreadList().GetSize();
836
837 // Set the actions that the threads should each take when resuming
838 for (uint32_t idx=0; idx<num_threads; ++idx)
839 {
840 process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState (eStateRunning);
841 }
842
843 Error error(process->Resume());
844 if (error.Success())
845 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000846 result.AppendMessageWithFormat ("Process %llu resuming\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000847 if (synchronous_execution)
848 {
Greg Claytonbef15832010-07-14 00:18:15 +0000849 state = process->WaitForProcessToStop (NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000850
851 result.SetDidChangeProcessState (true);
Greg Clayton444e35b2011-10-19 18:09:39 +0000852 result.AppendMessageWithFormat ("Process %llu %s\n", process->GetID(), StateAsCString (state));
Chris Lattner24943d22010-06-08 16:52:24 +0000853 result.SetStatus (eReturnStatusSuccessFinishNoResult);
854 }
855 else
856 {
857 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
858 }
859 }
860 else
861 {
862 result.AppendErrorWithFormat("Failed to resume process: %s.\n", error.AsCString());
863 result.SetStatus (eReturnStatusFailed);
864 }
865 }
866 else
867 {
868 result.AppendErrorWithFormat ("Process cannot be continued from its current state (%s).\n",
869 StateAsCString(state));
870 result.SetStatus (eReturnStatusFailed);
871 }
872 return result.Succeeded();
873 }
874};
875
876//-------------------------------------------------------------------------
877// CommandObjectProcessDetach
878//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000879#pragma mark CommandObjectProcessDetach
Chris Lattner24943d22010-06-08 16:52:24 +0000880
881class CommandObjectProcessDetach : public CommandObject
882{
883public:
884
Greg Clayton238c0a12010-09-18 01:14:36 +0000885 CommandObjectProcessDetach (CommandInterpreter &interpreter) :
886 CommandObject (interpreter,
887 "process detach",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000888 "Detach from the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +0000889 "process detach",
890 eFlagProcessMustBeLaunched)
891 {
892 }
893
894 ~CommandObjectProcessDetach ()
895 {
896 }
897
898 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000899 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000900 CommandReturnObject &result)
901 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000902 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000903 if (process == NULL)
904 {
905 result.AppendError ("must have a valid process in order to detach");
906 result.SetStatus (eReturnStatusFailed);
907 return false;
908 }
909
Greg Clayton444e35b2011-10-19 18:09:39 +0000910 result.AppendMessageWithFormat ("Detaching from process %llu\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000911 Error error (process->Detach());
912 if (error.Success())
913 {
914 result.SetStatus (eReturnStatusSuccessFinishResult);
915 }
916 else
917 {
918 result.AppendErrorWithFormat ("Detach failed: %s\n", error.AsCString());
919 result.SetStatus (eReturnStatusFailed);
920 return false;
921 }
922 return result.Succeeded();
923 }
924};
925
926//-------------------------------------------------------------------------
Greg Claytone71e2582011-02-04 01:58:07 +0000927// CommandObjectProcessConnect
928//-------------------------------------------------------------------------
929#pragma mark CommandObjectProcessConnect
930
931class CommandObjectProcessConnect : public CommandObject
932{
933public:
934
935 class CommandOptions : public Options
936 {
937 public:
938
Greg Claytonf15996e2011-04-07 22:46:35 +0000939 CommandOptions (CommandInterpreter &interpreter) :
940 Options(interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000941 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000942 // Keep default values of all options in one place: OptionParsingStarting ()
943 OptionParsingStarting ();
Greg Claytone71e2582011-02-04 01:58:07 +0000944 }
945
946 ~CommandOptions ()
947 {
948 }
949
950 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000951 SetOptionValue (uint32_t option_idx, const char *option_arg)
Greg Claytone71e2582011-02-04 01:58:07 +0000952 {
953 Error error;
954 char short_option = (char) m_getopt_table[option_idx].val;
955
956 switch (short_option)
957 {
958 case 'p':
959 plugin_name.assign (option_arg);
960 break;
961
962 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000963 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Greg Claytone71e2582011-02-04 01:58:07 +0000964 break;
965 }
966 return error;
967 }
968
969 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000970 OptionParsingStarting ()
Greg Claytone71e2582011-02-04 01:58:07 +0000971 {
Greg Claytone71e2582011-02-04 01:58:07 +0000972 plugin_name.clear();
973 }
974
Greg Claytonb3448432011-03-24 21:19:54 +0000975 const OptionDefinition*
Greg Claytone71e2582011-02-04 01:58:07 +0000976 GetDefinitions ()
977 {
978 return g_option_table;
979 }
980
981 // Options table: Required for subclasses of Options.
982
Greg Claytonb3448432011-03-24 21:19:54 +0000983 static OptionDefinition g_option_table[];
Greg Claytone71e2582011-02-04 01:58:07 +0000984
985 // Instance variables to hold the values for command options.
986
987 std::string plugin_name;
988 };
989
990 CommandObjectProcessConnect (CommandInterpreter &interpreter) :
Greg Claytonf15996e2011-04-07 22:46:35 +0000991 CommandObject (interpreter,
992 "process connect",
993 "Connect to a remote debug service.",
994 "process connect <remote-url>",
995 0),
996 m_options (interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000997 {
998 }
999
1000 ~CommandObjectProcessConnect ()
1001 {
1002 }
1003
1004
1005 bool
1006 Execute (Args& command,
1007 CommandReturnObject &result)
1008 {
1009
1010 TargetSP target_sp (m_interpreter.GetDebugger().GetSelectedTarget());
1011 Error error;
Greg Clayton567e7f32011-09-22 04:58:26 +00001012 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytone71e2582011-02-04 01:58:07 +00001013 if (process)
1014 {
1015 if (process->IsAlive())
1016 {
Greg Clayton444e35b2011-10-19 18:09:39 +00001017 result.AppendErrorWithFormat ("Process %llu is currently being debugged, kill the process before connecting.\n",
Greg Claytone71e2582011-02-04 01:58:07 +00001018 process->GetID());
1019 result.SetStatus (eReturnStatusFailed);
1020 return false;
1021 }
1022 }
1023
1024 if (!target_sp)
1025 {
1026 // If there isn't a current target create one.
1027 FileSpec emptyFileSpec;
Greg Claytone71e2582011-02-04 01:58:07 +00001028
1029 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
1030 emptyFileSpec,
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001031 NULL,
Greg Claytone71e2582011-02-04 01:58:07 +00001032 false,
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001033 NULL, // No platform options
Greg Claytone71e2582011-02-04 01:58:07 +00001034 target_sp);
1035 if (!target_sp || error.Fail())
1036 {
1037 result.AppendError(error.AsCString("Error creating target"));
1038 result.SetStatus (eReturnStatusFailed);
1039 return false;
1040 }
1041 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target_sp.get());
1042 }
1043
1044 if (command.GetArgumentCount() == 1)
1045 {
1046 const char *plugin_name = NULL;
1047 if (!m_options.plugin_name.empty())
1048 plugin_name = m_options.plugin_name.c_str();
1049
1050 const char *remote_url = command.GetArgumentAtIndex(0);
1051 process = target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name).get();
1052
1053 if (process)
1054 {
1055 error = process->ConnectRemote (remote_url);
1056
1057 if (error.Fail())
1058 {
1059 result.AppendError(error.AsCString("Remote connect failed"));
1060 result.SetStatus (eReturnStatusFailed);
1061 return false;
1062 }
1063 }
1064 else
1065 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001066 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",
1067 m_cmd_name.c_str());
Greg Claytone71e2582011-02-04 01:58:07 +00001068 result.SetStatus (eReturnStatusFailed);
1069 }
1070 }
1071 else
1072 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001073 result.AppendErrorWithFormat ("'%s' takes exactly one argument:\nUsage: %s\n",
Greg Claytone71e2582011-02-04 01:58:07 +00001074 m_cmd_name.c_str(),
1075 m_cmd_syntax.c_str());
1076 result.SetStatus (eReturnStatusFailed);
1077 }
1078 return result.Succeeded();
1079 }
1080
1081 Options *
1082 GetOptions ()
1083 {
1084 return &m_options;
1085 }
1086
1087protected:
1088
1089 CommandOptions m_options;
1090};
1091
1092
Greg Claytonb3448432011-03-24 21:19:54 +00001093OptionDefinition
Greg Claytone71e2582011-02-04 01:58:07 +00001094CommandObjectProcessConnect::CommandOptions::g_option_table[] =
1095{
1096 { LLDB_OPT_SET_ALL, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
1097 { 0, false, NULL, 0 , 0, NULL, 0, eArgTypeNone, NULL }
1098};
1099
1100//-------------------------------------------------------------------------
Greg Clayton0baa3942010-11-04 01:54:29 +00001101// CommandObjectProcessLoad
1102//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001103#pragma mark CommandObjectProcessLoad
Greg Clayton0baa3942010-11-04 01:54:29 +00001104
1105class CommandObjectProcessLoad : public CommandObject
1106{
1107public:
1108
1109 CommandObjectProcessLoad (CommandInterpreter &interpreter) :
1110 CommandObject (interpreter,
1111 "process load",
1112 "Load a shared library into the current process.",
1113 "process load <filename> [<filename> ...]",
1114 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
1115 {
1116 }
1117
1118 ~CommandObjectProcessLoad ()
1119 {
1120 }
1121
1122 bool
1123 Execute (Args& command,
1124 CommandReturnObject &result)
1125 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001126 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton0baa3942010-11-04 01:54:29 +00001127 if (process == NULL)
1128 {
1129 result.AppendError ("must have a valid process in order to load a shared library");
1130 result.SetStatus (eReturnStatusFailed);
1131 return false;
1132 }
1133
1134 const uint32_t argc = command.GetArgumentCount();
1135
1136 for (uint32_t i=0; i<argc; ++i)
1137 {
1138 Error error;
1139 const char *image_path = command.GetArgumentAtIndex(i);
1140 FileSpec image_spec (image_path, false);
Greg Claytonf2bf8702011-08-11 16:25:18 +00001141 process->GetTarget().GetPlatform()->ResolveRemotePath(image_spec, image_spec);
Greg Clayton0baa3942010-11-04 01:54:29 +00001142 uint32_t image_token = process->LoadImage(image_spec, error);
1143 if (image_token != LLDB_INVALID_IMAGE_TOKEN)
1144 {
1145 result.AppendMessageWithFormat ("Loading \"%s\"...ok\nImage %u loaded.\n", image_path, image_token);
1146 result.SetStatus (eReturnStatusSuccessFinishResult);
1147 }
1148 else
1149 {
1150 result.AppendErrorWithFormat ("failed to load '%s': %s", image_path, error.AsCString());
1151 result.SetStatus (eReturnStatusFailed);
1152 }
1153 }
1154 return result.Succeeded();
1155 }
1156};
1157
1158
1159//-------------------------------------------------------------------------
1160// CommandObjectProcessUnload
1161//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001162#pragma mark CommandObjectProcessUnload
Greg Clayton0baa3942010-11-04 01:54:29 +00001163
1164class CommandObjectProcessUnload : public CommandObject
1165{
1166public:
1167
1168 CommandObjectProcessUnload (CommandInterpreter &interpreter) :
1169 CommandObject (interpreter,
1170 "process unload",
1171 "Unload a shared library from the current process using the index returned by a previous call to \"process load\".",
1172 "process unload <index>",
1173 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
1174 {
1175 }
1176
1177 ~CommandObjectProcessUnload ()
1178 {
1179 }
1180
1181 bool
1182 Execute (Args& command,
1183 CommandReturnObject &result)
1184 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001185 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton0baa3942010-11-04 01:54:29 +00001186 if (process == NULL)
1187 {
1188 result.AppendError ("must have a valid process in order to load a shared library");
1189 result.SetStatus (eReturnStatusFailed);
1190 return false;
1191 }
1192
1193 const uint32_t argc = command.GetArgumentCount();
1194
1195 for (uint32_t i=0; i<argc; ++i)
1196 {
1197 const char *image_token_cstr = command.GetArgumentAtIndex(i);
1198 uint32_t image_token = Args::StringToUInt32(image_token_cstr, LLDB_INVALID_IMAGE_TOKEN, 0);
1199 if (image_token == LLDB_INVALID_IMAGE_TOKEN)
1200 {
1201 result.AppendErrorWithFormat ("invalid image index argument '%s'", image_token_cstr);
1202 result.SetStatus (eReturnStatusFailed);
1203 break;
1204 }
1205 else
1206 {
1207 Error error (process->UnloadImage(image_token));
1208 if (error.Success())
1209 {
1210 result.AppendMessageWithFormat ("Unloading shared library with index %u...ok\n", image_token);
1211 result.SetStatus (eReturnStatusSuccessFinishResult);
1212 }
1213 else
1214 {
1215 result.AppendErrorWithFormat ("failed to unload image: %s", error.AsCString());
1216 result.SetStatus (eReturnStatusFailed);
1217 break;
1218 }
1219 }
1220 }
1221 return result.Succeeded();
1222 }
1223};
1224
1225//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001226// CommandObjectProcessSignal
1227//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001228#pragma mark CommandObjectProcessSignal
Chris Lattner24943d22010-06-08 16:52:24 +00001229
1230class CommandObjectProcessSignal : public CommandObject
1231{
1232public:
1233
Greg Clayton238c0a12010-09-18 01:14:36 +00001234 CommandObjectProcessSignal (CommandInterpreter &interpreter) :
1235 CommandObject (interpreter,
1236 "process signal",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001237 "Send a UNIX signal to the current process being debugged.",
Caroline Tice43b014a2010-10-04 22:28:36 +00001238 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001239 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001240 CommandArgumentEntry arg;
1241 CommandArgumentData signal_arg;
1242
1243 // Define the first (and only) variant of this arg.
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001244 signal_arg.arg_type = eArgTypeUnixSignal;
Caroline Tice43b014a2010-10-04 22:28:36 +00001245 signal_arg.arg_repetition = eArgRepeatPlain;
1246
1247 // There is only one variant this argument could be; put it into the argument entry.
1248 arg.push_back (signal_arg);
1249
1250 // Push the data for the first argument into the m_arguments vector.
1251 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001252 }
1253
1254 ~CommandObjectProcessSignal ()
1255 {
1256 }
1257
1258 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001259 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001260 CommandReturnObject &result)
1261 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001262 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001263 if (process == NULL)
1264 {
1265 result.AppendError ("no process to signal");
1266 result.SetStatus (eReturnStatusFailed);
1267 return false;
1268 }
1269
1270 if (command.GetArgumentCount() == 1)
1271 {
Greg Clayton8f6be2a2010-10-09 01:40:57 +00001272 int signo = LLDB_INVALID_SIGNAL_NUMBER;
1273
1274 const char *signal_name = command.GetArgumentAtIndex(0);
1275 if (::isxdigit (signal_name[0]))
1276 signo = Args::StringToSInt32(signal_name, LLDB_INVALID_SIGNAL_NUMBER, 0);
1277 else
1278 signo = process->GetUnixSignals().GetSignalNumberFromName (signal_name);
1279
1280 if (signo == LLDB_INVALID_SIGNAL_NUMBER)
Chris Lattner24943d22010-06-08 16:52:24 +00001281 {
1282 result.AppendErrorWithFormat ("Invalid signal argument '%s'.\n", command.GetArgumentAtIndex(0));
1283 result.SetStatus (eReturnStatusFailed);
1284 }
1285 else
1286 {
1287 Error error (process->Signal (signo));
1288 if (error.Success())
1289 {
1290 result.SetStatus (eReturnStatusSuccessFinishResult);
1291 }
1292 else
1293 {
1294 result.AppendErrorWithFormat ("Failed to send signal %i: %s\n", signo, error.AsCString());
1295 result.SetStatus (eReturnStatusFailed);
1296 }
1297 }
1298 }
1299 else
1300 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001301 result.AppendErrorWithFormat("'%s' takes exactly one signal number argument:\nUsage: %s\n", m_cmd_name.c_str(),
Chris Lattner24943d22010-06-08 16:52:24 +00001302 m_cmd_syntax.c_str());
1303 result.SetStatus (eReturnStatusFailed);
1304 }
1305 return result.Succeeded();
1306 }
1307};
1308
1309
1310//-------------------------------------------------------------------------
1311// CommandObjectProcessInterrupt
1312//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001313#pragma mark CommandObjectProcessInterrupt
Chris Lattner24943d22010-06-08 16:52:24 +00001314
1315class CommandObjectProcessInterrupt : public CommandObject
1316{
1317public:
1318
1319
Greg Clayton238c0a12010-09-18 01:14:36 +00001320 CommandObjectProcessInterrupt (CommandInterpreter &interpreter) :
1321 CommandObject (interpreter,
1322 "process interrupt",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001323 "Interrupt the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +00001324 "process interrupt",
1325 eFlagProcessMustBeLaunched)
1326 {
1327 }
1328
1329 ~CommandObjectProcessInterrupt ()
1330 {
1331 }
1332
1333 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001334 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001335 CommandReturnObject &result)
1336 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001337 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001338 if (process == NULL)
1339 {
1340 result.AppendError ("no process to halt");
1341 result.SetStatus (eReturnStatusFailed);
1342 return false;
1343 }
1344
1345 if (command.GetArgumentCount() == 0)
1346 {
1347 Error error(process->Halt ());
1348 if (error.Success())
1349 {
1350 result.SetStatus (eReturnStatusSuccessFinishResult);
1351
1352 // Maybe we should add a "SuspendThreadPlans so we
1353 // can halt, and keep in place all the current thread plans.
1354 process->GetThreadList().DiscardThreadPlans();
1355 }
1356 else
1357 {
1358 result.AppendErrorWithFormat ("Failed to halt process: %s\n", error.AsCString());
1359 result.SetStatus (eReturnStatusFailed);
1360 }
1361 }
1362 else
1363 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001364 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner24943d22010-06-08 16:52:24 +00001365 m_cmd_name.c_str(),
1366 m_cmd_syntax.c_str());
1367 result.SetStatus (eReturnStatusFailed);
1368 }
1369 return result.Succeeded();
1370 }
1371};
1372
1373//-------------------------------------------------------------------------
1374// CommandObjectProcessKill
1375//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001376#pragma mark CommandObjectProcessKill
Chris Lattner24943d22010-06-08 16:52:24 +00001377
1378class CommandObjectProcessKill : public CommandObject
1379{
1380public:
1381
Greg Clayton238c0a12010-09-18 01:14:36 +00001382 CommandObjectProcessKill (CommandInterpreter &interpreter) :
1383 CommandObject (interpreter,
1384 "process kill",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001385 "Terminate the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +00001386 "process kill",
1387 eFlagProcessMustBeLaunched)
1388 {
1389 }
1390
1391 ~CommandObjectProcessKill ()
1392 {
1393 }
1394
1395 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001396 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001397 CommandReturnObject &result)
1398 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001399 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001400 if (process == NULL)
1401 {
1402 result.AppendError ("no process to kill");
1403 result.SetStatus (eReturnStatusFailed);
1404 return false;
1405 }
1406
1407 if (command.GetArgumentCount() == 0)
1408 {
1409 Error error (process->Destroy());
1410 if (error.Success())
1411 {
1412 result.SetStatus (eReturnStatusSuccessFinishResult);
1413 }
1414 else
1415 {
1416 result.AppendErrorWithFormat ("Failed to kill process: %s\n", error.AsCString());
1417 result.SetStatus (eReturnStatusFailed);
1418 }
1419 }
1420 else
1421 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001422 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner24943d22010-06-08 16:52:24 +00001423 m_cmd_name.c_str(),
1424 m_cmd_syntax.c_str());
1425 result.SetStatus (eReturnStatusFailed);
1426 }
1427 return result.Succeeded();
1428 }
1429};
1430
1431//-------------------------------------------------------------------------
Jim Ingham41313fc2010-06-18 01:23:09 +00001432// CommandObjectProcessStatus
1433//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001434#pragma mark CommandObjectProcessStatus
1435
Jim Ingham41313fc2010-06-18 01:23:09 +00001436class CommandObjectProcessStatus : public CommandObject
1437{
1438public:
Greg Clayton238c0a12010-09-18 01:14:36 +00001439 CommandObjectProcessStatus (CommandInterpreter &interpreter) :
1440 CommandObject (interpreter,
1441 "process status",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001442 "Show the current status and location of executing process.",
1443 "process status",
Jim Ingham41313fc2010-06-18 01:23:09 +00001444 0)
1445 {
1446 }
1447
1448 ~CommandObjectProcessStatus()
1449 {
1450 }
1451
1452
1453 bool
1454 Execute
1455 (
1456 Args& command,
Jim Ingham41313fc2010-06-18 01:23:09 +00001457 CommandReturnObject &result
1458 )
1459 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001460 Stream &strm = result.GetOutputStream();
Jim Ingham41313fc2010-06-18 01:23:09 +00001461 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Claytonb72d0f02011-04-12 05:54:46 +00001462 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
Greg Clayton567e7f32011-09-22 04:58:26 +00001463 Process *process = exe_ctx.GetProcessPtr();
1464 if (process)
Jim Ingham41313fc2010-06-18 01:23:09 +00001465 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001466 const bool only_threads_with_stop_reason = true;
1467 const uint32_t start_frame = 0;
1468 const uint32_t num_frames = 1;
1469 const uint32_t num_frames_with_source = 1;
Greg Clayton567e7f32011-09-22 04:58:26 +00001470 process->GetStatus(strm);
1471 process->GetThreadStatus (strm,
1472 only_threads_with_stop_reason,
1473 start_frame,
1474 num_frames,
1475 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001476
Jim Ingham41313fc2010-06-18 01:23:09 +00001477 }
1478 else
1479 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001480 result.AppendError ("No process.");
Jim Ingham41313fc2010-06-18 01:23:09 +00001481 result.SetStatus (eReturnStatusFailed);
1482 }
1483 return result.Succeeded();
1484 }
1485};
1486
1487//-------------------------------------------------------------------------
Caroline Tice23d6f272010-10-13 20:44:39 +00001488// CommandObjectProcessHandle
1489//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001490#pragma mark CommandObjectProcessHandle
Caroline Tice23d6f272010-10-13 20:44:39 +00001491
1492class CommandObjectProcessHandle : public CommandObject
1493{
1494public:
1495
1496 class CommandOptions : public Options
1497 {
1498 public:
1499
Greg Claytonf15996e2011-04-07 22:46:35 +00001500 CommandOptions (CommandInterpreter &interpreter) :
1501 Options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001502 {
Greg Clayton143fcc32011-04-13 00:18:08 +00001503 OptionParsingStarting ();
Caroline Tice23d6f272010-10-13 20:44:39 +00001504 }
1505
1506 ~CommandOptions ()
1507 {
1508 }
1509
1510 Error
Greg Clayton143fcc32011-04-13 00:18:08 +00001511 SetOptionValue (uint32_t option_idx, const char *option_arg)
Caroline Tice23d6f272010-10-13 20:44:39 +00001512 {
1513 Error error;
1514 char short_option = (char) m_getopt_table[option_idx].val;
1515
1516 switch (short_option)
1517 {
1518 case 's':
1519 stop = option_arg;
1520 break;
1521 case 'n':
1522 notify = option_arg;
1523 break;
1524 case 'p':
1525 pass = option_arg;
1526 break;
1527 default:
Greg Clayton9c236732011-10-26 00:56:27 +00001528 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Caroline Tice23d6f272010-10-13 20:44:39 +00001529 break;
1530 }
1531 return error;
1532 }
1533
1534 void
Greg Clayton143fcc32011-04-13 00:18:08 +00001535 OptionParsingStarting ()
Caroline Tice23d6f272010-10-13 20:44:39 +00001536 {
Caroline Tice23d6f272010-10-13 20:44:39 +00001537 stop.clear();
1538 notify.clear();
1539 pass.clear();
1540 }
1541
Greg Claytonb3448432011-03-24 21:19:54 +00001542 const OptionDefinition*
Caroline Tice23d6f272010-10-13 20:44:39 +00001543 GetDefinitions ()
1544 {
1545 return g_option_table;
1546 }
1547
1548 // Options table: Required for subclasses of Options.
1549
Greg Claytonb3448432011-03-24 21:19:54 +00001550 static OptionDefinition g_option_table[];
Caroline Tice23d6f272010-10-13 20:44:39 +00001551
1552 // Instance variables to hold the values for command options.
1553
1554 std::string stop;
1555 std::string notify;
1556 std::string pass;
1557 };
1558
1559
1560 CommandObjectProcessHandle (CommandInterpreter &interpreter) :
1561 CommandObject (interpreter,
1562 "process handle",
Caroline Ticee7471982010-10-14 21:31:13 +00001563 "Show or update what the process and debugger should do with various signals received from the OS.",
Greg Claytonf15996e2011-04-07 22:46:35 +00001564 NULL),
1565 m_options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001566 {
Caroline Ticee7471982010-10-14 21:31:13 +00001567 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 +00001568 CommandArgumentEntry arg;
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001569 CommandArgumentData signal_arg;
Caroline Tice23d6f272010-10-13 20:44:39 +00001570
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001571 signal_arg.arg_type = eArgTypeUnixSignal;
1572 signal_arg.arg_repetition = eArgRepeatStar;
Caroline Tice23d6f272010-10-13 20:44:39 +00001573
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001574 arg.push_back (signal_arg);
Caroline Tice23d6f272010-10-13 20:44:39 +00001575
1576 m_arguments.push_back (arg);
1577 }
1578
1579 ~CommandObjectProcessHandle ()
1580 {
1581 }
1582
1583 Options *
1584 GetOptions ()
1585 {
1586 return &m_options;
1587 }
1588
1589 bool
Caroline Ticee7471982010-10-14 21:31:13 +00001590 VerifyCommandOptionValue (const std::string &option, int &real_value)
Caroline Tice23d6f272010-10-13 20:44:39 +00001591 {
1592 bool okay = true;
1593
Caroline Ticee7471982010-10-14 21:31:13 +00001594 bool success = false;
1595 bool tmp_value = Args::StringToBoolean (option.c_str(), false, &success);
1596
1597 if (success && tmp_value)
1598 real_value = 1;
1599 else if (success && !tmp_value)
1600 real_value = 0;
Caroline Tice23d6f272010-10-13 20:44:39 +00001601 else
1602 {
1603 // If the value isn't 'true' or 'false', it had better be 0 or 1.
Caroline Ticee7471982010-10-14 21:31:13 +00001604 real_value = Args::StringToUInt32 (option.c_str(), 3);
1605 if (real_value != 0 && real_value != 1)
Caroline Tice23d6f272010-10-13 20:44:39 +00001606 okay = false;
1607 }
1608
1609 return okay;
1610 }
1611
Caroline Ticee7471982010-10-14 21:31:13 +00001612 void
1613 PrintSignalHeader (Stream &str)
1614 {
1615 str.Printf ("NAME PASS STOP NOTIFY\n");
1616 str.Printf ("========== ===== ===== ======\n");
1617 }
1618
1619 void
1620 PrintSignal (Stream &str, int32_t signo, const char *sig_name, UnixSignals &signals)
1621 {
1622 bool stop;
1623 bool suppress;
1624 bool notify;
1625
1626 str.Printf ("%-10s ", sig_name);
1627 if (signals.GetSignalInfo (signo, suppress, stop, notify))
1628 {
1629 bool pass = !suppress;
1630 str.Printf ("%s %s %s",
1631 (pass ? "true " : "false"),
1632 (stop ? "true " : "false"),
1633 (notify ? "true " : "false"));
1634 }
1635 str.Printf ("\n");
1636 }
1637
1638 void
1639 PrintSignalInformation (Stream &str, Args &signal_args, int num_valid_signals, UnixSignals &signals)
1640 {
1641 PrintSignalHeader (str);
1642
1643 if (num_valid_signals > 0)
1644 {
1645 size_t num_args = signal_args.GetArgumentCount();
1646 for (size_t i = 0; i < num_args; ++i)
1647 {
1648 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1649 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
1650 PrintSignal (str, signo, signal_args.GetArgumentAtIndex (i), signals);
1651 }
1652 }
1653 else // Print info for ALL signals
1654 {
1655 int32_t signo = signals.GetFirstSignalNumber();
1656 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1657 {
1658 PrintSignal (str, signo, signals.GetSignalAsCString (signo), signals);
1659 signo = signals.GetNextSignalNumber (signo);
1660 }
1661 }
1662 }
1663
Caroline Tice23d6f272010-10-13 20:44:39 +00001664 bool
1665 Execute (Args &signal_args, CommandReturnObject &result)
1666 {
1667 TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
1668
1669 if (!target_sp)
1670 {
1671 result.AppendError ("No current target;"
1672 " cannot handle signals until you have a valid target and process.\n");
1673 result.SetStatus (eReturnStatusFailed);
1674 return false;
1675 }
1676
1677 ProcessSP process_sp = target_sp->GetProcessSP();
1678
1679 if (!process_sp)
1680 {
1681 result.AppendError ("No current process; cannot handle signals until you have a valid process.\n");
1682 result.SetStatus (eReturnStatusFailed);
1683 return false;
1684 }
1685
Caroline Tice23d6f272010-10-13 20:44:39 +00001686 int stop_action = -1; // -1 means leave the current setting alone
Caroline Ticee7471982010-10-14 21:31:13 +00001687 int pass_action = -1; // -1 means leave the current setting alone
Caroline Tice23d6f272010-10-13 20:44:39 +00001688 int notify_action = -1; // -1 means leave the current setting alone
1689
1690 if (! m_options.stop.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001691 && ! VerifyCommandOptionValue (m_options.stop, stop_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001692 {
1693 result.AppendError ("Invalid argument for command option --stop; must be true or false.\n");
1694 result.SetStatus (eReturnStatusFailed);
1695 return false;
1696 }
1697
1698 if (! m_options.notify.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001699 && ! VerifyCommandOptionValue (m_options.notify, notify_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001700 {
1701 result.AppendError ("Invalid argument for command option --notify; must be true or false.\n");
1702 result.SetStatus (eReturnStatusFailed);
1703 return false;
1704 }
1705
1706 if (! m_options.pass.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001707 && ! VerifyCommandOptionValue (m_options.pass, pass_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001708 {
1709 result.AppendError ("Invalid argument for command option --pass; must be true or false.\n");
1710 result.SetStatus (eReturnStatusFailed);
1711 return false;
1712 }
1713
1714 size_t num_args = signal_args.GetArgumentCount();
1715 UnixSignals &signals = process_sp->GetUnixSignals();
1716 int num_signals_set = 0;
1717
Caroline Ticee7471982010-10-14 21:31:13 +00001718 if (num_args > 0)
Caroline Tice23d6f272010-10-13 20:44:39 +00001719 {
Caroline Ticee7471982010-10-14 21:31:13 +00001720 for (size_t i = 0; i < num_args; ++i)
Caroline Tice23d6f272010-10-13 20:44:39 +00001721 {
Caroline Ticee7471982010-10-14 21:31:13 +00001722 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1723 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
Caroline Tice23d6f272010-10-13 20:44:39 +00001724 {
Caroline Ticee7471982010-10-14 21:31:13 +00001725 // Casting the actions as bools here should be okay, because VerifyCommandOptionValue guarantees
1726 // the value is either 0 or 1.
1727 if (stop_action != -1)
1728 signals.SetShouldStop (signo, (bool) stop_action);
1729 if (pass_action != -1)
1730 {
1731 bool suppress = ! ((bool) pass_action);
1732 signals.SetShouldSuppress (signo, suppress);
1733 }
1734 if (notify_action != -1)
1735 signals.SetShouldNotify (signo, (bool) notify_action);
1736 ++num_signals_set;
Caroline Tice23d6f272010-10-13 20:44:39 +00001737 }
Caroline Ticee7471982010-10-14 21:31:13 +00001738 else
1739 {
1740 result.AppendErrorWithFormat ("Invalid signal name '%s'\n", signal_args.GetArgumentAtIndex (i));
1741 }
Caroline Tice23d6f272010-10-13 20:44:39 +00001742 }
1743 }
Caroline Ticee7471982010-10-14 21:31:13 +00001744 else
1745 {
1746 // No signal specified, if any command options were specified, update ALL signals.
1747 if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1))
1748 {
1749 if (m_interpreter.Confirm ("Do you really want to update all the signals?", false))
1750 {
1751 int32_t signo = signals.GetFirstSignalNumber();
1752 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1753 {
1754 if (notify_action != -1)
1755 signals.SetShouldNotify (signo, (bool) notify_action);
1756 if (stop_action != -1)
1757 signals.SetShouldStop (signo, (bool) stop_action);
1758 if (pass_action != -1)
1759 {
1760 bool suppress = ! ((bool) pass_action);
1761 signals.SetShouldSuppress (signo, suppress);
1762 }
1763 signo = signals.GetNextSignalNumber (signo);
1764 }
1765 }
1766 }
1767 }
1768
1769 PrintSignalInformation (result.GetOutputStream(), signal_args, num_signals_set, signals);
Caroline Tice23d6f272010-10-13 20:44:39 +00001770
1771 if (num_signals_set > 0)
1772 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1773 else
1774 result.SetStatus (eReturnStatusFailed);
1775
1776 return result.Succeeded();
1777 }
1778
1779protected:
1780
1781 CommandOptions m_options;
1782};
1783
Greg Claytonb3448432011-03-24 21:19:54 +00001784OptionDefinition
Caroline Tice23d6f272010-10-13 20:44:39 +00001785CommandObjectProcessHandle::CommandOptions::g_option_table[] =
1786{
1787{ 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." },
1788{ 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." },
1789{ LLDB_OPT_SET_1, false, "pass", 'p', required_argument, NULL, 0, eArgTypeBoolean, "Whether or not the signal should be passed to the process." },
1790{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1791};
1792
1793//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001794// CommandObjectMultiwordProcess
1795//-------------------------------------------------------------------------
1796
Greg Clayton63094e02010-06-23 01:19:29 +00001797CommandObjectMultiwordProcess::CommandObjectMultiwordProcess (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00001798 CommandObjectMultiword (interpreter,
1799 "process",
1800 "A set of commands for operating on a process.",
1801 "process <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00001802{
Greg Claytona9eb8272011-07-02 21:07:54 +00001803 LoadSubCommand ("attach", CommandObjectSP (new CommandObjectProcessAttach (interpreter)));
1804 LoadSubCommand ("launch", CommandObjectSP (new CommandObjectProcessLaunch (interpreter)));
1805 LoadSubCommand ("continue", CommandObjectSP (new CommandObjectProcessContinue (interpreter)));
1806 LoadSubCommand ("connect", CommandObjectSP (new CommandObjectProcessConnect (interpreter)));
1807 LoadSubCommand ("detach", CommandObjectSP (new CommandObjectProcessDetach (interpreter)));
1808 LoadSubCommand ("load", CommandObjectSP (new CommandObjectProcessLoad (interpreter)));
1809 LoadSubCommand ("unload", CommandObjectSP (new CommandObjectProcessUnload (interpreter)));
1810 LoadSubCommand ("signal", CommandObjectSP (new CommandObjectProcessSignal (interpreter)));
1811 LoadSubCommand ("handle", CommandObjectSP (new CommandObjectProcessHandle (interpreter)));
1812 LoadSubCommand ("status", CommandObjectSP (new CommandObjectProcessStatus (interpreter)));
Greg Clayton238c0a12010-09-18 01:14:36 +00001813 LoadSubCommand ("interrupt", CommandObjectSP (new CommandObjectProcessInterrupt (interpreter)));
Greg Claytona9eb8272011-07-02 21:07:54 +00001814 LoadSubCommand ("kill", CommandObjectSP (new CommandObjectProcessKill (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00001815}
1816
1817CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess ()
1818{
1819}
1820