blob: d618c04633b598d062ab46956d9ed80f23725f55 [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 Clayton527154d2011-11-15 03:53:30 +0000215 if (launch_args.GetArgumentCount() == 0)
216 {
217 const Args &process_args = target->GetRunArguments();
218 if (process_args.GetArgumentCount() > 0)
219 m_options.launch_info.GetArguments().AppendArguments (process_args);
220 }
221 else
Greg Claytonabb33022011-11-08 02:43:13 +0000222 {
223 m_options.launch_info.GetArguments().AppendArguments (launch_args);
224 }
Greg Claytonabb33022011-11-08 02:43:13 +0000225
Greg Clayton527154d2011-11-15 03:53:30 +0000226 if (target->GetDisableASLR())
227 m_options.launch_info.GetFlags().Set (eLaunchFlagDisableASLR);
228
229 if (target->GetDisableSTDIO())
230 m_options.launch_info.GetFlags().Set (eLaunchFlagDisableSTDIO);
231
232 m_options.launch_info.GetFlags().Set (eLaunchFlagDebug);
233
234 Args environment;
235 target->GetEnvironmentAsArgs (environment);
236 if (environment.GetArgumentCount() > 0)
237 m_options.launch_info.GetEnvironmentEntries ().AppendArguments (environment);
238
239 m_options.launch_info.FinalizeFileActions (target);
240
Greg Claytonabb33022011-11-08 02:43:13 +0000241 if (state == eStateConnected)
242 {
243 if (m_options.launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
244 {
245 result.AppendWarning("can't launch in tty when launching through a remote connection");
246 m_options.launch_info.GetFlags().Clear (eLaunchFlagLaunchInTTY);
247 }
248 }
249 else
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000250 {
Greg Clayton527154d2011-11-15 03:53:30 +0000251 if (!m_options.launch_info.GetArchitecture().IsValid())
Greg Clayton2d9adb72011-11-12 02:10:56 +0000252 m_options.launch_info.GetArchitecture() = target->GetArchitecture();
253
Greg Clayton527154d2011-11-15 03:53:30 +0000254 process = target->GetPlatform()->DebugProcess (m_options.launch_info,
255 debugger,
256 target,
257 debugger.GetListener(),
258 error).get();
Greg Claytonabb33022011-11-08 02:43:13 +0000259
Greg Claytona2f74232011-02-24 22:24:29 +0000260 if (process == NULL)
261 {
Greg Clayton527154d2011-11-15 03:53:30 +0000262 result.SetError (error, "failed to launch or debug process");
Greg Claytona2f74232011-02-24 22:24:29 +0000263 return false;
264 }
Chris Lattner24943d22010-06-08 16:52:24 +0000265 }
Greg Claytonabb33022011-11-08 02:43:13 +0000266
Greg Clayton238c0a12010-09-18 01:14:36 +0000267 if (error.Success())
268 {
Greg Clayton940b1032011-02-23 00:35:02 +0000269 const char *archname = exe_module->GetArchitecture().GetArchitectureName();
Greg Claytonc1d37752010-10-18 01:45:30 +0000270
Greg Clayton444e35b2011-10-19 18:09:39 +0000271 result.AppendMessageWithFormat ("Process %llu launched: '%s' (%s)\n", process->GetID(), filename, archname);
Greg Claytond8c62532010-10-07 04:19:01 +0000272 result.SetDidChangeProcessState (true);
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000273 if (m_options.launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
Greg Clayton238c0a12010-09-18 01:14:36 +0000274 {
Greg Claytond8c62532010-10-07 04:19:01 +0000275 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
Greg Clayton238c0a12010-09-18 01:14:36 +0000276 StateType state = process->WaitForProcessToStop (NULL);
277
278 if (state == eStateStopped)
279 {
Greg Claytond8c62532010-10-07 04:19:01 +0000280 error = process->Resume();
281 if (error.Success())
282 {
283 bool synchronous_execution = m_interpreter.GetSynchronous ();
284 if (synchronous_execution)
285 {
286 state = process->WaitForProcessToStop (NULL);
Greg Clayton20206082011-11-17 01:23:07 +0000287 const bool must_be_alive = true;
288 if (!StateIsStoppedState(state, must_be_alive))
Greg Clayton395fc332011-02-15 21:59:32 +0000289 {
Greg Clayton527154d2011-11-15 03:53:30 +0000290 result.AppendErrorWithFormat ("process isn't stopped: %s", StateAsCString(state));
Greg Clayton395fc332011-02-15 21:59:32 +0000291 }
Greg Claytond8c62532010-10-07 04:19:01 +0000292 result.SetDidChangeProcessState (true);
293 result.SetStatus (eReturnStatusSuccessFinishResult);
294 }
295 else
296 {
297 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
298 }
299 }
Greg Clayton395fc332011-02-15 21:59:32 +0000300 else
301 {
Greg Clayton527154d2011-11-15 03:53:30 +0000302 result.AppendErrorWithFormat ("process resume at entry point failed: %s", error.AsCString());
Greg Clayton395fc332011-02-15 21:59:32 +0000303 result.SetStatus (eReturnStatusFailed);
304 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000305 }
Greg Clayton395fc332011-02-15 21:59:32 +0000306 else
307 {
Greg Clayton527154d2011-11-15 03:53:30 +0000308 result.AppendErrorWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
Greg Clayton395fc332011-02-15 21:59:32 +0000309 result.SetStatus (eReturnStatusFailed);
310 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000311 }
312 }
Greg Clayton395fc332011-02-15 21:59:32 +0000313 else
314 {
Greg Claytona9eb8272011-07-02 21:07:54 +0000315 result.AppendErrorWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton395fc332011-02-15 21:59:32 +0000316 result.SetStatus (eReturnStatusFailed);
317 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000318
Chris Lattner24943d22010-06-08 16:52:24 +0000319 return result.Succeeded();
320 }
321
Jim Ingham767af882010-07-07 03:36:20 +0000322 virtual const char *GetRepeatCommand (Args &current_command_args, uint32_t index)
323 {
324 // No repeat for "process launch"...
325 return "";
326 }
327
Chris Lattner24943d22010-06-08 16:52:24 +0000328protected:
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000329 ProcessLaunchCommandOptions m_options;
Chris Lattner24943d22010-06-08 16:52:24 +0000330};
331
332
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000333//#define SET1 LLDB_OPT_SET_1
334//#define SET2 LLDB_OPT_SET_2
335//#define SET3 LLDB_OPT_SET_3
336//
337//OptionDefinition
338//CommandObjectProcessLaunch::CommandOptions::g_option_table[] =
339//{
340//{ 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."},
341//{ SET1 , false, "stdin", 'i', required_argument, NULL, 0, eArgTypePath, "Redirect stdin for the process to <path>."},
342//{ SET1 , false, "stdout", 'o', required_argument, NULL, 0, eArgTypePath, "Redirect stdout for the process to <path>."},
343//{ SET1 , false, "stderr", 'e', required_argument, NULL, 0, eArgTypePath, "Redirect stderr for the process to <path>."},
344//{ SET1 | SET2 | SET3, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
345//{ 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."},
346//{ SET3, false, "no-stdio", 'n', no_argument, NULL, 0, eArgTypeNone, "Do not set up for terminal I/O to go to running process."},
347//{ SET1 | SET2 | SET3, false, "working-dir", 'w', required_argument, NULL, 0, eArgTypePath, "Set the current working directory to <path> when running the inferior."},
348//{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
349//};
350//
351//#undef SET1
352//#undef SET2
353//#undef SET3
Chris Lattner24943d22010-06-08 16:52:24 +0000354
355//-------------------------------------------------------------------------
356// CommandObjectProcessAttach
357//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000358#pragma mark CommandObjectProcessAttach
Chris Lattner24943d22010-06-08 16:52:24 +0000359class CommandObjectProcessAttach : public CommandObject
360{
361public:
362
Chris Lattner24943d22010-06-08 16:52:24 +0000363 class CommandOptions : public Options
364 {
365 public:
366
Greg Claytonf15996e2011-04-07 22:46:35 +0000367 CommandOptions (CommandInterpreter &interpreter) :
368 Options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000369 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000370 // Keep default values of all options in one place: OptionParsingStarting ()
371 OptionParsingStarting ();
Chris Lattner24943d22010-06-08 16:52:24 +0000372 }
373
374 ~CommandOptions ()
375 {
376 }
377
378 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000379 SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +0000380 {
381 Error error;
382 char short_option = (char) m_getopt_table[option_idx].val;
383 bool success = false;
384 switch (short_option)
385 {
386 case 'p':
Chris Lattner24943d22010-06-08 16:52:24 +0000387 {
Greg Clayton527154d2011-11-15 03:53:30 +0000388 lldb::pid_t pid = Args::StringToUInt32 (option_arg, LLDB_INVALID_PROCESS_ID, 0, &success);
389 if (!success || pid == LLDB_INVALID_PROCESS_ID)
390 {
391 error.SetErrorStringWithFormat("invalid process ID '%s'", option_arg);
392 }
393 else
394 {
395 attach_info.SetProcessID (pid);
396 }
Chris Lattner24943d22010-06-08 16:52:24 +0000397 }
398 break;
399
400 case 'P':
Greg Clayton527154d2011-11-15 03:53:30 +0000401 attach_info.SetProcessPluginName (option_arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000402 break;
403
404 case 'n':
Greg Clayton527154d2011-11-15 03:53:30 +0000405 attach_info.GetExecutableFile().SetFile(option_arg, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000406 break;
407
408 case 'w':
Greg Clayton527154d2011-11-15 03:53:30 +0000409 attach_info.SetWaitForLaunch(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000410 break;
411
412 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000413 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner24943d22010-06-08 16:52:24 +0000414 break;
415 }
416 return error;
417 }
418
419 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000420 OptionParsingStarting ()
Chris Lattner24943d22010-06-08 16:52:24 +0000421 {
Greg Clayton527154d2011-11-15 03:53:30 +0000422 attach_info.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000423 }
424
Greg Claytonb3448432011-03-24 21:19:54 +0000425 const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +0000426 GetDefinitions ()
427 {
428 return g_option_table;
429 }
430
Jim Ingham7508e732010-08-09 23:31:02 +0000431 virtual bool
Greg Claytonf15996e2011-04-07 22:46:35 +0000432 HandleOptionArgumentCompletion (Args &input,
Jim Ingham7508e732010-08-09 23:31:02 +0000433 int cursor_index,
434 int char_pos,
435 OptionElementVector &opt_element_vector,
436 int opt_element_index,
437 int match_start_point,
438 int max_return_elements,
439 bool &word_complete,
440 StringList &matches)
441 {
442 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
443 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
444
445 // We are only completing the name option for now...
446
Greg Claytonb3448432011-03-24 21:19:54 +0000447 const OptionDefinition *opt_defs = GetDefinitions();
Jim Ingham7508e732010-08-09 23:31:02 +0000448 if (opt_defs[opt_defs_index].short_option == 'n')
449 {
450 // Are we in the name?
451
452 // Look to see if there is a -P argument provided, and if so use that plugin, otherwise
453 // use the default plugin.
Jim Ingham7508e732010-08-09 23:31:02 +0000454
455 const char *partial_name = NULL;
456 partial_name = input.GetArgumentAtIndex(opt_arg_pos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000457
Greg Claytonb72d0f02011-04-12 05:54:46 +0000458 PlatformSP platform_sp (m_interpreter.GetPlatform (true));
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000459 if (platform_sp)
Jim Ingham7508e732010-08-09 23:31:02 +0000460 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000461 ProcessInstanceInfoList process_infos;
462 ProcessInstanceInfoMatch match_info;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000463 if (partial_name)
464 {
Greg Clayton527154d2011-11-15 03:53:30 +0000465 match_info.GetProcessInfo().GetExecutableFile().SetFile(partial_name, false);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000466 match_info.SetNameMatchType(eNameMatchStartsWith);
467 }
468 platform_sp->FindProcesses (match_info, process_infos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000469 const uint32_t num_matches = process_infos.GetSize();
470 if (num_matches > 0)
471 {
472 for (uint32_t i=0; i<num_matches; ++i)
473 {
474 matches.AppendString (process_infos.GetProcessNameAtIndex(i),
475 process_infos.GetProcessNameLengthAtIndex(i));
476 }
477 }
Jim Ingham7508e732010-08-09 23:31:02 +0000478 }
479 }
480
481 return false;
482 }
483
Chris Lattner24943d22010-06-08 16:52:24 +0000484 // Options table: Required for subclasses of Options.
485
Greg Claytonb3448432011-03-24 21:19:54 +0000486 static OptionDefinition g_option_table[];
Chris Lattner24943d22010-06-08 16:52:24 +0000487
488 // Instance variables to hold the values for command options.
489
Greg Clayton527154d2011-11-15 03:53:30 +0000490 ProcessAttachInfo attach_info;
Chris Lattner24943d22010-06-08 16:52:24 +0000491 };
492
Greg Clayton238c0a12010-09-18 01:14:36 +0000493 CommandObjectProcessAttach (CommandInterpreter &interpreter) :
494 CommandObject (interpreter,
495 "process attach",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000496 "Attach to a process.",
Greg Claytonf15996e2011-04-07 22:46:35 +0000497 "process attach <cmd-options>"),
498 m_options (interpreter)
Jim Ingham7508e732010-08-09 23:31:02 +0000499 {
Jim Ingham7508e732010-08-09 23:31:02 +0000500 }
501
502 ~CommandObjectProcessAttach ()
503 {
504 }
505
506 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000507 Execute (Args& command,
Jim Ingham7508e732010-08-09 23:31:02 +0000508 CommandReturnObject &result)
509 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000510 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Jim Inghamee940e22011-09-15 01:08:57 +0000511 // N.B. The attach should be synchronous. It doesn't help much to get the prompt back between initiating the attach
512 // and the target actually stopping. So even if the interpreter is set to be asynchronous, we wait for the stop
513 // ourselves here.
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000514
Greg Clayton567e7f32011-09-22 04:58:26 +0000515 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytona2f74232011-02-24 22:24:29 +0000516 StateType state = eStateInvalid;
Jim Ingham7508e732010-08-09 23:31:02 +0000517 if (process)
518 {
Greg Claytona2f74232011-02-24 22:24:29 +0000519 state = process->GetState();
520 if (process->IsAlive() && state != eStateConnected)
Jim Ingham7508e732010-08-09 23:31:02 +0000521 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000522 result.AppendErrorWithFormat ("Process %llu is currently being debugged, kill the process before attaching.\n",
Jim Ingham7508e732010-08-09 23:31:02 +0000523 process->GetID());
524 result.SetStatus (eReturnStatusFailed);
525 return false;
526 }
527 }
528
529 if (target == NULL)
530 {
531 // If there isn't a current target create one.
532 TargetSP new_target_sp;
533 FileSpec emptyFileSpec;
Jim Ingham7508e732010-08-09 23:31:02 +0000534 Error error;
535
Greg Clayton238c0a12010-09-18 01:14:36 +0000536 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
537 emptyFileSpec,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000538 NULL,
Greg Clayton238c0a12010-09-18 01:14:36 +0000539 false,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000540 NULL, // No platform options
Greg Clayton238c0a12010-09-18 01:14:36 +0000541 new_target_sp);
Jim Ingham7508e732010-08-09 23:31:02 +0000542 target = new_target_sp.get();
543 if (target == NULL || error.Fail())
544 {
Greg Claytone71e2582011-02-04 01:58:07 +0000545 result.AppendError(error.AsCString("Error creating target"));
Jim Ingham7508e732010-08-09 23:31:02 +0000546 return false;
547 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000548 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target);
Jim Ingham7508e732010-08-09 23:31:02 +0000549 }
550
551 // Record the old executable module, we want to issue a warning if the process of attaching changed the
552 // current executable (like somebody said "file foo" then attached to a PID whose executable was bar.)
553
554 ModuleSP old_exec_module_sp = target->GetExecutableModule();
555 ArchSpec old_arch_spec = target->GetArchitecture();
556
557 if (command.GetArgumentCount())
558 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000559 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 +0000560 result.SetStatus (eReturnStatusFailed);
561 }
562 else
563 {
Greg Claytona2f74232011-02-24 22:24:29 +0000564 if (state != eStateConnected)
565 {
Greg Clayton527154d2011-11-15 03:53:30 +0000566 const char *plugin_name = m_options.attach_info.GetProcessPluginName();
Greg Claytona2f74232011-02-24 22:24:29 +0000567 process = target->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name).get();
568 }
Jim Ingham7508e732010-08-09 23:31:02 +0000569
570 if (process)
571 {
572 Error error;
Greg Clayton527154d2011-11-15 03:53:30 +0000573 // If no process info was specified, then use the target executable
574 // name as the process to attach to by default
575 if (!m_options.attach_info.ProcessInfoSpecified ())
Jim Ingham4805a1c2010-09-15 01:34:14 +0000576 {
577 if (old_exec_module_sp)
Greg Clayton527154d2011-11-15 03:53:30 +0000578 m_options.attach_info.GetExecutableFile().GetFilename() = old_exec_module_sp->GetFileSpec().GetFilename();
Jim Ingham4805a1c2010-09-15 01:34:14 +0000579
Greg Clayton527154d2011-11-15 03:53:30 +0000580 if (!m_options.attach_info.ProcessInfoSpecified ())
581 {
582 error.SetErrorString ("no process specified, create a target with a file, or specify the --pid or --name command option");
583 }
584 }
585
586 if (error.Success())
587 {
588 error = process->Attach (m_options.attach_info);
589
Jim Ingham4805a1c2010-09-15 01:34:14 +0000590 if (error.Success())
591 {
592 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
593 }
Jim Ingham7508e732010-08-09 23:31:02 +0000594 else
595 {
Greg Clayton527154d2011-11-15 03:53:30 +0000596 result.AppendErrorWithFormat ("attach failed: %s\n", error.AsCString());
Jim Ingham4805a1c2010-09-15 01:34:14 +0000597 result.SetStatus (eReturnStatusFailed);
598 return false;
Jim Ingham7508e732010-08-09 23:31:02 +0000599 }
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000600 // If we're synchronous, wait for the stopped event and report that.
601 // Otherwise just return.
602 // FIXME: in the async case it will now be possible to get to the command
603 // interpreter with a state eStateAttaching. Make sure we handle that correctly.
Jim Inghamee940e22011-09-15 01:08:57 +0000604 StateType state = process->WaitForProcessToStop (NULL);
Greg Clayton527154d2011-11-15 03:53:30 +0000605
Jim Inghamee940e22011-09-15 01:08:57 +0000606 result.SetDidChangeProcessState (true);
Greg Clayton444e35b2011-10-19 18:09:39 +0000607 result.AppendMessageWithFormat ("Process %llu %s\n", process->GetID(), StateAsCString (state));
Jim Inghamee940e22011-09-15 01:08:57 +0000608 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Jim Ingham7508e732010-08-09 23:31:02 +0000609 }
Jim Ingham7508e732010-08-09 23:31:02 +0000610 }
611 }
612
613 if (result.Succeeded())
614 {
615 // Okay, we're done. Last step is to warn if the executable module has changed:
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000616 char new_path[PATH_MAX];
Greg Clayton5beb99d2011-08-11 02:48:45 +0000617 ModuleSP new_exec_module_sp (target->GetExecutableModule());
Jim Ingham7508e732010-08-09 23:31:02 +0000618 if (!old_exec_module_sp)
619 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000620 // We might not have a module if we attached to a raw pid...
Greg Clayton5beb99d2011-08-11 02:48:45 +0000621 if (new_exec_module_sp)
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000622 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000623 new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000624 result.AppendMessageWithFormat("Executable module set to \"%s\".\n", new_path);
625 }
Jim Ingham7508e732010-08-09 23:31:02 +0000626 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000627 else if (old_exec_module_sp->GetFileSpec() != new_exec_module_sp->GetFileSpec())
Jim Ingham7508e732010-08-09 23:31:02 +0000628 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000629 char old_path[PATH_MAX];
Jim Ingham7508e732010-08-09 23:31:02 +0000630
Greg Clayton5beb99d2011-08-11 02:48:45 +0000631 old_exec_module_sp->GetFileSpec().GetPath (old_path, PATH_MAX);
632 new_exec_module_sp->GetFileSpec().GetPath (new_path, PATH_MAX);
Jim Ingham7508e732010-08-09 23:31:02 +0000633
634 result.AppendWarningWithFormat("Executable module changed from \"%s\" to \"%s\".\n",
635 old_path, new_path);
636 }
637
638 if (!old_arch_spec.IsValid())
639 {
Greg Clayton940b1032011-02-23 00:35:02 +0000640 result.AppendMessageWithFormat ("Architecture set to: %s.\n", target->GetArchitecture().GetArchitectureName());
Jim Ingham7508e732010-08-09 23:31:02 +0000641 }
642 else if (old_arch_spec != target->GetArchitecture())
643 {
644 result.AppendWarningWithFormat("Architecture changed from %s to %s.\n",
Greg Clayton940b1032011-02-23 00:35:02 +0000645 old_arch_spec.GetArchitectureName(), target->GetArchitecture().GetArchitectureName());
Jim Ingham7508e732010-08-09 23:31:02 +0000646 }
647 }
648 return result.Succeeded();
649 }
650
651 Options *
652 GetOptions ()
653 {
654 return &m_options;
655 }
656
Chris Lattner24943d22010-06-08 16:52:24 +0000657protected:
658
659 CommandOptions m_options;
660};
661
662
Greg Claytonb3448432011-03-24 21:19:54 +0000663OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000664CommandObjectProcessAttach::CommandOptions::g_option_table[] =
665{
Caroline Tice4d6675c2010-10-01 19:59:14 +0000666{ LLDB_OPT_SET_ALL, false, "plugin", 'P', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
667{ LLDB_OPT_SET_1, false, "pid", 'p', required_argument, NULL, 0, eArgTypePid, "The process ID of an existing process to attach to."},
668{ LLDB_OPT_SET_2, false, "name", 'n', required_argument, NULL, 0, eArgTypeProcessName, "The name of the process to attach to."},
669{ LLDB_OPT_SET_2, false, "waitfor",'w', no_argument, NULL, 0, eArgTypeNone, "Wait for the the process with <process-name> to launch."},
670{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000671};
672
673//-------------------------------------------------------------------------
674// CommandObjectProcessContinue
675//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000676#pragma mark CommandObjectProcessContinue
Chris Lattner24943d22010-06-08 16:52:24 +0000677
678class CommandObjectProcessContinue : public CommandObject
679{
680public:
681
Greg Clayton238c0a12010-09-18 01:14:36 +0000682 CommandObjectProcessContinue (CommandInterpreter &interpreter) :
683 CommandObject (interpreter,
684 "process continue",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000685 "Continue execution of all threads in the current process.",
Chris Lattner24943d22010-06-08 16:52:24 +0000686 "process continue",
687 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
688 {
689 }
690
691
692 ~CommandObjectProcessContinue ()
693 {
694 }
695
696 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000697 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000698 CommandReturnObject &result)
699 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000700 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton238c0a12010-09-18 01:14:36 +0000701 bool synchronous_execution = m_interpreter.GetSynchronous ();
Chris Lattner24943d22010-06-08 16:52:24 +0000702
703 if (process == NULL)
704 {
705 result.AppendError ("no process to continue");
706 result.SetStatus (eReturnStatusFailed);
707 return false;
708 }
709
710 StateType state = process->GetState();
711 if (state == eStateStopped)
712 {
713 if (command.GetArgumentCount() != 0)
714 {
715 result.AppendErrorWithFormat ("The '%s' command does not take any arguments.\n", m_cmd_name.c_str());
716 result.SetStatus (eReturnStatusFailed);
717 return false;
718 }
719
720 const uint32_t num_threads = process->GetThreadList().GetSize();
721
722 // Set the actions that the threads should each take when resuming
723 for (uint32_t idx=0; idx<num_threads; ++idx)
724 {
725 process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState (eStateRunning);
726 }
727
728 Error error(process->Resume());
729 if (error.Success())
730 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000731 result.AppendMessageWithFormat ("Process %llu resuming\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000732 if (synchronous_execution)
733 {
Greg Claytonbef15832010-07-14 00:18:15 +0000734 state = process->WaitForProcessToStop (NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000735
736 result.SetDidChangeProcessState (true);
Greg Clayton444e35b2011-10-19 18:09:39 +0000737 result.AppendMessageWithFormat ("Process %llu %s\n", process->GetID(), StateAsCString (state));
Chris Lattner24943d22010-06-08 16:52:24 +0000738 result.SetStatus (eReturnStatusSuccessFinishNoResult);
739 }
740 else
741 {
742 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
743 }
744 }
745 else
746 {
747 result.AppendErrorWithFormat("Failed to resume process: %s.\n", error.AsCString());
748 result.SetStatus (eReturnStatusFailed);
749 }
750 }
751 else
752 {
753 result.AppendErrorWithFormat ("Process cannot be continued from its current state (%s).\n",
754 StateAsCString(state));
755 result.SetStatus (eReturnStatusFailed);
756 }
757 return result.Succeeded();
758 }
759};
760
761//-------------------------------------------------------------------------
762// CommandObjectProcessDetach
763//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000764#pragma mark CommandObjectProcessDetach
Chris Lattner24943d22010-06-08 16:52:24 +0000765
766class CommandObjectProcessDetach : public CommandObject
767{
768public:
769
Greg Clayton238c0a12010-09-18 01:14:36 +0000770 CommandObjectProcessDetach (CommandInterpreter &interpreter) :
771 CommandObject (interpreter,
772 "process detach",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000773 "Detach from the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +0000774 "process detach",
775 eFlagProcessMustBeLaunched)
776 {
777 }
778
779 ~CommandObjectProcessDetach ()
780 {
781 }
782
783 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000784 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000785 CommandReturnObject &result)
786 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000787 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000788 if (process == NULL)
789 {
790 result.AppendError ("must have a valid process in order to detach");
791 result.SetStatus (eReturnStatusFailed);
792 return false;
793 }
794
Greg Clayton444e35b2011-10-19 18:09:39 +0000795 result.AppendMessageWithFormat ("Detaching from process %llu\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000796 Error error (process->Detach());
797 if (error.Success())
798 {
799 result.SetStatus (eReturnStatusSuccessFinishResult);
800 }
801 else
802 {
803 result.AppendErrorWithFormat ("Detach failed: %s\n", error.AsCString());
804 result.SetStatus (eReturnStatusFailed);
805 return false;
806 }
807 return result.Succeeded();
808 }
809};
810
811//-------------------------------------------------------------------------
Greg Claytone71e2582011-02-04 01:58:07 +0000812// CommandObjectProcessConnect
813//-------------------------------------------------------------------------
814#pragma mark CommandObjectProcessConnect
815
816class CommandObjectProcessConnect : public CommandObject
817{
818public:
819
820 class CommandOptions : public Options
821 {
822 public:
823
Greg Claytonf15996e2011-04-07 22:46:35 +0000824 CommandOptions (CommandInterpreter &interpreter) :
825 Options(interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000826 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000827 // Keep default values of all options in one place: OptionParsingStarting ()
828 OptionParsingStarting ();
Greg Claytone71e2582011-02-04 01:58:07 +0000829 }
830
831 ~CommandOptions ()
832 {
833 }
834
835 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000836 SetOptionValue (uint32_t option_idx, const char *option_arg)
Greg Claytone71e2582011-02-04 01:58:07 +0000837 {
838 Error error;
839 char short_option = (char) m_getopt_table[option_idx].val;
840
841 switch (short_option)
842 {
843 case 'p':
844 plugin_name.assign (option_arg);
845 break;
846
847 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000848 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Greg Claytone71e2582011-02-04 01:58:07 +0000849 break;
850 }
851 return error;
852 }
853
854 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000855 OptionParsingStarting ()
Greg Claytone71e2582011-02-04 01:58:07 +0000856 {
Greg Claytone71e2582011-02-04 01:58:07 +0000857 plugin_name.clear();
858 }
859
Greg Claytonb3448432011-03-24 21:19:54 +0000860 const OptionDefinition*
Greg Claytone71e2582011-02-04 01:58:07 +0000861 GetDefinitions ()
862 {
863 return g_option_table;
864 }
865
866 // Options table: Required for subclasses of Options.
867
Greg Claytonb3448432011-03-24 21:19:54 +0000868 static OptionDefinition g_option_table[];
Greg Claytone71e2582011-02-04 01:58:07 +0000869
870 // Instance variables to hold the values for command options.
871
872 std::string plugin_name;
873 };
874
875 CommandObjectProcessConnect (CommandInterpreter &interpreter) :
Greg Claytonf15996e2011-04-07 22:46:35 +0000876 CommandObject (interpreter,
877 "process connect",
878 "Connect to a remote debug service.",
879 "process connect <remote-url>",
880 0),
881 m_options (interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000882 {
883 }
884
885 ~CommandObjectProcessConnect ()
886 {
887 }
888
889
890 bool
891 Execute (Args& command,
892 CommandReturnObject &result)
893 {
894
895 TargetSP target_sp (m_interpreter.GetDebugger().GetSelectedTarget());
896 Error error;
Greg Clayton567e7f32011-09-22 04:58:26 +0000897 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytone71e2582011-02-04 01:58:07 +0000898 if (process)
899 {
900 if (process->IsAlive())
901 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000902 result.AppendErrorWithFormat ("Process %llu is currently being debugged, kill the process before connecting.\n",
Greg Claytone71e2582011-02-04 01:58:07 +0000903 process->GetID());
904 result.SetStatus (eReturnStatusFailed);
905 return false;
906 }
907 }
908
909 if (!target_sp)
910 {
911 // If there isn't a current target create one.
912 FileSpec emptyFileSpec;
Greg Claytone71e2582011-02-04 01:58:07 +0000913
914 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
915 emptyFileSpec,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000916 NULL,
Greg Claytone71e2582011-02-04 01:58:07 +0000917 false,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000918 NULL, // No platform options
Greg Claytone71e2582011-02-04 01:58:07 +0000919 target_sp);
920 if (!target_sp || error.Fail())
921 {
922 result.AppendError(error.AsCString("Error creating target"));
923 result.SetStatus (eReturnStatusFailed);
924 return false;
925 }
926 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target_sp.get());
927 }
928
929 if (command.GetArgumentCount() == 1)
930 {
931 const char *plugin_name = NULL;
932 if (!m_options.plugin_name.empty())
933 plugin_name = m_options.plugin_name.c_str();
934
935 const char *remote_url = command.GetArgumentAtIndex(0);
936 process = target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name).get();
937
938 if (process)
939 {
940 error = process->ConnectRemote (remote_url);
941
942 if (error.Fail())
943 {
944 result.AppendError(error.AsCString("Remote connect failed"));
945 result.SetStatus (eReturnStatusFailed);
946 return false;
947 }
948 }
949 else
950 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000951 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",
952 m_cmd_name.c_str());
Greg Claytone71e2582011-02-04 01:58:07 +0000953 result.SetStatus (eReturnStatusFailed);
954 }
955 }
956 else
957 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000958 result.AppendErrorWithFormat ("'%s' takes exactly one argument:\nUsage: %s\n",
Greg Claytone71e2582011-02-04 01:58:07 +0000959 m_cmd_name.c_str(),
960 m_cmd_syntax.c_str());
961 result.SetStatus (eReturnStatusFailed);
962 }
963 return result.Succeeded();
964 }
965
966 Options *
967 GetOptions ()
968 {
969 return &m_options;
970 }
971
972protected:
973
974 CommandOptions m_options;
975};
976
977
Greg Claytonb3448432011-03-24 21:19:54 +0000978OptionDefinition
Greg Claytone71e2582011-02-04 01:58:07 +0000979CommandObjectProcessConnect::CommandOptions::g_option_table[] =
980{
981 { LLDB_OPT_SET_ALL, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
982 { 0, false, NULL, 0 , 0, NULL, 0, eArgTypeNone, NULL }
983};
984
985//-------------------------------------------------------------------------
Greg Clayton0baa3942010-11-04 01:54:29 +0000986// CommandObjectProcessLoad
987//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000988#pragma mark CommandObjectProcessLoad
Greg Clayton0baa3942010-11-04 01:54:29 +0000989
990class CommandObjectProcessLoad : public CommandObject
991{
992public:
993
994 CommandObjectProcessLoad (CommandInterpreter &interpreter) :
995 CommandObject (interpreter,
996 "process load",
997 "Load a shared library into the current process.",
998 "process load <filename> [<filename> ...]",
999 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
1000 {
1001 }
1002
1003 ~CommandObjectProcessLoad ()
1004 {
1005 }
1006
1007 bool
1008 Execute (Args& command,
1009 CommandReturnObject &result)
1010 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001011 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton0baa3942010-11-04 01:54:29 +00001012 if (process == NULL)
1013 {
1014 result.AppendError ("must have a valid process in order to load a shared library");
1015 result.SetStatus (eReturnStatusFailed);
1016 return false;
1017 }
1018
1019 const uint32_t argc = command.GetArgumentCount();
1020
1021 for (uint32_t i=0; i<argc; ++i)
1022 {
1023 Error error;
1024 const char *image_path = command.GetArgumentAtIndex(i);
1025 FileSpec image_spec (image_path, false);
Greg Claytonf2bf8702011-08-11 16:25:18 +00001026 process->GetTarget().GetPlatform()->ResolveRemotePath(image_spec, image_spec);
Greg Clayton0baa3942010-11-04 01:54:29 +00001027 uint32_t image_token = process->LoadImage(image_spec, error);
1028 if (image_token != LLDB_INVALID_IMAGE_TOKEN)
1029 {
1030 result.AppendMessageWithFormat ("Loading \"%s\"...ok\nImage %u loaded.\n", image_path, image_token);
1031 result.SetStatus (eReturnStatusSuccessFinishResult);
1032 }
1033 else
1034 {
1035 result.AppendErrorWithFormat ("failed to load '%s': %s", image_path, error.AsCString());
1036 result.SetStatus (eReturnStatusFailed);
1037 }
1038 }
1039 return result.Succeeded();
1040 }
1041};
1042
1043
1044//-------------------------------------------------------------------------
1045// CommandObjectProcessUnload
1046//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001047#pragma mark CommandObjectProcessUnload
Greg Clayton0baa3942010-11-04 01:54:29 +00001048
1049class CommandObjectProcessUnload : public CommandObject
1050{
1051public:
1052
1053 CommandObjectProcessUnload (CommandInterpreter &interpreter) :
1054 CommandObject (interpreter,
1055 "process unload",
1056 "Unload a shared library from the current process using the index returned by a previous call to \"process load\".",
1057 "process unload <index>",
1058 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
1059 {
1060 }
1061
1062 ~CommandObjectProcessUnload ()
1063 {
1064 }
1065
1066 bool
1067 Execute (Args& command,
1068 CommandReturnObject &result)
1069 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001070 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton0baa3942010-11-04 01:54:29 +00001071 if (process == NULL)
1072 {
1073 result.AppendError ("must have a valid process in order to load a shared library");
1074 result.SetStatus (eReturnStatusFailed);
1075 return false;
1076 }
1077
1078 const uint32_t argc = command.GetArgumentCount();
1079
1080 for (uint32_t i=0; i<argc; ++i)
1081 {
1082 const char *image_token_cstr = command.GetArgumentAtIndex(i);
1083 uint32_t image_token = Args::StringToUInt32(image_token_cstr, LLDB_INVALID_IMAGE_TOKEN, 0);
1084 if (image_token == LLDB_INVALID_IMAGE_TOKEN)
1085 {
1086 result.AppendErrorWithFormat ("invalid image index argument '%s'", image_token_cstr);
1087 result.SetStatus (eReturnStatusFailed);
1088 break;
1089 }
1090 else
1091 {
1092 Error error (process->UnloadImage(image_token));
1093 if (error.Success())
1094 {
1095 result.AppendMessageWithFormat ("Unloading shared library with index %u...ok\n", image_token);
1096 result.SetStatus (eReturnStatusSuccessFinishResult);
1097 }
1098 else
1099 {
1100 result.AppendErrorWithFormat ("failed to unload image: %s", error.AsCString());
1101 result.SetStatus (eReturnStatusFailed);
1102 break;
1103 }
1104 }
1105 }
1106 return result.Succeeded();
1107 }
1108};
1109
1110//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001111// CommandObjectProcessSignal
1112//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001113#pragma mark CommandObjectProcessSignal
Chris Lattner24943d22010-06-08 16:52:24 +00001114
1115class CommandObjectProcessSignal : public CommandObject
1116{
1117public:
1118
Greg Clayton238c0a12010-09-18 01:14:36 +00001119 CommandObjectProcessSignal (CommandInterpreter &interpreter) :
1120 CommandObject (interpreter,
1121 "process signal",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001122 "Send a UNIX signal to the current process being debugged.",
Caroline Tice43b014a2010-10-04 22:28:36 +00001123 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001124 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001125 CommandArgumentEntry arg;
1126 CommandArgumentData signal_arg;
1127
1128 // Define the first (and only) variant of this arg.
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001129 signal_arg.arg_type = eArgTypeUnixSignal;
Caroline Tice43b014a2010-10-04 22:28:36 +00001130 signal_arg.arg_repetition = eArgRepeatPlain;
1131
1132 // There is only one variant this argument could be; put it into the argument entry.
1133 arg.push_back (signal_arg);
1134
1135 // Push the data for the first argument into the m_arguments vector.
1136 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001137 }
1138
1139 ~CommandObjectProcessSignal ()
1140 {
1141 }
1142
1143 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001144 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001145 CommandReturnObject &result)
1146 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001147 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001148 if (process == NULL)
1149 {
1150 result.AppendError ("no process to signal");
1151 result.SetStatus (eReturnStatusFailed);
1152 return false;
1153 }
1154
1155 if (command.GetArgumentCount() == 1)
1156 {
Greg Clayton8f6be2a2010-10-09 01:40:57 +00001157 int signo = LLDB_INVALID_SIGNAL_NUMBER;
1158
1159 const char *signal_name = command.GetArgumentAtIndex(0);
1160 if (::isxdigit (signal_name[0]))
1161 signo = Args::StringToSInt32(signal_name, LLDB_INVALID_SIGNAL_NUMBER, 0);
1162 else
1163 signo = process->GetUnixSignals().GetSignalNumberFromName (signal_name);
1164
1165 if (signo == LLDB_INVALID_SIGNAL_NUMBER)
Chris Lattner24943d22010-06-08 16:52:24 +00001166 {
1167 result.AppendErrorWithFormat ("Invalid signal argument '%s'.\n", command.GetArgumentAtIndex(0));
1168 result.SetStatus (eReturnStatusFailed);
1169 }
1170 else
1171 {
1172 Error error (process->Signal (signo));
1173 if (error.Success())
1174 {
1175 result.SetStatus (eReturnStatusSuccessFinishResult);
1176 }
1177 else
1178 {
1179 result.AppendErrorWithFormat ("Failed to send signal %i: %s\n", signo, error.AsCString());
1180 result.SetStatus (eReturnStatusFailed);
1181 }
1182 }
1183 }
1184 else
1185 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001186 result.AppendErrorWithFormat("'%s' takes exactly one signal number argument:\nUsage: %s\n", m_cmd_name.c_str(),
Chris Lattner24943d22010-06-08 16:52:24 +00001187 m_cmd_syntax.c_str());
1188 result.SetStatus (eReturnStatusFailed);
1189 }
1190 return result.Succeeded();
1191 }
1192};
1193
1194
1195//-------------------------------------------------------------------------
1196// CommandObjectProcessInterrupt
1197//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001198#pragma mark CommandObjectProcessInterrupt
Chris Lattner24943d22010-06-08 16:52:24 +00001199
1200class CommandObjectProcessInterrupt : public CommandObject
1201{
1202public:
1203
1204
Greg Clayton238c0a12010-09-18 01:14:36 +00001205 CommandObjectProcessInterrupt (CommandInterpreter &interpreter) :
1206 CommandObject (interpreter,
1207 "process interrupt",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001208 "Interrupt the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +00001209 "process interrupt",
1210 eFlagProcessMustBeLaunched)
1211 {
1212 }
1213
1214 ~CommandObjectProcessInterrupt ()
1215 {
1216 }
1217
1218 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001219 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001220 CommandReturnObject &result)
1221 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001222 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001223 if (process == NULL)
1224 {
1225 result.AppendError ("no process to halt");
1226 result.SetStatus (eReturnStatusFailed);
1227 return false;
1228 }
1229
1230 if (command.GetArgumentCount() == 0)
1231 {
1232 Error error(process->Halt ());
1233 if (error.Success())
1234 {
1235 result.SetStatus (eReturnStatusSuccessFinishResult);
1236
1237 // Maybe we should add a "SuspendThreadPlans so we
1238 // can halt, and keep in place all the current thread plans.
1239 process->GetThreadList().DiscardThreadPlans();
1240 }
1241 else
1242 {
1243 result.AppendErrorWithFormat ("Failed to halt process: %s\n", error.AsCString());
1244 result.SetStatus (eReturnStatusFailed);
1245 }
1246 }
1247 else
1248 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001249 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner24943d22010-06-08 16:52:24 +00001250 m_cmd_name.c_str(),
1251 m_cmd_syntax.c_str());
1252 result.SetStatus (eReturnStatusFailed);
1253 }
1254 return result.Succeeded();
1255 }
1256};
1257
1258//-------------------------------------------------------------------------
1259// CommandObjectProcessKill
1260//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001261#pragma mark CommandObjectProcessKill
Chris Lattner24943d22010-06-08 16:52:24 +00001262
1263class CommandObjectProcessKill : public CommandObject
1264{
1265public:
1266
Greg Clayton238c0a12010-09-18 01:14:36 +00001267 CommandObjectProcessKill (CommandInterpreter &interpreter) :
1268 CommandObject (interpreter,
1269 "process kill",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001270 "Terminate the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +00001271 "process kill",
1272 eFlagProcessMustBeLaunched)
1273 {
1274 }
1275
1276 ~CommandObjectProcessKill ()
1277 {
1278 }
1279
1280 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001281 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001282 CommandReturnObject &result)
1283 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001284 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001285 if (process == NULL)
1286 {
1287 result.AppendError ("no process to kill");
1288 result.SetStatus (eReturnStatusFailed);
1289 return false;
1290 }
1291
1292 if (command.GetArgumentCount() == 0)
1293 {
1294 Error error (process->Destroy());
1295 if (error.Success())
1296 {
1297 result.SetStatus (eReturnStatusSuccessFinishResult);
1298 }
1299 else
1300 {
1301 result.AppendErrorWithFormat ("Failed to kill process: %s\n", error.AsCString());
1302 result.SetStatus (eReturnStatusFailed);
1303 }
1304 }
1305 else
1306 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001307 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner24943d22010-06-08 16:52:24 +00001308 m_cmd_name.c_str(),
1309 m_cmd_syntax.c_str());
1310 result.SetStatus (eReturnStatusFailed);
1311 }
1312 return result.Succeeded();
1313 }
1314};
1315
1316//-------------------------------------------------------------------------
Jim Ingham41313fc2010-06-18 01:23:09 +00001317// CommandObjectProcessStatus
1318//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001319#pragma mark CommandObjectProcessStatus
1320
Jim Ingham41313fc2010-06-18 01:23:09 +00001321class CommandObjectProcessStatus : public CommandObject
1322{
1323public:
Greg Clayton238c0a12010-09-18 01:14:36 +00001324 CommandObjectProcessStatus (CommandInterpreter &interpreter) :
1325 CommandObject (interpreter,
1326 "process status",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001327 "Show the current status and location of executing process.",
1328 "process status",
Jim Ingham41313fc2010-06-18 01:23:09 +00001329 0)
1330 {
1331 }
1332
1333 ~CommandObjectProcessStatus()
1334 {
1335 }
1336
1337
1338 bool
1339 Execute
1340 (
1341 Args& command,
Jim Ingham41313fc2010-06-18 01:23:09 +00001342 CommandReturnObject &result
1343 )
1344 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001345 Stream &strm = result.GetOutputStream();
Jim Ingham41313fc2010-06-18 01:23:09 +00001346 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Claytonb72d0f02011-04-12 05:54:46 +00001347 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
Greg Clayton567e7f32011-09-22 04:58:26 +00001348 Process *process = exe_ctx.GetProcessPtr();
1349 if (process)
Jim Ingham41313fc2010-06-18 01:23:09 +00001350 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001351 const bool only_threads_with_stop_reason = true;
1352 const uint32_t start_frame = 0;
1353 const uint32_t num_frames = 1;
1354 const uint32_t num_frames_with_source = 1;
Greg Clayton567e7f32011-09-22 04:58:26 +00001355 process->GetStatus(strm);
1356 process->GetThreadStatus (strm,
1357 only_threads_with_stop_reason,
1358 start_frame,
1359 num_frames,
1360 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001361
Jim Ingham41313fc2010-06-18 01:23:09 +00001362 }
1363 else
1364 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001365 result.AppendError ("No process.");
Jim Ingham41313fc2010-06-18 01:23:09 +00001366 result.SetStatus (eReturnStatusFailed);
1367 }
1368 return result.Succeeded();
1369 }
1370};
1371
1372//-------------------------------------------------------------------------
Caroline Tice23d6f272010-10-13 20:44:39 +00001373// CommandObjectProcessHandle
1374//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001375#pragma mark CommandObjectProcessHandle
Caroline Tice23d6f272010-10-13 20:44:39 +00001376
1377class CommandObjectProcessHandle : public CommandObject
1378{
1379public:
1380
1381 class CommandOptions : public Options
1382 {
1383 public:
1384
Greg Claytonf15996e2011-04-07 22:46:35 +00001385 CommandOptions (CommandInterpreter &interpreter) :
1386 Options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001387 {
Greg Clayton143fcc32011-04-13 00:18:08 +00001388 OptionParsingStarting ();
Caroline Tice23d6f272010-10-13 20:44:39 +00001389 }
1390
1391 ~CommandOptions ()
1392 {
1393 }
1394
1395 Error
Greg Clayton143fcc32011-04-13 00:18:08 +00001396 SetOptionValue (uint32_t option_idx, const char *option_arg)
Caroline Tice23d6f272010-10-13 20:44:39 +00001397 {
1398 Error error;
1399 char short_option = (char) m_getopt_table[option_idx].val;
1400
1401 switch (short_option)
1402 {
1403 case 's':
1404 stop = option_arg;
1405 break;
1406 case 'n':
1407 notify = option_arg;
1408 break;
1409 case 'p':
1410 pass = option_arg;
1411 break;
1412 default:
Greg Clayton9c236732011-10-26 00:56:27 +00001413 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Caroline Tice23d6f272010-10-13 20:44:39 +00001414 break;
1415 }
1416 return error;
1417 }
1418
1419 void
Greg Clayton143fcc32011-04-13 00:18:08 +00001420 OptionParsingStarting ()
Caroline Tice23d6f272010-10-13 20:44:39 +00001421 {
Caroline Tice23d6f272010-10-13 20:44:39 +00001422 stop.clear();
1423 notify.clear();
1424 pass.clear();
1425 }
1426
Greg Claytonb3448432011-03-24 21:19:54 +00001427 const OptionDefinition*
Caroline Tice23d6f272010-10-13 20:44:39 +00001428 GetDefinitions ()
1429 {
1430 return g_option_table;
1431 }
1432
1433 // Options table: Required for subclasses of Options.
1434
Greg Claytonb3448432011-03-24 21:19:54 +00001435 static OptionDefinition g_option_table[];
Caroline Tice23d6f272010-10-13 20:44:39 +00001436
1437 // Instance variables to hold the values for command options.
1438
1439 std::string stop;
1440 std::string notify;
1441 std::string pass;
1442 };
1443
1444
1445 CommandObjectProcessHandle (CommandInterpreter &interpreter) :
1446 CommandObject (interpreter,
1447 "process handle",
Caroline Ticee7471982010-10-14 21:31:13 +00001448 "Show or update what the process and debugger should do with various signals received from the OS.",
Greg Claytonf15996e2011-04-07 22:46:35 +00001449 NULL),
1450 m_options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001451 {
Caroline Ticee7471982010-10-14 21:31:13 +00001452 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 +00001453 CommandArgumentEntry arg;
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001454 CommandArgumentData signal_arg;
Caroline Tice23d6f272010-10-13 20:44:39 +00001455
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001456 signal_arg.arg_type = eArgTypeUnixSignal;
1457 signal_arg.arg_repetition = eArgRepeatStar;
Caroline Tice23d6f272010-10-13 20:44:39 +00001458
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001459 arg.push_back (signal_arg);
Caroline Tice23d6f272010-10-13 20:44:39 +00001460
1461 m_arguments.push_back (arg);
1462 }
1463
1464 ~CommandObjectProcessHandle ()
1465 {
1466 }
1467
1468 Options *
1469 GetOptions ()
1470 {
1471 return &m_options;
1472 }
1473
1474 bool
Caroline Ticee7471982010-10-14 21:31:13 +00001475 VerifyCommandOptionValue (const std::string &option, int &real_value)
Caroline Tice23d6f272010-10-13 20:44:39 +00001476 {
1477 bool okay = true;
1478
Caroline Ticee7471982010-10-14 21:31:13 +00001479 bool success = false;
1480 bool tmp_value = Args::StringToBoolean (option.c_str(), false, &success);
1481
1482 if (success && tmp_value)
1483 real_value = 1;
1484 else if (success && !tmp_value)
1485 real_value = 0;
Caroline Tice23d6f272010-10-13 20:44:39 +00001486 else
1487 {
1488 // If the value isn't 'true' or 'false', it had better be 0 or 1.
Caroline Ticee7471982010-10-14 21:31:13 +00001489 real_value = Args::StringToUInt32 (option.c_str(), 3);
1490 if (real_value != 0 && real_value != 1)
Caroline Tice23d6f272010-10-13 20:44:39 +00001491 okay = false;
1492 }
1493
1494 return okay;
1495 }
1496
Caroline Ticee7471982010-10-14 21:31:13 +00001497 void
1498 PrintSignalHeader (Stream &str)
1499 {
1500 str.Printf ("NAME PASS STOP NOTIFY\n");
1501 str.Printf ("========== ===== ===== ======\n");
1502 }
1503
1504 void
1505 PrintSignal (Stream &str, int32_t signo, const char *sig_name, UnixSignals &signals)
1506 {
1507 bool stop;
1508 bool suppress;
1509 bool notify;
1510
1511 str.Printf ("%-10s ", sig_name);
1512 if (signals.GetSignalInfo (signo, suppress, stop, notify))
1513 {
1514 bool pass = !suppress;
1515 str.Printf ("%s %s %s",
1516 (pass ? "true " : "false"),
1517 (stop ? "true " : "false"),
1518 (notify ? "true " : "false"));
1519 }
1520 str.Printf ("\n");
1521 }
1522
1523 void
1524 PrintSignalInformation (Stream &str, Args &signal_args, int num_valid_signals, UnixSignals &signals)
1525 {
1526 PrintSignalHeader (str);
1527
1528 if (num_valid_signals > 0)
1529 {
1530 size_t num_args = signal_args.GetArgumentCount();
1531 for (size_t i = 0; i < num_args; ++i)
1532 {
1533 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1534 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
1535 PrintSignal (str, signo, signal_args.GetArgumentAtIndex (i), signals);
1536 }
1537 }
1538 else // Print info for ALL signals
1539 {
1540 int32_t signo = signals.GetFirstSignalNumber();
1541 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1542 {
1543 PrintSignal (str, signo, signals.GetSignalAsCString (signo), signals);
1544 signo = signals.GetNextSignalNumber (signo);
1545 }
1546 }
1547 }
1548
Caroline Tice23d6f272010-10-13 20:44:39 +00001549 bool
1550 Execute (Args &signal_args, CommandReturnObject &result)
1551 {
1552 TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
1553
1554 if (!target_sp)
1555 {
1556 result.AppendError ("No current target;"
1557 " cannot handle signals until you have a valid target and process.\n");
1558 result.SetStatus (eReturnStatusFailed);
1559 return false;
1560 }
1561
1562 ProcessSP process_sp = target_sp->GetProcessSP();
1563
1564 if (!process_sp)
1565 {
1566 result.AppendError ("No current process; cannot handle signals until you have a valid process.\n");
1567 result.SetStatus (eReturnStatusFailed);
1568 return false;
1569 }
1570
Caroline Tice23d6f272010-10-13 20:44:39 +00001571 int stop_action = -1; // -1 means leave the current setting alone
Caroline Ticee7471982010-10-14 21:31:13 +00001572 int pass_action = -1; // -1 means leave the current setting alone
Caroline Tice23d6f272010-10-13 20:44:39 +00001573 int notify_action = -1; // -1 means leave the current setting alone
1574
1575 if (! m_options.stop.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001576 && ! VerifyCommandOptionValue (m_options.stop, stop_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001577 {
1578 result.AppendError ("Invalid argument for command option --stop; must be true or false.\n");
1579 result.SetStatus (eReturnStatusFailed);
1580 return false;
1581 }
1582
1583 if (! m_options.notify.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001584 && ! VerifyCommandOptionValue (m_options.notify, notify_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001585 {
1586 result.AppendError ("Invalid argument for command option --notify; must be true or false.\n");
1587 result.SetStatus (eReturnStatusFailed);
1588 return false;
1589 }
1590
1591 if (! m_options.pass.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001592 && ! VerifyCommandOptionValue (m_options.pass, pass_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001593 {
1594 result.AppendError ("Invalid argument for command option --pass; must be true or false.\n");
1595 result.SetStatus (eReturnStatusFailed);
1596 return false;
1597 }
1598
1599 size_t num_args = signal_args.GetArgumentCount();
1600 UnixSignals &signals = process_sp->GetUnixSignals();
1601 int num_signals_set = 0;
1602
Caroline Ticee7471982010-10-14 21:31:13 +00001603 if (num_args > 0)
Caroline Tice23d6f272010-10-13 20:44:39 +00001604 {
Caroline Ticee7471982010-10-14 21:31:13 +00001605 for (size_t i = 0; i < num_args; ++i)
Caroline Tice23d6f272010-10-13 20:44:39 +00001606 {
Caroline Ticee7471982010-10-14 21:31:13 +00001607 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1608 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
Caroline Tice23d6f272010-10-13 20:44:39 +00001609 {
Caroline Ticee7471982010-10-14 21:31:13 +00001610 // Casting the actions as bools here should be okay, because VerifyCommandOptionValue guarantees
1611 // the value is either 0 or 1.
1612 if (stop_action != -1)
1613 signals.SetShouldStop (signo, (bool) stop_action);
1614 if (pass_action != -1)
1615 {
1616 bool suppress = ! ((bool) pass_action);
1617 signals.SetShouldSuppress (signo, suppress);
1618 }
1619 if (notify_action != -1)
1620 signals.SetShouldNotify (signo, (bool) notify_action);
1621 ++num_signals_set;
Caroline Tice23d6f272010-10-13 20:44:39 +00001622 }
Caroline Ticee7471982010-10-14 21:31:13 +00001623 else
1624 {
1625 result.AppendErrorWithFormat ("Invalid signal name '%s'\n", signal_args.GetArgumentAtIndex (i));
1626 }
Caroline Tice23d6f272010-10-13 20:44:39 +00001627 }
1628 }
Caroline Ticee7471982010-10-14 21:31:13 +00001629 else
1630 {
1631 // No signal specified, if any command options were specified, update ALL signals.
1632 if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1))
1633 {
1634 if (m_interpreter.Confirm ("Do you really want to update all the signals?", false))
1635 {
1636 int32_t signo = signals.GetFirstSignalNumber();
1637 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1638 {
1639 if (notify_action != -1)
1640 signals.SetShouldNotify (signo, (bool) notify_action);
1641 if (stop_action != -1)
1642 signals.SetShouldStop (signo, (bool) stop_action);
1643 if (pass_action != -1)
1644 {
1645 bool suppress = ! ((bool) pass_action);
1646 signals.SetShouldSuppress (signo, suppress);
1647 }
1648 signo = signals.GetNextSignalNumber (signo);
1649 }
1650 }
1651 }
1652 }
1653
1654 PrintSignalInformation (result.GetOutputStream(), signal_args, num_signals_set, signals);
Caroline Tice23d6f272010-10-13 20:44:39 +00001655
1656 if (num_signals_set > 0)
1657 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1658 else
1659 result.SetStatus (eReturnStatusFailed);
1660
1661 return result.Succeeded();
1662 }
1663
1664protected:
1665
1666 CommandOptions m_options;
1667};
1668
Greg Claytonb3448432011-03-24 21:19:54 +00001669OptionDefinition
Caroline Tice23d6f272010-10-13 20:44:39 +00001670CommandObjectProcessHandle::CommandOptions::g_option_table[] =
1671{
1672{ 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." },
1673{ 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." },
1674{ LLDB_OPT_SET_1, false, "pass", 'p', required_argument, NULL, 0, eArgTypeBoolean, "Whether or not the signal should be passed to the process." },
1675{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1676};
1677
1678//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001679// CommandObjectMultiwordProcess
1680//-------------------------------------------------------------------------
1681
Greg Clayton63094e02010-06-23 01:19:29 +00001682CommandObjectMultiwordProcess::CommandObjectMultiwordProcess (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00001683 CommandObjectMultiword (interpreter,
1684 "process",
1685 "A set of commands for operating on a process.",
1686 "process <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00001687{
Greg Claytona9eb8272011-07-02 21:07:54 +00001688 LoadSubCommand ("attach", CommandObjectSP (new CommandObjectProcessAttach (interpreter)));
1689 LoadSubCommand ("launch", CommandObjectSP (new CommandObjectProcessLaunch (interpreter)));
1690 LoadSubCommand ("continue", CommandObjectSP (new CommandObjectProcessContinue (interpreter)));
1691 LoadSubCommand ("connect", CommandObjectSP (new CommandObjectProcessConnect (interpreter)));
1692 LoadSubCommand ("detach", CommandObjectSP (new CommandObjectProcessDetach (interpreter)));
1693 LoadSubCommand ("load", CommandObjectSP (new CommandObjectProcessLoad (interpreter)));
1694 LoadSubCommand ("unload", CommandObjectSP (new CommandObjectProcessUnload (interpreter)));
1695 LoadSubCommand ("signal", CommandObjectSP (new CommandObjectProcessSignal (interpreter)));
1696 LoadSubCommand ("handle", CommandObjectSP (new CommandObjectProcessHandle (interpreter)));
1697 LoadSubCommand ("status", CommandObjectSP (new CommandObjectProcessStatus (interpreter)));
Greg Clayton238c0a12010-09-18 01:14:36 +00001698 LoadSubCommand ("interrupt", CommandObjectSP (new CommandObjectProcessInterrupt (interpreter)));
Greg Claytona9eb8272011-07-02 21:07:54 +00001699 LoadSubCommand ("kill", CommandObjectSP (new CommandObjectProcessKill (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00001700}
1701
1702CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess ()
1703{
1704}
1705