blob: 0400dfb96d58188a9e182f0e8b58a60caad82790 [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 {
Greg Clayton3e6f2cc2011-11-21 21:51:18 +0000223 // Save the arguments for subsequent runs in the current target.
224 target->SetRunArguments (launch_args);
225
Greg Claytonabb33022011-11-08 02:43:13 +0000226 m_options.launch_info.GetArguments().AppendArguments (launch_args);
227 }
Greg Claytonabb33022011-11-08 02:43:13 +0000228
Greg Clayton527154d2011-11-15 03:53:30 +0000229 if (target->GetDisableASLR())
230 m_options.launch_info.GetFlags().Set (eLaunchFlagDisableASLR);
231
232 if (target->GetDisableSTDIO())
233 m_options.launch_info.GetFlags().Set (eLaunchFlagDisableSTDIO);
234
235 m_options.launch_info.GetFlags().Set (eLaunchFlagDebug);
236
237 Args environment;
238 target->GetEnvironmentAsArgs (environment);
239 if (environment.GetArgumentCount() > 0)
240 m_options.launch_info.GetEnvironmentEntries ().AppendArguments (environment);
241
Greg Clayton464c6162011-11-17 22:14:31 +0000242 // Finalize the file actions, and if none were given, default to opening
243 // up a pseudo terminal
244 const bool default_to_use_pty = true;
245 m_options.launch_info.FinalizeFileActions (target, default_to_use_pty);
Greg Clayton527154d2011-11-15 03:53:30 +0000246
Greg Claytonabb33022011-11-08 02:43:13 +0000247 if (state == eStateConnected)
248 {
249 if (m_options.launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
250 {
251 result.AppendWarning("can't launch in tty when launching through a remote connection");
252 m_options.launch_info.GetFlags().Clear (eLaunchFlagLaunchInTTY);
253 }
254 }
255 else
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000256 {
Greg Clayton527154d2011-11-15 03:53:30 +0000257 if (!m_options.launch_info.GetArchitecture().IsValid())
Greg Clayton2d9adb72011-11-12 02:10:56 +0000258 m_options.launch_info.GetArchitecture() = target->GetArchitecture();
259
Greg Clayton75d8c252011-11-28 01:45:00 +0000260 PlatformSP platform_sp (target->GetPlatform());
261
262 if (platform_sp && platform_sp->CanDebugProcess ())
263 {
264 process = target->GetPlatform()->DebugProcess (m_options.launch_info,
265 debugger,
266 target,
267 debugger.GetListener(),
268 error).get();
269 }
270 else
271 {
272 const char *plugin_name = m_options.launch_info.GetProcessPluginName();
273 process = target->CreateProcess (debugger.GetListener(), plugin_name).get();
274 if (process)
275 error = process->Launch (m_options.launch_info);
276 }
Greg Claytonabb33022011-11-08 02:43:13 +0000277
Greg Claytona2f74232011-02-24 22:24:29 +0000278 if (process == NULL)
279 {
Greg Clayton527154d2011-11-15 03:53:30 +0000280 result.SetError (error, "failed to launch or debug process");
Greg Claytona2f74232011-02-24 22:24:29 +0000281 return false;
282 }
Chris Lattner24943d22010-06-08 16:52:24 +0000283 }
Greg Claytonabb33022011-11-08 02:43:13 +0000284
Greg Clayton238c0a12010-09-18 01:14:36 +0000285 if (error.Success())
286 {
Greg Clayton940b1032011-02-23 00:35:02 +0000287 const char *archname = exe_module->GetArchitecture().GetArchitectureName();
Greg Claytonc1d37752010-10-18 01:45:30 +0000288
Greg Clayton444e35b2011-10-19 18:09:39 +0000289 result.AppendMessageWithFormat ("Process %llu launched: '%s' (%s)\n", process->GetID(), filename, archname);
Greg Claytond8c62532010-10-07 04:19:01 +0000290 result.SetDidChangeProcessState (true);
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000291 if (m_options.launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
Greg Clayton238c0a12010-09-18 01:14:36 +0000292 {
Greg Claytond8c62532010-10-07 04:19:01 +0000293 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
Greg Clayton238c0a12010-09-18 01:14:36 +0000294 StateType state = process->WaitForProcessToStop (NULL);
295
296 if (state == eStateStopped)
297 {
Greg Claytond8c62532010-10-07 04:19:01 +0000298 error = process->Resume();
299 if (error.Success())
300 {
301 bool synchronous_execution = m_interpreter.GetSynchronous ();
302 if (synchronous_execution)
303 {
304 state = process->WaitForProcessToStop (NULL);
Greg Clayton20206082011-11-17 01:23:07 +0000305 const bool must_be_alive = true;
306 if (!StateIsStoppedState(state, must_be_alive))
Greg Clayton395fc332011-02-15 21:59:32 +0000307 {
Greg Clayton527154d2011-11-15 03:53:30 +0000308 result.AppendErrorWithFormat ("process isn't stopped: %s", StateAsCString(state));
Greg Clayton395fc332011-02-15 21:59:32 +0000309 }
Greg Claytond8c62532010-10-07 04:19:01 +0000310 result.SetDidChangeProcessState (true);
311 result.SetStatus (eReturnStatusSuccessFinishResult);
312 }
313 else
314 {
315 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
316 }
317 }
Greg Clayton395fc332011-02-15 21:59:32 +0000318 else
319 {
Greg Clayton527154d2011-11-15 03:53:30 +0000320 result.AppendErrorWithFormat ("process resume at entry point failed: %s", error.AsCString());
Greg Clayton395fc332011-02-15 21:59:32 +0000321 result.SetStatus (eReturnStatusFailed);
322 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000323 }
Greg Clayton395fc332011-02-15 21:59:32 +0000324 else
325 {
Greg Clayton527154d2011-11-15 03:53:30 +0000326 result.AppendErrorWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
Greg Clayton395fc332011-02-15 21:59:32 +0000327 result.SetStatus (eReturnStatusFailed);
328 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000329 }
330 }
Greg Clayton395fc332011-02-15 21:59:32 +0000331 else
332 {
Greg Claytona9eb8272011-07-02 21:07:54 +0000333 result.AppendErrorWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton395fc332011-02-15 21:59:32 +0000334 result.SetStatus (eReturnStatusFailed);
335 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000336
Chris Lattner24943d22010-06-08 16:52:24 +0000337 return result.Succeeded();
338 }
339
Jim Ingham767af882010-07-07 03:36:20 +0000340 virtual const char *GetRepeatCommand (Args &current_command_args, uint32_t index)
341 {
342 // No repeat for "process launch"...
343 return "";
344 }
345
Chris Lattner24943d22010-06-08 16:52:24 +0000346protected:
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000347 ProcessLaunchCommandOptions m_options;
Chris Lattner24943d22010-06-08 16:52:24 +0000348};
349
350
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000351//#define SET1 LLDB_OPT_SET_1
352//#define SET2 LLDB_OPT_SET_2
353//#define SET3 LLDB_OPT_SET_3
354//
355//OptionDefinition
356//CommandObjectProcessLaunch::CommandOptions::g_option_table[] =
357//{
358//{ 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."},
359//{ SET1 , false, "stdin", 'i', required_argument, NULL, 0, eArgTypePath, "Redirect stdin for the process to <path>."},
360//{ SET1 , false, "stdout", 'o', required_argument, NULL, 0, eArgTypePath, "Redirect stdout for the process to <path>."},
361//{ SET1 , false, "stderr", 'e', required_argument, NULL, 0, eArgTypePath, "Redirect stderr for the process to <path>."},
362//{ SET1 | SET2 | SET3, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
363//{ 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."},
364//{ SET3, false, "no-stdio", 'n', no_argument, NULL, 0, eArgTypeNone, "Do not set up for terminal I/O to go to running process."},
365//{ SET1 | SET2 | SET3, false, "working-dir", 'w', required_argument, NULL, 0, eArgTypePath, "Set the current working directory to <path> when running the inferior."},
366//{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
367//};
368//
369//#undef SET1
370//#undef SET2
371//#undef SET3
Chris Lattner24943d22010-06-08 16:52:24 +0000372
373//-------------------------------------------------------------------------
374// CommandObjectProcessAttach
375//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000376#pragma mark CommandObjectProcessAttach
Chris Lattner24943d22010-06-08 16:52:24 +0000377class CommandObjectProcessAttach : public CommandObject
378{
379public:
380
Chris Lattner24943d22010-06-08 16:52:24 +0000381 class CommandOptions : public Options
382 {
383 public:
384
Greg Claytonf15996e2011-04-07 22:46:35 +0000385 CommandOptions (CommandInterpreter &interpreter) :
386 Options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000387 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000388 // Keep default values of all options in one place: OptionParsingStarting ()
389 OptionParsingStarting ();
Chris Lattner24943d22010-06-08 16:52:24 +0000390 }
391
392 ~CommandOptions ()
393 {
394 }
395
396 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000397 SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +0000398 {
399 Error error;
400 char short_option = (char) m_getopt_table[option_idx].val;
401 bool success = false;
402 switch (short_option)
403 {
404 case 'p':
Chris Lattner24943d22010-06-08 16:52:24 +0000405 {
Greg Clayton527154d2011-11-15 03:53:30 +0000406 lldb::pid_t pid = Args::StringToUInt32 (option_arg, LLDB_INVALID_PROCESS_ID, 0, &success);
407 if (!success || pid == LLDB_INVALID_PROCESS_ID)
408 {
409 error.SetErrorStringWithFormat("invalid process ID '%s'", option_arg);
410 }
411 else
412 {
413 attach_info.SetProcessID (pid);
414 }
Chris Lattner24943d22010-06-08 16:52:24 +0000415 }
416 break;
417
418 case 'P':
Greg Clayton527154d2011-11-15 03:53:30 +0000419 attach_info.SetProcessPluginName (option_arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000420 break;
421
422 case 'n':
Greg Clayton527154d2011-11-15 03:53:30 +0000423 attach_info.GetExecutableFile().SetFile(option_arg, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000424 break;
425
426 case 'w':
Greg Clayton527154d2011-11-15 03:53:30 +0000427 attach_info.SetWaitForLaunch(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000428 break;
429
430 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000431 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner24943d22010-06-08 16:52:24 +0000432 break;
433 }
434 return error;
435 }
436
437 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000438 OptionParsingStarting ()
Chris Lattner24943d22010-06-08 16:52:24 +0000439 {
Greg Clayton527154d2011-11-15 03:53:30 +0000440 attach_info.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000441 }
442
Greg Claytonb3448432011-03-24 21:19:54 +0000443 const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +0000444 GetDefinitions ()
445 {
446 return g_option_table;
447 }
448
Jim Ingham7508e732010-08-09 23:31:02 +0000449 virtual bool
Greg Claytonf15996e2011-04-07 22:46:35 +0000450 HandleOptionArgumentCompletion (Args &input,
Jim Ingham7508e732010-08-09 23:31:02 +0000451 int cursor_index,
452 int char_pos,
453 OptionElementVector &opt_element_vector,
454 int opt_element_index,
455 int match_start_point,
456 int max_return_elements,
457 bool &word_complete,
458 StringList &matches)
459 {
460 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
461 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
462
463 // We are only completing the name option for now...
464
Greg Claytonb3448432011-03-24 21:19:54 +0000465 const OptionDefinition *opt_defs = GetDefinitions();
Jim Ingham7508e732010-08-09 23:31:02 +0000466 if (opt_defs[opt_defs_index].short_option == 'n')
467 {
468 // Are we in the name?
469
470 // Look to see if there is a -P argument provided, and if so use that plugin, otherwise
471 // use the default plugin.
Jim Ingham7508e732010-08-09 23:31:02 +0000472
473 const char *partial_name = NULL;
474 partial_name = input.GetArgumentAtIndex(opt_arg_pos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000475
Greg Claytonb72d0f02011-04-12 05:54:46 +0000476 PlatformSP platform_sp (m_interpreter.GetPlatform (true));
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000477 if (platform_sp)
Jim Ingham7508e732010-08-09 23:31:02 +0000478 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000479 ProcessInstanceInfoList process_infos;
480 ProcessInstanceInfoMatch match_info;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000481 if (partial_name)
482 {
Greg Clayton527154d2011-11-15 03:53:30 +0000483 match_info.GetProcessInfo().GetExecutableFile().SetFile(partial_name, false);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000484 match_info.SetNameMatchType(eNameMatchStartsWith);
485 }
486 platform_sp->FindProcesses (match_info, process_infos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000487 const uint32_t num_matches = process_infos.GetSize();
488 if (num_matches > 0)
489 {
490 for (uint32_t i=0; i<num_matches; ++i)
491 {
492 matches.AppendString (process_infos.GetProcessNameAtIndex(i),
493 process_infos.GetProcessNameLengthAtIndex(i));
494 }
495 }
Jim Ingham7508e732010-08-09 23:31:02 +0000496 }
497 }
498
499 return false;
500 }
501
Chris Lattner24943d22010-06-08 16:52:24 +0000502 // Options table: Required for subclasses of Options.
503
Greg Claytonb3448432011-03-24 21:19:54 +0000504 static OptionDefinition g_option_table[];
Chris Lattner24943d22010-06-08 16:52:24 +0000505
506 // Instance variables to hold the values for command options.
507
Greg Clayton527154d2011-11-15 03:53:30 +0000508 ProcessAttachInfo attach_info;
Chris Lattner24943d22010-06-08 16:52:24 +0000509 };
510
Greg Clayton238c0a12010-09-18 01:14:36 +0000511 CommandObjectProcessAttach (CommandInterpreter &interpreter) :
512 CommandObject (interpreter,
513 "process attach",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000514 "Attach to a process.",
Greg Claytonf15996e2011-04-07 22:46:35 +0000515 "process attach <cmd-options>"),
516 m_options (interpreter)
Jim Ingham7508e732010-08-09 23:31:02 +0000517 {
Jim Ingham7508e732010-08-09 23:31:02 +0000518 }
519
520 ~CommandObjectProcessAttach ()
521 {
522 }
523
524 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000525 Execute (Args& command,
Jim Ingham7508e732010-08-09 23:31:02 +0000526 CommandReturnObject &result)
527 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000528 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Jim Inghamee940e22011-09-15 01:08:57 +0000529 // N.B. The attach should be synchronous. It doesn't help much to get the prompt back between initiating the attach
530 // and the target actually stopping. So even if the interpreter is set to be asynchronous, we wait for the stop
531 // ourselves here.
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000532
Greg Clayton567e7f32011-09-22 04:58:26 +0000533 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytona2f74232011-02-24 22:24:29 +0000534 StateType state = eStateInvalid;
Jim Ingham7508e732010-08-09 23:31:02 +0000535 if (process)
536 {
Greg Claytona2f74232011-02-24 22:24:29 +0000537 state = process->GetState();
538 if (process->IsAlive() && state != eStateConnected)
Jim Ingham7508e732010-08-09 23:31:02 +0000539 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000540 result.AppendErrorWithFormat ("Process %llu is currently being debugged, kill the process before attaching.\n",
Jim Ingham7508e732010-08-09 23:31:02 +0000541 process->GetID());
542 result.SetStatus (eReturnStatusFailed);
543 return false;
544 }
545 }
546
547 if (target == NULL)
548 {
549 // If there isn't a current target create one.
550 TargetSP new_target_sp;
551 FileSpec emptyFileSpec;
Jim Ingham7508e732010-08-09 23:31:02 +0000552 Error error;
553
Greg Clayton238c0a12010-09-18 01:14:36 +0000554 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
555 emptyFileSpec,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000556 NULL,
Greg Clayton238c0a12010-09-18 01:14:36 +0000557 false,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000558 NULL, // No platform options
Greg Clayton238c0a12010-09-18 01:14:36 +0000559 new_target_sp);
Jim Ingham7508e732010-08-09 23:31:02 +0000560 target = new_target_sp.get();
561 if (target == NULL || error.Fail())
562 {
Greg Claytone71e2582011-02-04 01:58:07 +0000563 result.AppendError(error.AsCString("Error creating target"));
Jim Ingham7508e732010-08-09 23:31:02 +0000564 return false;
565 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000566 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target);
Jim Ingham7508e732010-08-09 23:31:02 +0000567 }
568
569 // Record the old executable module, we want to issue a warning if the process of attaching changed the
570 // current executable (like somebody said "file foo" then attached to a PID whose executable was bar.)
571
572 ModuleSP old_exec_module_sp = target->GetExecutableModule();
573 ArchSpec old_arch_spec = target->GetArchitecture();
574
575 if (command.GetArgumentCount())
576 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000577 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 +0000578 result.SetStatus (eReturnStatusFailed);
579 }
580 else
581 {
Greg Claytona2f74232011-02-24 22:24:29 +0000582 if (state != eStateConnected)
583 {
Greg Clayton527154d2011-11-15 03:53:30 +0000584 const char *plugin_name = m_options.attach_info.GetProcessPluginName();
Greg Claytona2f74232011-02-24 22:24:29 +0000585 process = target->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name).get();
586 }
Jim Ingham7508e732010-08-09 23:31:02 +0000587
588 if (process)
589 {
590 Error error;
Greg Clayton527154d2011-11-15 03:53:30 +0000591 // If no process info was specified, then use the target executable
592 // name as the process to attach to by default
593 if (!m_options.attach_info.ProcessInfoSpecified ())
Jim Ingham4805a1c2010-09-15 01:34:14 +0000594 {
595 if (old_exec_module_sp)
Greg Clayton527154d2011-11-15 03:53:30 +0000596 m_options.attach_info.GetExecutableFile().GetFilename() = old_exec_module_sp->GetFileSpec().GetFilename();
Jim Ingham4805a1c2010-09-15 01:34:14 +0000597
Greg Clayton527154d2011-11-15 03:53:30 +0000598 if (!m_options.attach_info.ProcessInfoSpecified ())
599 {
600 error.SetErrorString ("no process specified, create a target with a file, or specify the --pid or --name command option");
601 }
602 }
603
604 if (error.Success())
605 {
606 error = process->Attach (m_options.attach_info);
607
Jim Ingham4805a1c2010-09-15 01:34:14 +0000608 if (error.Success())
609 {
610 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
611 }
Jim Ingham7508e732010-08-09 23:31:02 +0000612 else
613 {
Greg Clayton527154d2011-11-15 03:53:30 +0000614 result.AppendErrorWithFormat ("attach failed: %s\n", error.AsCString());
Jim Ingham4805a1c2010-09-15 01:34:14 +0000615 result.SetStatus (eReturnStatusFailed);
616 return false;
Jim Ingham7508e732010-08-09 23:31:02 +0000617 }
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000618 // If we're synchronous, wait for the stopped event and report that.
619 // Otherwise just return.
620 // FIXME: in the async case it will now be possible to get to the command
621 // interpreter with a state eStateAttaching. Make sure we handle that correctly.
Jim Inghamee940e22011-09-15 01:08:57 +0000622 StateType state = process->WaitForProcessToStop (NULL);
Greg Clayton527154d2011-11-15 03:53:30 +0000623
Jim Inghamee940e22011-09-15 01:08:57 +0000624 result.SetDidChangeProcessState (true);
Greg Clayton444e35b2011-10-19 18:09:39 +0000625 result.AppendMessageWithFormat ("Process %llu %s\n", process->GetID(), StateAsCString (state));
Jim Inghamee940e22011-09-15 01:08:57 +0000626 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Jim Ingham7508e732010-08-09 23:31:02 +0000627 }
Jim Ingham7508e732010-08-09 23:31:02 +0000628 }
629 }
630
631 if (result.Succeeded())
632 {
633 // Okay, we're done. Last step is to warn if the executable module has changed:
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000634 char new_path[PATH_MAX];
Greg Clayton5beb99d2011-08-11 02:48:45 +0000635 ModuleSP new_exec_module_sp (target->GetExecutableModule());
Jim Ingham7508e732010-08-09 23:31:02 +0000636 if (!old_exec_module_sp)
637 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000638 // We might not have a module if we attached to a raw pid...
Greg Clayton5beb99d2011-08-11 02:48:45 +0000639 if (new_exec_module_sp)
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000640 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000641 new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000642 result.AppendMessageWithFormat("Executable module set to \"%s\".\n", new_path);
643 }
Jim Ingham7508e732010-08-09 23:31:02 +0000644 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000645 else if (old_exec_module_sp->GetFileSpec() != new_exec_module_sp->GetFileSpec())
Jim Ingham7508e732010-08-09 23:31:02 +0000646 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000647 char old_path[PATH_MAX];
Jim Ingham7508e732010-08-09 23:31:02 +0000648
Greg Clayton5beb99d2011-08-11 02:48:45 +0000649 old_exec_module_sp->GetFileSpec().GetPath (old_path, PATH_MAX);
650 new_exec_module_sp->GetFileSpec().GetPath (new_path, PATH_MAX);
Jim Ingham7508e732010-08-09 23:31:02 +0000651
652 result.AppendWarningWithFormat("Executable module changed from \"%s\" to \"%s\".\n",
653 old_path, new_path);
654 }
655
656 if (!old_arch_spec.IsValid())
657 {
Greg Clayton940b1032011-02-23 00:35:02 +0000658 result.AppendMessageWithFormat ("Architecture set to: %s.\n", target->GetArchitecture().GetArchitectureName());
Jim Ingham7508e732010-08-09 23:31:02 +0000659 }
660 else if (old_arch_spec != target->GetArchitecture())
661 {
662 result.AppendWarningWithFormat("Architecture changed from %s to %s.\n",
Greg Clayton940b1032011-02-23 00:35:02 +0000663 old_arch_spec.GetArchitectureName(), target->GetArchitecture().GetArchitectureName());
Jim Ingham7508e732010-08-09 23:31:02 +0000664 }
665 }
666 return result.Succeeded();
667 }
668
669 Options *
670 GetOptions ()
671 {
672 return &m_options;
673 }
674
Chris Lattner24943d22010-06-08 16:52:24 +0000675protected:
676
677 CommandOptions m_options;
678};
679
680
Greg Claytonb3448432011-03-24 21:19:54 +0000681OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000682CommandObjectProcessAttach::CommandOptions::g_option_table[] =
683{
Caroline Tice4d6675c2010-10-01 19:59:14 +0000684{ LLDB_OPT_SET_ALL, false, "plugin", 'P', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
685{ LLDB_OPT_SET_1, false, "pid", 'p', required_argument, NULL, 0, eArgTypePid, "The process ID of an existing process to attach to."},
686{ LLDB_OPT_SET_2, false, "name", 'n', required_argument, NULL, 0, eArgTypeProcessName, "The name of the process to attach to."},
687{ LLDB_OPT_SET_2, false, "waitfor",'w', no_argument, NULL, 0, eArgTypeNone, "Wait for the the process with <process-name> to launch."},
688{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000689};
690
691//-------------------------------------------------------------------------
692// CommandObjectProcessContinue
693//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000694#pragma mark CommandObjectProcessContinue
Chris Lattner24943d22010-06-08 16:52:24 +0000695
696class CommandObjectProcessContinue : public CommandObject
697{
698public:
699
Greg Clayton238c0a12010-09-18 01:14:36 +0000700 CommandObjectProcessContinue (CommandInterpreter &interpreter) :
701 CommandObject (interpreter,
702 "process continue",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000703 "Continue execution of all threads in the current process.",
Chris Lattner24943d22010-06-08 16:52:24 +0000704 "process continue",
705 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
706 {
707 }
708
709
710 ~CommandObjectProcessContinue ()
711 {
712 }
713
714 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000715 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000716 CommandReturnObject &result)
717 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000718 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton238c0a12010-09-18 01:14:36 +0000719 bool synchronous_execution = m_interpreter.GetSynchronous ();
Chris Lattner24943d22010-06-08 16:52:24 +0000720
721 if (process == NULL)
722 {
723 result.AppendError ("no process to continue");
724 result.SetStatus (eReturnStatusFailed);
725 return false;
726 }
727
728 StateType state = process->GetState();
729 if (state == eStateStopped)
730 {
731 if (command.GetArgumentCount() != 0)
732 {
733 result.AppendErrorWithFormat ("The '%s' command does not take any arguments.\n", m_cmd_name.c_str());
734 result.SetStatus (eReturnStatusFailed);
735 return false;
736 }
737
738 const uint32_t num_threads = process->GetThreadList().GetSize();
739
740 // Set the actions that the threads should each take when resuming
741 for (uint32_t idx=0; idx<num_threads; ++idx)
742 {
743 process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState (eStateRunning);
744 }
745
746 Error error(process->Resume());
747 if (error.Success())
748 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000749 result.AppendMessageWithFormat ("Process %llu resuming\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000750 if (synchronous_execution)
751 {
Greg Claytonbef15832010-07-14 00:18:15 +0000752 state = process->WaitForProcessToStop (NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000753
754 result.SetDidChangeProcessState (true);
Greg Clayton444e35b2011-10-19 18:09:39 +0000755 result.AppendMessageWithFormat ("Process %llu %s\n", process->GetID(), StateAsCString (state));
Chris Lattner24943d22010-06-08 16:52:24 +0000756 result.SetStatus (eReturnStatusSuccessFinishNoResult);
757 }
758 else
759 {
760 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
761 }
762 }
763 else
764 {
765 result.AppendErrorWithFormat("Failed to resume process: %s.\n", error.AsCString());
766 result.SetStatus (eReturnStatusFailed);
767 }
768 }
769 else
770 {
771 result.AppendErrorWithFormat ("Process cannot be continued from its current state (%s).\n",
772 StateAsCString(state));
773 result.SetStatus (eReturnStatusFailed);
774 }
775 return result.Succeeded();
776 }
777};
778
779//-------------------------------------------------------------------------
780// CommandObjectProcessDetach
781//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000782#pragma mark CommandObjectProcessDetach
Chris Lattner24943d22010-06-08 16:52:24 +0000783
784class CommandObjectProcessDetach : public CommandObject
785{
786public:
787
Greg Clayton238c0a12010-09-18 01:14:36 +0000788 CommandObjectProcessDetach (CommandInterpreter &interpreter) :
789 CommandObject (interpreter,
790 "process detach",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000791 "Detach from the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +0000792 "process detach",
793 eFlagProcessMustBeLaunched)
794 {
795 }
796
797 ~CommandObjectProcessDetach ()
798 {
799 }
800
801 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000802 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000803 CommandReturnObject &result)
804 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000805 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000806 if (process == NULL)
807 {
808 result.AppendError ("must have a valid process in order to detach");
809 result.SetStatus (eReturnStatusFailed);
810 return false;
811 }
812
Greg Clayton444e35b2011-10-19 18:09:39 +0000813 result.AppendMessageWithFormat ("Detaching from process %llu\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000814 Error error (process->Detach());
815 if (error.Success())
816 {
817 result.SetStatus (eReturnStatusSuccessFinishResult);
818 }
819 else
820 {
821 result.AppendErrorWithFormat ("Detach failed: %s\n", error.AsCString());
822 result.SetStatus (eReturnStatusFailed);
823 return false;
824 }
825 return result.Succeeded();
826 }
827};
828
829//-------------------------------------------------------------------------
Greg Claytone71e2582011-02-04 01:58:07 +0000830// CommandObjectProcessConnect
831//-------------------------------------------------------------------------
832#pragma mark CommandObjectProcessConnect
833
834class CommandObjectProcessConnect : public CommandObject
835{
836public:
837
838 class CommandOptions : public Options
839 {
840 public:
841
Greg Claytonf15996e2011-04-07 22:46:35 +0000842 CommandOptions (CommandInterpreter &interpreter) :
843 Options(interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000844 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000845 // Keep default values of all options in one place: OptionParsingStarting ()
846 OptionParsingStarting ();
Greg Claytone71e2582011-02-04 01:58:07 +0000847 }
848
849 ~CommandOptions ()
850 {
851 }
852
853 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000854 SetOptionValue (uint32_t option_idx, const char *option_arg)
Greg Claytone71e2582011-02-04 01:58:07 +0000855 {
856 Error error;
857 char short_option = (char) m_getopt_table[option_idx].val;
858
859 switch (short_option)
860 {
861 case 'p':
862 plugin_name.assign (option_arg);
863 break;
864
865 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000866 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Greg Claytone71e2582011-02-04 01:58:07 +0000867 break;
868 }
869 return error;
870 }
871
872 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000873 OptionParsingStarting ()
Greg Claytone71e2582011-02-04 01:58:07 +0000874 {
Greg Claytone71e2582011-02-04 01:58:07 +0000875 plugin_name.clear();
876 }
877
Greg Claytonb3448432011-03-24 21:19:54 +0000878 const OptionDefinition*
Greg Claytone71e2582011-02-04 01:58:07 +0000879 GetDefinitions ()
880 {
881 return g_option_table;
882 }
883
884 // Options table: Required for subclasses of Options.
885
Greg Claytonb3448432011-03-24 21:19:54 +0000886 static OptionDefinition g_option_table[];
Greg Claytone71e2582011-02-04 01:58:07 +0000887
888 // Instance variables to hold the values for command options.
889
890 std::string plugin_name;
891 };
892
893 CommandObjectProcessConnect (CommandInterpreter &interpreter) :
Greg Claytonf15996e2011-04-07 22:46:35 +0000894 CommandObject (interpreter,
895 "process connect",
896 "Connect to a remote debug service.",
897 "process connect <remote-url>",
898 0),
899 m_options (interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000900 {
901 }
902
903 ~CommandObjectProcessConnect ()
904 {
905 }
906
907
908 bool
909 Execute (Args& command,
910 CommandReturnObject &result)
911 {
912
913 TargetSP target_sp (m_interpreter.GetDebugger().GetSelectedTarget());
914 Error error;
Greg Clayton567e7f32011-09-22 04:58:26 +0000915 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytone71e2582011-02-04 01:58:07 +0000916 if (process)
917 {
918 if (process->IsAlive())
919 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000920 result.AppendErrorWithFormat ("Process %llu is currently being debugged, kill the process before connecting.\n",
Greg Claytone71e2582011-02-04 01:58:07 +0000921 process->GetID());
922 result.SetStatus (eReturnStatusFailed);
923 return false;
924 }
925 }
926
927 if (!target_sp)
928 {
929 // If there isn't a current target create one.
930 FileSpec emptyFileSpec;
Greg Claytone71e2582011-02-04 01:58:07 +0000931
932 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
933 emptyFileSpec,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000934 NULL,
Greg Claytone71e2582011-02-04 01:58:07 +0000935 false,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000936 NULL, // No platform options
Greg Claytone71e2582011-02-04 01:58:07 +0000937 target_sp);
938 if (!target_sp || error.Fail())
939 {
940 result.AppendError(error.AsCString("Error creating target"));
941 result.SetStatus (eReturnStatusFailed);
942 return false;
943 }
944 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target_sp.get());
945 }
946
947 if (command.GetArgumentCount() == 1)
948 {
949 const char *plugin_name = NULL;
950 if (!m_options.plugin_name.empty())
951 plugin_name = m_options.plugin_name.c_str();
952
953 const char *remote_url = command.GetArgumentAtIndex(0);
954 process = target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name).get();
955
956 if (process)
957 {
958 error = process->ConnectRemote (remote_url);
959
960 if (error.Fail())
961 {
962 result.AppendError(error.AsCString("Remote connect failed"));
963 result.SetStatus (eReturnStatusFailed);
964 return false;
965 }
966 }
967 else
968 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000969 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",
970 m_cmd_name.c_str());
Greg Claytone71e2582011-02-04 01:58:07 +0000971 result.SetStatus (eReturnStatusFailed);
972 }
973 }
974 else
975 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000976 result.AppendErrorWithFormat ("'%s' takes exactly one argument:\nUsage: %s\n",
Greg Claytone71e2582011-02-04 01:58:07 +0000977 m_cmd_name.c_str(),
978 m_cmd_syntax.c_str());
979 result.SetStatus (eReturnStatusFailed);
980 }
981 return result.Succeeded();
982 }
983
984 Options *
985 GetOptions ()
986 {
987 return &m_options;
988 }
989
990protected:
991
992 CommandOptions m_options;
993};
994
995
Greg Claytonb3448432011-03-24 21:19:54 +0000996OptionDefinition
Greg Claytone71e2582011-02-04 01:58:07 +0000997CommandObjectProcessConnect::CommandOptions::g_option_table[] =
998{
999 { LLDB_OPT_SET_ALL, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
1000 { 0, false, NULL, 0 , 0, NULL, 0, eArgTypeNone, NULL }
1001};
1002
1003//-------------------------------------------------------------------------
Greg Clayton0baa3942010-11-04 01:54:29 +00001004// CommandObjectProcessLoad
1005//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001006#pragma mark CommandObjectProcessLoad
Greg Clayton0baa3942010-11-04 01:54:29 +00001007
1008class CommandObjectProcessLoad : public CommandObject
1009{
1010public:
1011
1012 CommandObjectProcessLoad (CommandInterpreter &interpreter) :
1013 CommandObject (interpreter,
1014 "process load",
1015 "Load a shared library into the current process.",
1016 "process load <filename> [<filename> ...]",
1017 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
1018 {
1019 }
1020
1021 ~CommandObjectProcessLoad ()
1022 {
1023 }
1024
1025 bool
1026 Execute (Args& command,
1027 CommandReturnObject &result)
1028 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001029 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton0baa3942010-11-04 01:54:29 +00001030 if (process == NULL)
1031 {
1032 result.AppendError ("must have a valid process in order to load a shared library");
1033 result.SetStatus (eReturnStatusFailed);
1034 return false;
1035 }
1036
1037 const uint32_t argc = command.GetArgumentCount();
1038
1039 for (uint32_t i=0; i<argc; ++i)
1040 {
1041 Error error;
1042 const char *image_path = command.GetArgumentAtIndex(i);
1043 FileSpec image_spec (image_path, false);
Greg Claytonf2bf8702011-08-11 16:25:18 +00001044 process->GetTarget().GetPlatform()->ResolveRemotePath(image_spec, image_spec);
Greg Clayton0baa3942010-11-04 01:54:29 +00001045 uint32_t image_token = process->LoadImage(image_spec, error);
1046 if (image_token != LLDB_INVALID_IMAGE_TOKEN)
1047 {
1048 result.AppendMessageWithFormat ("Loading \"%s\"...ok\nImage %u loaded.\n", image_path, image_token);
1049 result.SetStatus (eReturnStatusSuccessFinishResult);
1050 }
1051 else
1052 {
1053 result.AppendErrorWithFormat ("failed to load '%s': %s", image_path, error.AsCString());
1054 result.SetStatus (eReturnStatusFailed);
1055 }
1056 }
1057 return result.Succeeded();
1058 }
1059};
1060
1061
1062//-------------------------------------------------------------------------
1063// CommandObjectProcessUnload
1064//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001065#pragma mark CommandObjectProcessUnload
Greg Clayton0baa3942010-11-04 01:54:29 +00001066
1067class CommandObjectProcessUnload : public CommandObject
1068{
1069public:
1070
1071 CommandObjectProcessUnload (CommandInterpreter &interpreter) :
1072 CommandObject (interpreter,
1073 "process unload",
1074 "Unload a shared library from the current process using the index returned by a previous call to \"process load\".",
1075 "process unload <index>",
1076 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
1077 {
1078 }
1079
1080 ~CommandObjectProcessUnload ()
1081 {
1082 }
1083
1084 bool
1085 Execute (Args& command,
1086 CommandReturnObject &result)
1087 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001088 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton0baa3942010-11-04 01:54:29 +00001089 if (process == NULL)
1090 {
1091 result.AppendError ("must have a valid process in order to load a shared library");
1092 result.SetStatus (eReturnStatusFailed);
1093 return false;
1094 }
1095
1096 const uint32_t argc = command.GetArgumentCount();
1097
1098 for (uint32_t i=0; i<argc; ++i)
1099 {
1100 const char *image_token_cstr = command.GetArgumentAtIndex(i);
1101 uint32_t image_token = Args::StringToUInt32(image_token_cstr, LLDB_INVALID_IMAGE_TOKEN, 0);
1102 if (image_token == LLDB_INVALID_IMAGE_TOKEN)
1103 {
1104 result.AppendErrorWithFormat ("invalid image index argument '%s'", image_token_cstr);
1105 result.SetStatus (eReturnStatusFailed);
1106 break;
1107 }
1108 else
1109 {
1110 Error error (process->UnloadImage(image_token));
1111 if (error.Success())
1112 {
1113 result.AppendMessageWithFormat ("Unloading shared library with index %u...ok\n", image_token);
1114 result.SetStatus (eReturnStatusSuccessFinishResult);
1115 }
1116 else
1117 {
1118 result.AppendErrorWithFormat ("failed to unload image: %s", error.AsCString());
1119 result.SetStatus (eReturnStatusFailed);
1120 break;
1121 }
1122 }
1123 }
1124 return result.Succeeded();
1125 }
1126};
1127
1128//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001129// CommandObjectProcessSignal
1130//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001131#pragma mark CommandObjectProcessSignal
Chris Lattner24943d22010-06-08 16:52:24 +00001132
1133class CommandObjectProcessSignal : public CommandObject
1134{
1135public:
1136
Greg Clayton238c0a12010-09-18 01:14:36 +00001137 CommandObjectProcessSignal (CommandInterpreter &interpreter) :
1138 CommandObject (interpreter,
1139 "process signal",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001140 "Send a UNIX signal to the current process being debugged.",
Caroline Tice43b014a2010-10-04 22:28:36 +00001141 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001142 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001143 CommandArgumentEntry arg;
1144 CommandArgumentData signal_arg;
1145
1146 // Define the first (and only) variant of this arg.
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001147 signal_arg.arg_type = eArgTypeUnixSignal;
Caroline Tice43b014a2010-10-04 22:28:36 +00001148 signal_arg.arg_repetition = eArgRepeatPlain;
1149
1150 // There is only one variant this argument could be; put it into the argument entry.
1151 arg.push_back (signal_arg);
1152
1153 // Push the data for the first argument into the m_arguments vector.
1154 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001155 }
1156
1157 ~CommandObjectProcessSignal ()
1158 {
1159 }
1160
1161 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001162 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001163 CommandReturnObject &result)
1164 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001165 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001166 if (process == NULL)
1167 {
1168 result.AppendError ("no process to signal");
1169 result.SetStatus (eReturnStatusFailed);
1170 return false;
1171 }
1172
1173 if (command.GetArgumentCount() == 1)
1174 {
Greg Clayton8f6be2a2010-10-09 01:40:57 +00001175 int signo = LLDB_INVALID_SIGNAL_NUMBER;
1176
1177 const char *signal_name = command.GetArgumentAtIndex(0);
1178 if (::isxdigit (signal_name[0]))
1179 signo = Args::StringToSInt32(signal_name, LLDB_INVALID_SIGNAL_NUMBER, 0);
1180 else
1181 signo = process->GetUnixSignals().GetSignalNumberFromName (signal_name);
1182
1183 if (signo == LLDB_INVALID_SIGNAL_NUMBER)
Chris Lattner24943d22010-06-08 16:52:24 +00001184 {
1185 result.AppendErrorWithFormat ("Invalid signal argument '%s'.\n", command.GetArgumentAtIndex(0));
1186 result.SetStatus (eReturnStatusFailed);
1187 }
1188 else
1189 {
1190 Error error (process->Signal (signo));
1191 if (error.Success())
1192 {
1193 result.SetStatus (eReturnStatusSuccessFinishResult);
1194 }
1195 else
1196 {
1197 result.AppendErrorWithFormat ("Failed to send signal %i: %s\n", signo, error.AsCString());
1198 result.SetStatus (eReturnStatusFailed);
1199 }
1200 }
1201 }
1202 else
1203 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001204 result.AppendErrorWithFormat("'%s' takes exactly one signal number argument:\nUsage: %s\n", m_cmd_name.c_str(),
Chris Lattner24943d22010-06-08 16:52:24 +00001205 m_cmd_syntax.c_str());
1206 result.SetStatus (eReturnStatusFailed);
1207 }
1208 return result.Succeeded();
1209 }
1210};
1211
1212
1213//-------------------------------------------------------------------------
1214// CommandObjectProcessInterrupt
1215//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001216#pragma mark CommandObjectProcessInterrupt
Chris Lattner24943d22010-06-08 16:52:24 +00001217
1218class CommandObjectProcessInterrupt : public CommandObject
1219{
1220public:
1221
1222
Greg Clayton238c0a12010-09-18 01:14:36 +00001223 CommandObjectProcessInterrupt (CommandInterpreter &interpreter) :
1224 CommandObject (interpreter,
1225 "process interrupt",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001226 "Interrupt the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +00001227 "process interrupt",
1228 eFlagProcessMustBeLaunched)
1229 {
1230 }
1231
1232 ~CommandObjectProcessInterrupt ()
1233 {
1234 }
1235
1236 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001237 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001238 CommandReturnObject &result)
1239 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001240 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001241 if (process == NULL)
1242 {
1243 result.AppendError ("no process to halt");
1244 result.SetStatus (eReturnStatusFailed);
1245 return false;
1246 }
1247
1248 if (command.GetArgumentCount() == 0)
1249 {
1250 Error error(process->Halt ());
1251 if (error.Success())
1252 {
1253 result.SetStatus (eReturnStatusSuccessFinishResult);
1254
1255 // Maybe we should add a "SuspendThreadPlans so we
1256 // can halt, and keep in place all the current thread plans.
1257 process->GetThreadList().DiscardThreadPlans();
1258 }
1259 else
1260 {
1261 result.AppendErrorWithFormat ("Failed to halt process: %s\n", error.AsCString());
1262 result.SetStatus (eReturnStatusFailed);
1263 }
1264 }
1265 else
1266 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001267 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner24943d22010-06-08 16:52:24 +00001268 m_cmd_name.c_str(),
1269 m_cmd_syntax.c_str());
1270 result.SetStatus (eReturnStatusFailed);
1271 }
1272 return result.Succeeded();
1273 }
1274};
1275
1276//-------------------------------------------------------------------------
1277// CommandObjectProcessKill
1278//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001279#pragma mark CommandObjectProcessKill
Chris Lattner24943d22010-06-08 16:52:24 +00001280
1281class CommandObjectProcessKill : public CommandObject
1282{
1283public:
1284
Greg Clayton238c0a12010-09-18 01:14:36 +00001285 CommandObjectProcessKill (CommandInterpreter &interpreter) :
1286 CommandObject (interpreter,
1287 "process kill",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001288 "Terminate the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +00001289 "process kill",
1290 eFlagProcessMustBeLaunched)
1291 {
1292 }
1293
1294 ~CommandObjectProcessKill ()
1295 {
1296 }
1297
1298 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001299 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001300 CommandReturnObject &result)
1301 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001302 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001303 if (process == NULL)
1304 {
1305 result.AppendError ("no process to kill");
1306 result.SetStatus (eReturnStatusFailed);
1307 return false;
1308 }
1309
1310 if (command.GetArgumentCount() == 0)
1311 {
1312 Error error (process->Destroy());
1313 if (error.Success())
1314 {
1315 result.SetStatus (eReturnStatusSuccessFinishResult);
1316 }
1317 else
1318 {
1319 result.AppendErrorWithFormat ("Failed to kill process: %s\n", error.AsCString());
1320 result.SetStatus (eReturnStatusFailed);
1321 }
1322 }
1323 else
1324 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001325 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner24943d22010-06-08 16:52:24 +00001326 m_cmd_name.c_str(),
1327 m_cmd_syntax.c_str());
1328 result.SetStatus (eReturnStatusFailed);
1329 }
1330 return result.Succeeded();
1331 }
1332};
1333
1334//-------------------------------------------------------------------------
Jim Ingham41313fc2010-06-18 01:23:09 +00001335// CommandObjectProcessStatus
1336//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001337#pragma mark CommandObjectProcessStatus
1338
Jim Ingham41313fc2010-06-18 01:23:09 +00001339class CommandObjectProcessStatus : public CommandObject
1340{
1341public:
Greg Clayton238c0a12010-09-18 01:14:36 +00001342 CommandObjectProcessStatus (CommandInterpreter &interpreter) :
1343 CommandObject (interpreter,
1344 "process status",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001345 "Show the current status and location of executing process.",
1346 "process status",
Jim Ingham41313fc2010-06-18 01:23:09 +00001347 0)
1348 {
1349 }
1350
1351 ~CommandObjectProcessStatus()
1352 {
1353 }
1354
1355
1356 bool
1357 Execute
1358 (
1359 Args& command,
Jim Ingham41313fc2010-06-18 01:23:09 +00001360 CommandReturnObject &result
1361 )
1362 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001363 Stream &strm = result.GetOutputStream();
Jim Ingham41313fc2010-06-18 01:23:09 +00001364 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Claytonb72d0f02011-04-12 05:54:46 +00001365 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
Greg Clayton567e7f32011-09-22 04:58:26 +00001366 Process *process = exe_ctx.GetProcessPtr();
1367 if (process)
Jim Ingham41313fc2010-06-18 01:23:09 +00001368 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001369 const bool only_threads_with_stop_reason = true;
1370 const uint32_t start_frame = 0;
1371 const uint32_t num_frames = 1;
1372 const uint32_t num_frames_with_source = 1;
Greg Clayton567e7f32011-09-22 04:58:26 +00001373 process->GetStatus(strm);
1374 process->GetThreadStatus (strm,
1375 only_threads_with_stop_reason,
1376 start_frame,
1377 num_frames,
1378 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001379
Jim Ingham41313fc2010-06-18 01:23:09 +00001380 }
1381 else
1382 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001383 result.AppendError ("No process.");
Jim Ingham41313fc2010-06-18 01:23:09 +00001384 result.SetStatus (eReturnStatusFailed);
1385 }
1386 return result.Succeeded();
1387 }
1388};
1389
1390//-------------------------------------------------------------------------
Caroline Tice23d6f272010-10-13 20:44:39 +00001391// CommandObjectProcessHandle
1392//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001393#pragma mark CommandObjectProcessHandle
Caroline Tice23d6f272010-10-13 20:44:39 +00001394
1395class CommandObjectProcessHandle : public CommandObject
1396{
1397public:
1398
1399 class CommandOptions : public Options
1400 {
1401 public:
1402
Greg Claytonf15996e2011-04-07 22:46:35 +00001403 CommandOptions (CommandInterpreter &interpreter) :
1404 Options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001405 {
Greg Clayton143fcc32011-04-13 00:18:08 +00001406 OptionParsingStarting ();
Caroline Tice23d6f272010-10-13 20:44:39 +00001407 }
1408
1409 ~CommandOptions ()
1410 {
1411 }
1412
1413 Error
Greg Clayton143fcc32011-04-13 00:18:08 +00001414 SetOptionValue (uint32_t option_idx, const char *option_arg)
Caroline Tice23d6f272010-10-13 20:44:39 +00001415 {
1416 Error error;
1417 char short_option = (char) m_getopt_table[option_idx].val;
1418
1419 switch (short_option)
1420 {
1421 case 's':
1422 stop = option_arg;
1423 break;
1424 case 'n':
1425 notify = option_arg;
1426 break;
1427 case 'p':
1428 pass = option_arg;
1429 break;
1430 default:
Greg Clayton9c236732011-10-26 00:56:27 +00001431 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Caroline Tice23d6f272010-10-13 20:44:39 +00001432 break;
1433 }
1434 return error;
1435 }
1436
1437 void
Greg Clayton143fcc32011-04-13 00:18:08 +00001438 OptionParsingStarting ()
Caroline Tice23d6f272010-10-13 20:44:39 +00001439 {
Caroline Tice23d6f272010-10-13 20:44:39 +00001440 stop.clear();
1441 notify.clear();
1442 pass.clear();
1443 }
1444
Greg Claytonb3448432011-03-24 21:19:54 +00001445 const OptionDefinition*
Caroline Tice23d6f272010-10-13 20:44:39 +00001446 GetDefinitions ()
1447 {
1448 return g_option_table;
1449 }
1450
1451 // Options table: Required for subclasses of Options.
1452
Greg Claytonb3448432011-03-24 21:19:54 +00001453 static OptionDefinition g_option_table[];
Caroline Tice23d6f272010-10-13 20:44:39 +00001454
1455 // Instance variables to hold the values for command options.
1456
1457 std::string stop;
1458 std::string notify;
1459 std::string pass;
1460 };
1461
1462
1463 CommandObjectProcessHandle (CommandInterpreter &interpreter) :
1464 CommandObject (interpreter,
1465 "process handle",
Caroline Ticee7471982010-10-14 21:31:13 +00001466 "Show or update what the process and debugger should do with various signals received from the OS.",
Greg Claytonf15996e2011-04-07 22:46:35 +00001467 NULL),
1468 m_options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001469 {
Caroline Ticee7471982010-10-14 21:31:13 +00001470 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 +00001471 CommandArgumentEntry arg;
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001472 CommandArgumentData signal_arg;
Caroline Tice23d6f272010-10-13 20:44:39 +00001473
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001474 signal_arg.arg_type = eArgTypeUnixSignal;
1475 signal_arg.arg_repetition = eArgRepeatStar;
Caroline Tice23d6f272010-10-13 20:44:39 +00001476
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001477 arg.push_back (signal_arg);
Caroline Tice23d6f272010-10-13 20:44:39 +00001478
1479 m_arguments.push_back (arg);
1480 }
1481
1482 ~CommandObjectProcessHandle ()
1483 {
1484 }
1485
1486 Options *
1487 GetOptions ()
1488 {
1489 return &m_options;
1490 }
1491
1492 bool
Caroline Ticee7471982010-10-14 21:31:13 +00001493 VerifyCommandOptionValue (const std::string &option, int &real_value)
Caroline Tice23d6f272010-10-13 20:44:39 +00001494 {
1495 bool okay = true;
1496
Caroline Ticee7471982010-10-14 21:31:13 +00001497 bool success = false;
1498 bool tmp_value = Args::StringToBoolean (option.c_str(), false, &success);
1499
1500 if (success && tmp_value)
1501 real_value = 1;
1502 else if (success && !tmp_value)
1503 real_value = 0;
Caroline Tice23d6f272010-10-13 20:44:39 +00001504 else
1505 {
1506 // If the value isn't 'true' or 'false', it had better be 0 or 1.
Caroline Ticee7471982010-10-14 21:31:13 +00001507 real_value = Args::StringToUInt32 (option.c_str(), 3);
1508 if (real_value != 0 && real_value != 1)
Caroline Tice23d6f272010-10-13 20:44:39 +00001509 okay = false;
1510 }
1511
1512 return okay;
1513 }
1514
Caroline Ticee7471982010-10-14 21:31:13 +00001515 void
1516 PrintSignalHeader (Stream &str)
1517 {
1518 str.Printf ("NAME PASS STOP NOTIFY\n");
1519 str.Printf ("========== ===== ===== ======\n");
1520 }
1521
1522 void
1523 PrintSignal (Stream &str, int32_t signo, const char *sig_name, UnixSignals &signals)
1524 {
1525 bool stop;
1526 bool suppress;
1527 bool notify;
1528
1529 str.Printf ("%-10s ", sig_name);
1530 if (signals.GetSignalInfo (signo, suppress, stop, notify))
1531 {
1532 bool pass = !suppress;
1533 str.Printf ("%s %s %s",
1534 (pass ? "true " : "false"),
1535 (stop ? "true " : "false"),
1536 (notify ? "true " : "false"));
1537 }
1538 str.Printf ("\n");
1539 }
1540
1541 void
1542 PrintSignalInformation (Stream &str, Args &signal_args, int num_valid_signals, UnixSignals &signals)
1543 {
1544 PrintSignalHeader (str);
1545
1546 if (num_valid_signals > 0)
1547 {
1548 size_t num_args = signal_args.GetArgumentCount();
1549 for (size_t i = 0; i < num_args; ++i)
1550 {
1551 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1552 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
1553 PrintSignal (str, signo, signal_args.GetArgumentAtIndex (i), signals);
1554 }
1555 }
1556 else // Print info for ALL signals
1557 {
1558 int32_t signo = signals.GetFirstSignalNumber();
1559 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1560 {
1561 PrintSignal (str, signo, signals.GetSignalAsCString (signo), signals);
1562 signo = signals.GetNextSignalNumber (signo);
1563 }
1564 }
1565 }
1566
Caroline Tice23d6f272010-10-13 20:44:39 +00001567 bool
1568 Execute (Args &signal_args, CommandReturnObject &result)
1569 {
1570 TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
1571
1572 if (!target_sp)
1573 {
1574 result.AppendError ("No current target;"
1575 " cannot handle signals until you have a valid target and process.\n");
1576 result.SetStatus (eReturnStatusFailed);
1577 return false;
1578 }
1579
1580 ProcessSP process_sp = target_sp->GetProcessSP();
1581
1582 if (!process_sp)
1583 {
1584 result.AppendError ("No current process; cannot handle signals until you have a valid process.\n");
1585 result.SetStatus (eReturnStatusFailed);
1586 return false;
1587 }
1588
Caroline Tice23d6f272010-10-13 20:44:39 +00001589 int stop_action = -1; // -1 means leave the current setting alone
Caroline Ticee7471982010-10-14 21:31:13 +00001590 int pass_action = -1; // -1 means leave the current setting alone
Caroline Tice23d6f272010-10-13 20:44:39 +00001591 int notify_action = -1; // -1 means leave the current setting alone
1592
1593 if (! m_options.stop.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001594 && ! VerifyCommandOptionValue (m_options.stop, stop_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001595 {
1596 result.AppendError ("Invalid argument for command option --stop; must be true or false.\n");
1597 result.SetStatus (eReturnStatusFailed);
1598 return false;
1599 }
1600
1601 if (! m_options.notify.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001602 && ! VerifyCommandOptionValue (m_options.notify, notify_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001603 {
1604 result.AppendError ("Invalid argument for command option --notify; must be true or false.\n");
1605 result.SetStatus (eReturnStatusFailed);
1606 return false;
1607 }
1608
1609 if (! m_options.pass.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001610 && ! VerifyCommandOptionValue (m_options.pass, pass_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001611 {
1612 result.AppendError ("Invalid argument for command option --pass; must be true or false.\n");
1613 result.SetStatus (eReturnStatusFailed);
1614 return false;
1615 }
1616
1617 size_t num_args = signal_args.GetArgumentCount();
1618 UnixSignals &signals = process_sp->GetUnixSignals();
1619 int num_signals_set = 0;
1620
Caroline Ticee7471982010-10-14 21:31:13 +00001621 if (num_args > 0)
Caroline Tice23d6f272010-10-13 20:44:39 +00001622 {
Caroline Ticee7471982010-10-14 21:31:13 +00001623 for (size_t i = 0; i < num_args; ++i)
Caroline Tice23d6f272010-10-13 20:44:39 +00001624 {
Caroline Ticee7471982010-10-14 21:31:13 +00001625 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1626 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
Caroline Tice23d6f272010-10-13 20:44:39 +00001627 {
Caroline Ticee7471982010-10-14 21:31:13 +00001628 // Casting the actions as bools here should be okay, because VerifyCommandOptionValue guarantees
1629 // the value is either 0 or 1.
1630 if (stop_action != -1)
1631 signals.SetShouldStop (signo, (bool) stop_action);
1632 if (pass_action != -1)
1633 {
1634 bool suppress = ! ((bool) pass_action);
1635 signals.SetShouldSuppress (signo, suppress);
1636 }
1637 if (notify_action != -1)
1638 signals.SetShouldNotify (signo, (bool) notify_action);
1639 ++num_signals_set;
Caroline Tice23d6f272010-10-13 20:44:39 +00001640 }
Caroline Ticee7471982010-10-14 21:31:13 +00001641 else
1642 {
1643 result.AppendErrorWithFormat ("Invalid signal name '%s'\n", signal_args.GetArgumentAtIndex (i));
1644 }
Caroline Tice23d6f272010-10-13 20:44:39 +00001645 }
1646 }
Caroline Ticee7471982010-10-14 21:31:13 +00001647 else
1648 {
1649 // No signal specified, if any command options were specified, update ALL signals.
1650 if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1))
1651 {
1652 if (m_interpreter.Confirm ("Do you really want to update all the signals?", false))
1653 {
1654 int32_t signo = signals.GetFirstSignalNumber();
1655 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1656 {
1657 if (notify_action != -1)
1658 signals.SetShouldNotify (signo, (bool) notify_action);
1659 if (stop_action != -1)
1660 signals.SetShouldStop (signo, (bool) stop_action);
1661 if (pass_action != -1)
1662 {
1663 bool suppress = ! ((bool) pass_action);
1664 signals.SetShouldSuppress (signo, suppress);
1665 }
1666 signo = signals.GetNextSignalNumber (signo);
1667 }
1668 }
1669 }
1670 }
1671
1672 PrintSignalInformation (result.GetOutputStream(), signal_args, num_signals_set, signals);
Caroline Tice23d6f272010-10-13 20:44:39 +00001673
1674 if (num_signals_set > 0)
1675 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1676 else
1677 result.SetStatus (eReturnStatusFailed);
1678
1679 return result.Succeeded();
1680 }
1681
1682protected:
1683
1684 CommandOptions m_options;
1685};
1686
Greg Claytonb3448432011-03-24 21:19:54 +00001687OptionDefinition
Caroline Tice23d6f272010-10-13 20:44:39 +00001688CommandObjectProcessHandle::CommandOptions::g_option_table[] =
1689{
1690{ 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." },
1691{ 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." },
1692{ LLDB_OPT_SET_1, false, "pass", 'p', required_argument, NULL, 0, eArgTypeBoolean, "Whether or not the signal should be passed to the process." },
1693{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1694};
1695
1696//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001697// CommandObjectMultiwordProcess
1698//-------------------------------------------------------------------------
1699
Greg Clayton63094e02010-06-23 01:19:29 +00001700CommandObjectMultiwordProcess::CommandObjectMultiwordProcess (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00001701 CommandObjectMultiword (interpreter,
1702 "process",
1703 "A set of commands for operating on a process.",
1704 "process <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00001705{
Greg Claytona9eb8272011-07-02 21:07:54 +00001706 LoadSubCommand ("attach", CommandObjectSP (new CommandObjectProcessAttach (interpreter)));
1707 LoadSubCommand ("launch", CommandObjectSP (new CommandObjectProcessLaunch (interpreter)));
1708 LoadSubCommand ("continue", CommandObjectSP (new CommandObjectProcessContinue (interpreter)));
1709 LoadSubCommand ("connect", CommandObjectSP (new CommandObjectProcessConnect (interpreter)));
1710 LoadSubCommand ("detach", CommandObjectSP (new CommandObjectProcessDetach (interpreter)));
1711 LoadSubCommand ("load", CommandObjectSP (new CommandObjectProcessLoad (interpreter)));
1712 LoadSubCommand ("unload", CommandObjectSP (new CommandObjectProcessUnload (interpreter)));
1713 LoadSubCommand ("signal", CommandObjectSP (new CommandObjectProcessSignal (interpreter)));
1714 LoadSubCommand ("handle", CommandObjectSP (new CommandObjectProcessHandle (interpreter)));
1715 LoadSubCommand ("status", CommandObjectSP (new CommandObjectProcessStatus (interpreter)));
Greg Clayton238c0a12010-09-18 01:14:36 +00001716 LoadSubCommand ("interrupt", CommandObjectSP (new CommandObjectProcessInterrupt (interpreter)));
Greg Claytona9eb8272011-07-02 21:07:54 +00001717 LoadSubCommand ("kill", CommandObjectSP (new CommandObjectProcessKill (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00001718}
1719
1720CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess ()
1721{
1722}
1723