blob: 16f4b9e3c36d40a2b193de67a4d5244adbb6ace7 [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 Clayton527154d2011-11-15 03:53:30 +0000260 process = target->GetPlatform()->DebugProcess (m_options.launch_info,
261 debugger,
262 target,
263 debugger.GetListener(),
264 error).get();
Greg Claytonabb33022011-11-08 02:43:13 +0000265
Greg Claytona2f74232011-02-24 22:24:29 +0000266 if (process == NULL)
267 {
Greg Clayton527154d2011-11-15 03:53:30 +0000268 result.SetError (error, "failed to launch or debug process");
Greg Claytona2f74232011-02-24 22:24:29 +0000269 return false;
270 }
Chris Lattner24943d22010-06-08 16:52:24 +0000271 }
Greg Claytonabb33022011-11-08 02:43:13 +0000272
Greg Clayton238c0a12010-09-18 01:14:36 +0000273 if (error.Success())
274 {
Greg Clayton940b1032011-02-23 00:35:02 +0000275 const char *archname = exe_module->GetArchitecture().GetArchitectureName();
Greg Claytonc1d37752010-10-18 01:45:30 +0000276
Greg Clayton444e35b2011-10-19 18:09:39 +0000277 result.AppendMessageWithFormat ("Process %llu launched: '%s' (%s)\n", process->GetID(), filename, archname);
Greg Claytond8c62532010-10-07 04:19:01 +0000278 result.SetDidChangeProcessState (true);
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000279 if (m_options.launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
Greg Clayton238c0a12010-09-18 01:14:36 +0000280 {
Greg Claytond8c62532010-10-07 04:19:01 +0000281 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
Greg Clayton238c0a12010-09-18 01:14:36 +0000282 StateType state = process->WaitForProcessToStop (NULL);
283
284 if (state == eStateStopped)
285 {
Greg Claytond8c62532010-10-07 04:19:01 +0000286 error = process->Resume();
287 if (error.Success())
288 {
289 bool synchronous_execution = m_interpreter.GetSynchronous ();
290 if (synchronous_execution)
291 {
292 state = process->WaitForProcessToStop (NULL);
Greg Clayton20206082011-11-17 01:23:07 +0000293 const bool must_be_alive = true;
294 if (!StateIsStoppedState(state, must_be_alive))
Greg Clayton395fc332011-02-15 21:59:32 +0000295 {
Greg Clayton527154d2011-11-15 03:53:30 +0000296 result.AppendErrorWithFormat ("process isn't stopped: %s", StateAsCString(state));
Greg Clayton395fc332011-02-15 21:59:32 +0000297 }
Greg Claytond8c62532010-10-07 04:19:01 +0000298 result.SetDidChangeProcessState (true);
299 result.SetStatus (eReturnStatusSuccessFinishResult);
300 }
301 else
302 {
303 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
304 }
305 }
Greg Clayton395fc332011-02-15 21:59:32 +0000306 else
307 {
Greg Clayton527154d2011-11-15 03:53:30 +0000308 result.AppendErrorWithFormat ("process resume at entry point failed: %s", error.AsCString());
Greg Clayton395fc332011-02-15 21:59:32 +0000309 result.SetStatus (eReturnStatusFailed);
310 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000311 }
Greg Clayton395fc332011-02-15 21:59:32 +0000312 else
313 {
Greg Clayton527154d2011-11-15 03:53:30 +0000314 result.AppendErrorWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
Greg Clayton395fc332011-02-15 21:59:32 +0000315 result.SetStatus (eReturnStatusFailed);
316 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000317 }
318 }
Greg Clayton395fc332011-02-15 21:59:32 +0000319 else
320 {
Greg Claytona9eb8272011-07-02 21:07:54 +0000321 result.AppendErrorWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton395fc332011-02-15 21:59:32 +0000322 result.SetStatus (eReturnStatusFailed);
323 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000324
Chris Lattner24943d22010-06-08 16:52:24 +0000325 return result.Succeeded();
326 }
327
Jim Ingham767af882010-07-07 03:36:20 +0000328 virtual const char *GetRepeatCommand (Args &current_command_args, uint32_t index)
329 {
330 // No repeat for "process launch"...
331 return "";
332 }
333
Chris Lattner24943d22010-06-08 16:52:24 +0000334protected:
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000335 ProcessLaunchCommandOptions m_options;
Chris Lattner24943d22010-06-08 16:52:24 +0000336};
337
338
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000339//#define SET1 LLDB_OPT_SET_1
340//#define SET2 LLDB_OPT_SET_2
341//#define SET3 LLDB_OPT_SET_3
342//
343//OptionDefinition
344//CommandObjectProcessLaunch::CommandOptions::g_option_table[] =
345//{
346//{ 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."},
347//{ SET1 , false, "stdin", 'i', required_argument, NULL, 0, eArgTypePath, "Redirect stdin for the process to <path>."},
348//{ SET1 , false, "stdout", 'o', required_argument, NULL, 0, eArgTypePath, "Redirect stdout for the process to <path>."},
349//{ SET1 , false, "stderr", 'e', required_argument, NULL, 0, eArgTypePath, "Redirect stderr for the process to <path>."},
350//{ SET1 | SET2 | SET3, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
351//{ 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."},
352//{ SET3, false, "no-stdio", 'n', no_argument, NULL, 0, eArgTypeNone, "Do not set up for terminal I/O to go to running process."},
353//{ SET1 | SET2 | SET3, false, "working-dir", 'w', required_argument, NULL, 0, eArgTypePath, "Set the current working directory to <path> when running the inferior."},
354//{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
355//};
356//
357//#undef SET1
358//#undef SET2
359//#undef SET3
Chris Lattner24943d22010-06-08 16:52:24 +0000360
361//-------------------------------------------------------------------------
362// CommandObjectProcessAttach
363//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000364#pragma mark CommandObjectProcessAttach
Chris Lattner24943d22010-06-08 16:52:24 +0000365class CommandObjectProcessAttach : public CommandObject
366{
367public:
368
Chris Lattner24943d22010-06-08 16:52:24 +0000369 class CommandOptions : public Options
370 {
371 public:
372
Greg Claytonf15996e2011-04-07 22:46:35 +0000373 CommandOptions (CommandInterpreter &interpreter) :
374 Options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000375 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000376 // Keep default values of all options in one place: OptionParsingStarting ()
377 OptionParsingStarting ();
Chris Lattner24943d22010-06-08 16:52:24 +0000378 }
379
380 ~CommandOptions ()
381 {
382 }
383
384 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000385 SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +0000386 {
387 Error error;
388 char short_option = (char) m_getopt_table[option_idx].val;
389 bool success = false;
390 switch (short_option)
391 {
392 case 'p':
Chris Lattner24943d22010-06-08 16:52:24 +0000393 {
Greg Clayton527154d2011-11-15 03:53:30 +0000394 lldb::pid_t pid = Args::StringToUInt32 (option_arg, LLDB_INVALID_PROCESS_ID, 0, &success);
395 if (!success || pid == LLDB_INVALID_PROCESS_ID)
396 {
397 error.SetErrorStringWithFormat("invalid process ID '%s'", option_arg);
398 }
399 else
400 {
401 attach_info.SetProcessID (pid);
402 }
Chris Lattner24943d22010-06-08 16:52:24 +0000403 }
404 break;
405
406 case 'P':
Greg Clayton527154d2011-11-15 03:53:30 +0000407 attach_info.SetProcessPluginName (option_arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000408 break;
409
410 case 'n':
Greg Clayton527154d2011-11-15 03:53:30 +0000411 attach_info.GetExecutableFile().SetFile(option_arg, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000412 break;
413
414 case 'w':
Greg Clayton527154d2011-11-15 03:53:30 +0000415 attach_info.SetWaitForLaunch(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000416 break;
417
418 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000419 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner24943d22010-06-08 16:52:24 +0000420 break;
421 }
422 return error;
423 }
424
425 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000426 OptionParsingStarting ()
Chris Lattner24943d22010-06-08 16:52:24 +0000427 {
Greg Clayton527154d2011-11-15 03:53:30 +0000428 attach_info.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000429 }
430
Greg Claytonb3448432011-03-24 21:19:54 +0000431 const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +0000432 GetDefinitions ()
433 {
434 return g_option_table;
435 }
436
Jim Ingham7508e732010-08-09 23:31:02 +0000437 virtual bool
Greg Claytonf15996e2011-04-07 22:46:35 +0000438 HandleOptionArgumentCompletion (Args &input,
Jim Ingham7508e732010-08-09 23:31:02 +0000439 int cursor_index,
440 int char_pos,
441 OptionElementVector &opt_element_vector,
442 int opt_element_index,
443 int match_start_point,
444 int max_return_elements,
445 bool &word_complete,
446 StringList &matches)
447 {
448 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
449 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
450
451 // We are only completing the name option for now...
452
Greg Claytonb3448432011-03-24 21:19:54 +0000453 const OptionDefinition *opt_defs = GetDefinitions();
Jim Ingham7508e732010-08-09 23:31:02 +0000454 if (opt_defs[opt_defs_index].short_option == 'n')
455 {
456 // Are we in the name?
457
458 // Look to see if there is a -P argument provided, and if so use that plugin, otherwise
459 // use the default plugin.
Jim Ingham7508e732010-08-09 23:31:02 +0000460
461 const char *partial_name = NULL;
462 partial_name = input.GetArgumentAtIndex(opt_arg_pos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000463
Greg Claytonb72d0f02011-04-12 05:54:46 +0000464 PlatformSP platform_sp (m_interpreter.GetPlatform (true));
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000465 if (platform_sp)
Jim Ingham7508e732010-08-09 23:31:02 +0000466 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000467 ProcessInstanceInfoList process_infos;
468 ProcessInstanceInfoMatch match_info;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000469 if (partial_name)
470 {
Greg Clayton527154d2011-11-15 03:53:30 +0000471 match_info.GetProcessInfo().GetExecutableFile().SetFile(partial_name, false);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000472 match_info.SetNameMatchType(eNameMatchStartsWith);
473 }
474 platform_sp->FindProcesses (match_info, process_infos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000475 const uint32_t num_matches = process_infos.GetSize();
476 if (num_matches > 0)
477 {
478 for (uint32_t i=0; i<num_matches; ++i)
479 {
480 matches.AppendString (process_infos.GetProcessNameAtIndex(i),
481 process_infos.GetProcessNameLengthAtIndex(i));
482 }
483 }
Jim Ingham7508e732010-08-09 23:31:02 +0000484 }
485 }
486
487 return false;
488 }
489
Chris Lattner24943d22010-06-08 16:52:24 +0000490 // Options table: Required for subclasses of Options.
491
Greg Claytonb3448432011-03-24 21:19:54 +0000492 static OptionDefinition g_option_table[];
Chris Lattner24943d22010-06-08 16:52:24 +0000493
494 // Instance variables to hold the values for command options.
495
Greg Clayton527154d2011-11-15 03:53:30 +0000496 ProcessAttachInfo attach_info;
Chris Lattner24943d22010-06-08 16:52:24 +0000497 };
498
Greg Clayton238c0a12010-09-18 01:14:36 +0000499 CommandObjectProcessAttach (CommandInterpreter &interpreter) :
500 CommandObject (interpreter,
501 "process attach",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000502 "Attach to a process.",
Greg Claytonf15996e2011-04-07 22:46:35 +0000503 "process attach <cmd-options>"),
504 m_options (interpreter)
Jim Ingham7508e732010-08-09 23:31:02 +0000505 {
Jim Ingham7508e732010-08-09 23:31:02 +0000506 }
507
508 ~CommandObjectProcessAttach ()
509 {
510 }
511
512 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000513 Execute (Args& command,
Jim Ingham7508e732010-08-09 23:31:02 +0000514 CommandReturnObject &result)
515 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000516 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Jim Inghamee940e22011-09-15 01:08:57 +0000517 // N.B. The attach should be synchronous. It doesn't help much to get the prompt back between initiating the attach
518 // and the target actually stopping. So even if the interpreter is set to be asynchronous, we wait for the stop
519 // ourselves here.
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000520
Greg Clayton567e7f32011-09-22 04:58:26 +0000521 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytona2f74232011-02-24 22:24:29 +0000522 StateType state = eStateInvalid;
Jim Ingham7508e732010-08-09 23:31:02 +0000523 if (process)
524 {
Greg Claytona2f74232011-02-24 22:24:29 +0000525 state = process->GetState();
526 if (process->IsAlive() && state != eStateConnected)
Jim Ingham7508e732010-08-09 23:31:02 +0000527 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000528 result.AppendErrorWithFormat ("Process %llu is currently being debugged, kill the process before attaching.\n",
Jim Ingham7508e732010-08-09 23:31:02 +0000529 process->GetID());
530 result.SetStatus (eReturnStatusFailed);
531 return false;
532 }
533 }
534
535 if (target == NULL)
536 {
537 // If there isn't a current target create one.
538 TargetSP new_target_sp;
539 FileSpec emptyFileSpec;
Jim Ingham7508e732010-08-09 23:31:02 +0000540 Error error;
541
Greg Clayton238c0a12010-09-18 01:14:36 +0000542 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
543 emptyFileSpec,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000544 NULL,
Greg Clayton238c0a12010-09-18 01:14:36 +0000545 false,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000546 NULL, // No platform options
Greg Clayton238c0a12010-09-18 01:14:36 +0000547 new_target_sp);
Jim Ingham7508e732010-08-09 23:31:02 +0000548 target = new_target_sp.get();
549 if (target == NULL || error.Fail())
550 {
Greg Claytone71e2582011-02-04 01:58:07 +0000551 result.AppendError(error.AsCString("Error creating target"));
Jim Ingham7508e732010-08-09 23:31:02 +0000552 return false;
553 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000554 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target);
Jim Ingham7508e732010-08-09 23:31:02 +0000555 }
556
557 // Record the old executable module, we want to issue a warning if the process of attaching changed the
558 // current executable (like somebody said "file foo" then attached to a PID whose executable was bar.)
559
560 ModuleSP old_exec_module_sp = target->GetExecutableModule();
561 ArchSpec old_arch_spec = target->GetArchitecture();
562
563 if (command.GetArgumentCount())
564 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000565 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 +0000566 result.SetStatus (eReturnStatusFailed);
567 }
568 else
569 {
Greg Claytona2f74232011-02-24 22:24:29 +0000570 if (state != eStateConnected)
571 {
Greg Clayton527154d2011-11-15 03:53:30 +0000572 const char *plugin_name = m_options.attach_info.GetProcessPluginName();
Greg Claytona2f74232011-02-24 22:24:29 +0000573 process = target->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name).get();
574 }
Jim Ingham7508e732010-08-09 23:31:02 +0000575
576 if (process)
577 {
578 Error error;
Greg Clayton527154d2011-11-15 03:53:30 +0000579 // If no process info was specified, then use the target executable
580 // name as the process to attach to by default
581 if (!m_options.attach_info.ProcessInfoSpecified ())
Jim Ingham4805a1c2010-09-15 01:34:14 +0000582 {
583 if (old_exec_module_sp)
Greg Clayton527154d2011-11-15 03:53:30 +0000584 m_options.attach_info.GetExecutableFile().GetFilename() = old_exec_module_sp->GetFileSpec().GetFilename();
Jim Ingham4805a1c2010-09-15 01:34:14 +0000585
Greg Clayton527154d2011-11-15 03:53:30 +0000586 if (!m_options.attach_info.ProcessInfoSpecified ())
587 {
588 error.SetErrorString ("no process specified, create a target with a file, or specify the --pid or --name command option");
589 }
590 }
591
592 if (error.Success())
593 {
594 error = process->Attach (m_options.attach_info);
595
Jim Ingham4805a1c2010-09-15 01:34:14 +0000596 if (error.Success())
597 {
598 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
599 }
Jim Ingham7508e732010-08-09 23:31:02 +0000600 else
601 {
Greg Clayton527154d2011-11-15 03:53:30 +0000602 result.AppendErrorWithFormat ("attach failed: %s\n", error.AsCString());
Jim Ingham4805a1c2010-09-15 01:34:14 +0000603 result.SetStatus (eReturnStatusFailed);
604 return false;
Jim Ingham7508e732010-08-09 23:31:02 +0000605 }
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000606 // If we're synchronous, wait for the stopped event and report that.
607 // Otherwise just return.
608 // FIXME: in the async case it will now be possible to get to the command
609 // interpreter with a state eStateAttaching. Make sure we handle that correctly.
Jim Inghamee940e22011-09-15 01:08:57 +0000610 StateType state = process->WaitForProcessToStop (NULL);
Greg Clayton527154d2011-11-15 03:53:30 +0000611
Jim Inghamee940e22011-09-15 01:08:57 +0000612 result.SetDidChangeProcessState (true);
Greg Clayton444e35b2011-10-19 18:09:39 +0000613 result.AppendMessageWithFormat ("Process %llu %s\n", process->GetID(), StateAsCString (state));
Jim Inghamee940e22011-09-15 01:08:57 +0000614 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Jim Ingham7508e732010-08-09 23:31:02 +0000615 }
Jim Ingham7508e732010-08-09 23:31:02 +0000616 }
617 }
618
619 if (result.Succeeded())
620 {
621 // Okay, we're done. Last step is to warn if the executable module has changed:
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000622 char new_path[PATH_MAX];
Greg Clayton5beb99d2011-08-11 02:48:45 +0000623 ModuleSP new_exec_module_sp (target->GetExecutableModule());
Jim Ingham7508e732010-08-09 23:31:02 +0000624 if (!old_exec_module_sp)
625 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000626 // We might not have a module if we attached to a raw pid...
Greg Clayton5beb99d2011-08-11 02:48:45 +0000627 if (new_exec_module_sp)
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000628 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000629 new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000630 result.AppendMessageWithFormat("Executable module set to \"%s\".\n", new_path);
631 }
Jim Ingham7508e732010-08-09 23:31:02 +0000632 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000633 else if (old_exec_module_sp->GetFileSpec() != new_exec_module_sp->GetFileSpec())
Jim Ingham7508e732010-08-09 23:31:02 +0000634 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000635 char old_path[PATH_MAX];
Jim Ingham7508e732010-08-09 23:31:02 +0000636
Greg Clayton5beb99d2011-08-11 02:48:45 +0000637 old_exec_module_sp->GetFileSpec().GetPath (old_path, PATH_MAX);
638 new_exec_module_sp->GetFileSpec().GetPath (new_path, PATH_MAX);
Jim Ingham7508e732010-08-09 23:31:02 +0000639
640 result.AppendWarningWithFormat("Executable module changed from \"%s\" to \"%s\".\n",
641 old_path, new_path);
642 }
643
644 if (!old_arch_spec.IsValid())
645 {
Greg Clayton940b1032011-02-23 00:35:02 +0000646 result.AppendMessageWithFormat ("Architecture set to: %s.\n", target->GetArchitecture().GetArchitectureName());
Jim Ingham7508e732010-08-09 23:31:02 +0000647 }
648 else if (old_arch_spec != target->GetArchitecture())
649 {
650 result.AppendWarningWithFormat("Architecture changed from %s to %s.\n",
Greg Clayton940b1032011-02-23 00:35:02 +0000651 old_arch_spec.GetArchitectureName(), target->GetArchitecture().GetArchitectureName());
Jim Ingham7508e732010-08-09 23:31:02 +0000652 }
653 }
654 return result.Succeeded();
655 }
656
657 Options *
658 GetOptions ()
659 {
660 return &m_options;
661 }
662
Chris Lattner24943d22010-06-08 16:52:24 +0000663protected:
664
665 CommandOptions m_options;
666};
667
668
Greg Claytonb3448432011-03-24 21:19:54 +0000669OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000670CommandObjectProcessAttach::CommandOptions::g_option_table[] =
671{
Caroline Tice4d6675c2010-10-01 19:59:14 +0000672{ LLDB_OPT_SET_ALL, false, "plugin", 'P', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
673{ LLDB_OPT_SET_1, false, "pid", 'p', required_argument, NULL, 0, eArgTypePid, "The process ID of an existing process to attach to."},
674{ LLDB_OPT_SET_2, false, "name", 'n', required_argument, NULL, 0, eArgTypeProcessName, "The name of the process to attach to."},
675{ LLDB_OPT_SET_2, false, "waitfor",'w', no_argument, NULL, 0, eArgTypeNone, "Wait for the the process with <process-name> to launch."},
676{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000677};
678
679//-------------------------------------------------------------------------
680// CommandObjectProcessContinue
681//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000682#pragma mark CommandObjectProcessContinue
Chris Lattner24943d22010-06-08 16:52:24 +0000683
684class CommandObjectProcessContinue : public CommandObject
685{
686public:
687
Greg Clayton238c0a12010-09-18 01:14:36 +0000688 CommandObjectProcessContinue (CommandInterpreter &interpreter) :
689 CommandObject (interpreter,
690 "process continue",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000691 "Continue execution of all threads in the current process.",
Chris Lattner24943d22010-06-08 16:52:24 +0000692 "process continue",
693 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
694 {
695 }
696
697
698 ~CommandObjectProcessContinue ()
699 {
700 }
701
702 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000703 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000704 CommandReturnObject &result)
705 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000706 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton238c0a12010-09-18 01:14:36 +0000707 bool synchronous_execution = m_interpreter.GetSynchronous ();
Chris Lattner24943d22010-06-08 16:52:24 +0000708
709 if (process == NULL)
710 {
711 result.AppendError ("no process to continue");
712 result.SetStatus (eReturnStatusFailed);
713 return false;
714 }
715
716 StateType state = process->GetState();
717 if (state == eStateStopped)
718 {
719 if (command.GetArgumentCount() != 0)
720 {
721 result.AppendErrorWithFormat ("The '%s' command does not take any arguments.\n", m_cmd_name.c_str());
722 result.SetStatus (eReturnStatusFailed);
723 return false;
724 }
725
726 const uint32_t num_threads = process->GetThreadList().GetSize();
727
728 // Set the actions that the threads should each take when resuming
729 for (uint32_t idx=0; idx<num_threads; ++idx)
730 {
731 process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState (eStateRunning);
732 }
733
734 Error error(process->Resume());
735 if (error.Success())
736 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000737 result.AppendMessageWithFormat ("Process %llu resuming\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000738 if (synchronous_execution)
739 {
Greg Claytonbef15832010-07-14 00:18:15 +0000740 state = process->WaitForProcessToStop (NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000741
742 result.SetDidChangeProcessState (true);
Greg Clayton444e35b2011-10-19 18:09:39 +0000743 result.AppendMessageWithFormat ("Process %llu %s\n", process->GetID(), StateAsCString (state));
Chris Lattner24943d22010-06-08 16:52:24 +0000744 result.SetStatus (eReturnStatusSuccessFinishNoResult);
745 }
746 else
747 {
748 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
749 }
750 }
751 else
752 {
753 result.AppendErrorWithFormat("Failed to resume process: %s.\n", error.AsCString());
754 result.SetStatus (eReturnStatusFailed);
755 }
756 }
757 else
758 {
759 result.AppendErrorWithFormat ("Process cannot be continued from its current state (%s).\n",
760 StateAsCString(state));
761 result.SetStatus (eReturnStatusFailed);
762 }
763 return result.Succeeded();
764 }
765};
766
767//-------------------------------------------------------------------------
768// CommandObjectProcessDetach
769//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000770#pragma mark CommandObjectProcessDetach
Chris Lattner24943d22010-06-08 16:52:24 +0000771
772class CommandObjectProcessDetach : public CommandObject
773{
774public:
775
Greg Clayton238c0a12010-09-18 01:14:36 +0000776 CommandObjectProcessDetach (CommandInterpreter &interpreter) :
777 CommandObject (interpreter,
778 "process detach",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000779 "Detach from the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +0000780 "process detach",
781 eFlagProcessMustBeLaunched)
782 {
783 }
784
785 ~CommandObjectProcessDetach ()
786 {
787 }
788
789 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000790 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000791 CommandReturnObject &result)
792 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000793 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000794 if (process == NULL)
795 {
796 result.AppendError ("must have a valid process in order to detach");
797 result.SetStatus (eReturnStatusFailed);
798 return false;
799 }
800
Greg Clayton444e35b2011-10-19 18:09:39 +0000801 result.AppendMessageWithFormat ("Detaching from process %llu\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000802 Error error (process->Detach());
803 if (error.Success())
804 {
805 result.SetStatus (eReturnStatusSuccessFinishResult);
806 }
807 else
808 {
809 result.AppendErrorWithFormat ("Detach failed: %s\n", error.AsCString());
810 result.SetStatus (eReturnStatusFailed);
811 return false;
812 }
813 return result.Succeeded();
814 }
815};
816
817//-------------------------------------------------------------------------
Greg Claytone71e2582011-02-04 01:58:07 +0000818// CommandObjectProcessConnect
819//-------------------------------------------------------------------------
820#pragma mark CommandObjectProcessConnect
821
822class CommandObjectProcessConnect : public CommandObject
823{
824public:
825
826 class CommandOptions : public Options
827 {
828 public:
829
Greg Claytonf15996e2011-04-07 22:46:35 +0000830 CommandOptions (CommandInterpreter &interpreter) :
831 Options(interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000832 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000833 // Keep default values of all options in one place: OptionParsingStarting ()
834 OptionParsingStarting ();
Greg Claytone71e2582011-02-04 01:58:07 +0000835 }
836
837 ~CommandOptions ()
838 {
839 }
840
841 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000842 SetOptionValue (uint32_t option_idx, const char *option_arg)
Greg Claytone71e2582011-02-04 01:58:07 +0000843 {
844 Error error;
845 char short_option = (char) m_getopt_table[option_idx].val;
846
847 switch (short_option)
848 {
849 case 'p':
850 plugin_name.assign (option_arg);
851 break;
852
853 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000854 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Greg Claytone71e2582011-02-04 01:58:07 +0000855 break;
856 }
857 return error;
858 }
859
860 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000861 OptionParsingStarting ()
Greg Claytone71e2582011-02-04 01:58:07 +0000862 {
Greg Claytone71e2582011-02-04 01:58:07 +0000863 plugin_name.clear();
864 }
865
Greg Claytonb3448432011-03-24 21:19:54 +0000866 const OptionDefinition*
Greg Claytone71e2582011-02-04 01:58:07 +0000867 GetDefinitions ()
868 {
869 return g_option_table;
870 }
871
872 // Options table: Required for subclasses of Options.
873
Greg Claytonb3448432011-03-24 21:19:54 +0000874 static OptionDefinition g_option_table[];
Greg Claytone71e2582011-02-04 01:58:07 +0000875
876 // Instance variables to hold the values for command options.
877
878 std::string plugin_name;
879 };
880
881 CommandObjectProcessConnect (CommandInterpreter &interpreter) :
Greg Claytonf15996e2011-04-07 22:46:35 +0000882 CommandObject (interpreter,
883 "process connect",
884 "Connect to a remote debug service.",
885 "process connect <remote-url>",
886 0),
887 m_options (interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000888 {
889 }
890
891 ~CommandObjectProcessConnect ()
892 {
893 }
894
895
896 bool
897 Execute (Args& command,
898 CommandReturnObject &result)
899 {
900
901 TargetSP target_sp (m_interpreter.GetDebugger().GetSelectedTarget());
902 Error error;
Greg Clayton567e7f32011-09-22 04:58:26 +0000903 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytone71e2582011-02-04 01:58:07 +0000904 if (process)
905 {
906 if (process->IsAlive())
907 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000908 result.AppendErrorWithFormat ("Process %llu is currently being debugged, kill the process before connecting.\n",
Greg Claytone71e2582011-02-04 01:58:07 +0000909 process->GetID());
910 result.SetStatus (eReturnStatusFailed);
911 return false;
912 }
913 }
914
915 if (!target_sp)
916 {
917 // If there isn't a current target create one.
918 FileSpec emptyFileSpec;
Greg Claytone71e2582011-02-04 01:58:07 +0000919
920 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
921 emptyFileSpec,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000922 NULL,
Greg Claytone71e2582011-02-04 01:58:07 +0000923 false,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000924 NULL, // No platform options
Greg Claytone71e2582011-02-04 01:58:07 +0000925 target_sp);
926 if (!target_sp || error.Fail())
927 {
928 result.AppendError(error.AsCString("Error creating target"));
929 result.SetStatus (eReturnStatusFailed);
930 return false;
931 }
932 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target_sp.get());
933 }
934
935 if (command.GetArgumentCount() == 1)
936 {
937 const char *plugin_name = NULL;
938 if (!m_options.plugin_name.empty())
939 plugin_name = m_options.plugin_name.c_str();
940
941 const char *remote_url = command.GetArgumentAtIndex(0);
942 process = target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name).get();
943
944 if (process)
945 {
946 error = process->ConnectRemote (remote_url);
947
948 if (error.Fail())
949 {
950 result.AppendError(error.AsCString("Remote connect failed"));
951 result.SetStatus (eReturnStatusFailed);
952 return false;
953 }
954 }
955 else
956 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000957 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",
958 m_cmd_name.c_str());
Greg Claytone71e2582011-02-04 01:58:07 +0000959 result.SetStatus (eReturnStatusFailed);
960 }
961 }
962 else
963 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000964 result.AppendErrorWithFormat ("'%s' takes exactly one argument:\nUsage: %s\n",
Greg Claytone71e2582011-02-04 01:58:07 +0000965 m_cmd_name.c_str(),
966 m_cmd_syntax.c_str());
967 result.SetStatus (eReturnStatusFailed);
968 }
969 return result.Succeeded();
970 }
971
972 Options *
973 GetOptions ()
974 {
975 return &m_options;
976 }
977
978protected:
979
980 CommandOptions m_options;
981};
982
983
Greg Claytonb3448432011-03-24 21:19:54 +0000984OptionDefinition
Greg Claytone71e2582011-02-04 01:58:07 +0000985CommandObjectProcessConnect::CommandOptions::g_option_table[] =
986{
987 { LLDB_OPT_SET_ALL, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
988 { 0, false, NULL, 0 , 0, NULL, 0, eArgTypeNone, NULL }
989};
990
991//-------------------------------------------------------------------------
Greg Clayton0baa3942010-11-04 01:54:29 +0000992// CommandObjectProcessLoad
993//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000994#pragma mark CommandObjectProcessLoad
Greg Clayton0baa3942010-11-04 01:54:29 +0000995
996class CommandObjectProcessLoad : public CommandObject
997{
998public:
999
1000 CommandObjectProcessLoad (CommandInterpreter &interpreter) :
1001 CommandObject (interpreter,
1002 "process load",
1003 "Load a shared library into the current process.",
1004 "process load <filename> [<filename> ...]",
1005 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
1006 {
1007 }
1008
1009 ~CommandObjectProcessLoad ()
1010 {
1011 }
1012
1013 bool
1014 Execute (Args& command,
1015 CommandReturnObject &result)
1016 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001017 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton0baa3942010-11-04 01:54:29 +00001018 if (process == NULL)
1019 {
1020 result.AppendError ("must have a valid process in order to load a shared library");
1021 result.SetStatus (eReturnStatusFailed);
1022 return false;
1023 }
1024
1025 const uint32_t argc = command.GetArgumentCount();
1026
1027 for (uint32_t i=0; i<argc; ++i)
1028 {
1029 Error error;
1030 const char *image_path = command.GetArgumentAtIndex(i);
1031 FileSpec image_spec (image_path, false);
Greg Claytonf2bf8702011-08-11 16:25:18 +00001032 process->GetTarget().GetPlatform()->ResolveRemotePath(image_spec, image_spec);
Greg Clayton0baa3942010-11-04 01:54:29 +00001033 uint32_t image_token = process->LoadImage(image_spec, error);
1034 if (image_token != LLDB_INVALID_IMAGE_TOKEN)
1035 {
1036 result.AppendMessageWithFormat ("Loading \"%s\"...ok\nImage %u loaded.\n", image_path, image_token);
1037 result.SetStatus (eReturnStatusSuccessFinishResult);
1038 }
1039 else
1040 {
1041 result.AppendErrorWithFormat ("failed to load '%s': %s", image_path, error.AsCString());
1042 result.SetStatus (eReturnStatusFailed);
1043 }
1044 }
1045 return result.Succeeded();
1046 }
1047};
1048
1049
1050//-------------------------------------------------------------------------
1051// CommandObjectProcessUnload
1052//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001053#pragma mark CommandObjectProcessUnload
Greg Clayton0baa3942010-11-04 01:54:29 +00001054
1055class CommandObjectProcessUnload : public CommandObject
1056{
1057public:
1058
1059 CommandObjectProcessUnload (CommandInterpreter &interpreter) :
1060 CommandObject (interpreter,
1061 "process unload",
1062 "Unload a shared library from the current process using the index returned by a previous call to \"process load\".",
1063 "process unload <index>",
1064 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
1065 {
1066 }
1067
1068 ~CommandObjectProcessUnload ()
1069 {
1070 }
1071
1072 bool
1073 Execute (Args& command,
1074 CommandReturnObject &result)
1075 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001076 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton0baa3942010-11-04 01:54:29 +00001077 if (process == NULL)
1078 {
1079 result.AppendError ("must have a valid process in order to load a shared library");
1080 result.SetStatus (eReturnStatusFailed);
1081 return false;
1082 }
1083
1084 const uint32_t argc = command.GetArgumentCount();
1085
1086 for (uint32_t i=0; i<argc; ++i)
1087 {
1088 const char *image_token_cstr = command.GetArgumentAtIndex(i);
1089 uint32_t image_token = Args::StringToUInt32(image_token_cstr, LLDB_INVALID_IMAGE_TOKEN, 0);
1090 if (image_token == LLDB_INVALID_IMAGE_TOKEN)
1091 {
1092 result.AppendErrorWithFormat ("invalid image index argument '%s'", image_token_cstr);
1093 result.SetStatus (eReturnStatusFailed);
1094 break;
1095 }
1096 else
1097 {
1098 Error error (process->UnloadImage(image_token));
1099 if (error.Success())
1100 {
1101 result.AppendMessageWithFormat ("Unloading shared library with index %u...ok\n", image_token);
1102 result.SetStatus (eReturnStatusSuccessFinishResult);
1103 }
1104 else
1105 {
1106 result.AppendErrorWithFormat ("failed to unload image: %s", error.AsCString());
1107 result.SetStatus (eReturnStatusFailed);
1108 break;
1109 }
1110 }
1111 }
1112 return result.Succeeded();
1113 }
1114};
1115
1116//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001117// CommandObjectProcessSignal
1118//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001119#pragma mark CommandObjectProcessSignal
Chris Lattner24943d22010-06-08 16:52:24 +00001120
1121class CommandObjectProcessSignal : public CommandObject
1122{
1123public:
1124
Greg Clayton238c0a12010-09-18 01:14:36 +00001125 CommandObjectProcessSignal (CommandInterpreter &interpreter) :
1126 CommandObject (interpreter,
1127 "process signal",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001128 "Send a UNIX signal to the current process being debugged.",
Caroline Tice43b014a2010-10-04 22:28:36 +00001129 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001130 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001131 CommandArgumentEntry arg;
1132 CommandArgumentData signal_arg;
1133
1134 // Define the first (and only) variant of this arg.
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001135 signal_arg.arg_type = eArgTypeUnixSignal;
Caroline Tice43b014a2010-10-04 22:28:36 +00001136 signal_arg.arg_repetition = eArgRepeatPlain;
1137
1138 // There is only one variant this argument could be; put it into the argument entry.
1139 arg.push_back (signal_arg);
1140
1141 // Push the data for the first argument into the m_arguments vector.
1142 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001143 }
1144
1145 ~CommandObjectProcessSignal ()
1146 {
1147 }
1148
1149 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001150 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001151 CommandReturnObject &result)
1152 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001153 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001154 if (process == NULL)
1155 {
1156 result.AppendError ("no process to signal");
1157 result.SetStatus (eReturnStatusFailed);
1158 return false;
1159 }
1160
1161 if (command.GetArgumentCount() == 1)
1162 {
Greg Clayton8f6be2a2010-10-09 01:40:57 +00001163 int signo = LLDB_INVALID_SIGNAL_NUMBER;
1164
1165 const char *signal_name = command.GetArgumentAtIndex(0);
1166 if (::isxdigit (signal_name[0]))
1167 signo = Args::StringToSInt32(signal_name, LLDB_INVALID_SIGNAL_NUMBER, 0);
1168 else
1169 signo = process->GetUnixSignals().GetSignalNumberFromName (signal_name);
1170
1171 if (signo == LLDB_INVALID_SIGNAL_NUMBER)
Chris Lattner24943d22010-06-08 16:52:24 +00001172 {
1173 result.AppendErrorWithFormat ("Invalid signal argument '%s'.\n", command.GetArgumentAtIndex(0));
1174 result.SetStatus (eReturnStatusFailed);
1175 }
1176 else
1177 {
1178 Error error (process->Signal (signo));
1179 if (error.Success())
1180 {
1181 result.SetStatus (eReturnStatusSuccessFinishResult);
1182 }
1183 else
1184 {
1185 result.AppendErrorWithFormat ("Failed to send signal %i: %s\n", signo, error.AsCString());
1186 result.SetStatus (eReturnStatusFailed);
1187 }
1188 }
1189 }
1190 else
1191 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001192 result.AppendErrorWithFormat("'%s' takes exactly one signal number argument:\nUsage: %s\n", m_cmd_name.c_str(),
Chris Lattner24943d22010-06-08 16:52:24 +00001193 m_cmd_syntax.c_str());
1194 result.SetStatus (eReturnStatusFailed);
1195 }
1196 return result.Succeeded();
1197 }
1198};
1199
1200
1201//-------------------------------------------------------------------------
1202// CommandObjectProcessInterrupt
1203//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001204#pragma mark CommandObjectProcessInterrupt
Chris Lattner24943d22010-06-08 16:52:24 +00001205
1206class CommandObjectProcessInterrupt : public CommandObject
1207{
1208public:
1209
1210
Greg Clayton238c0a12010-09-18 01:14:36 +00001211 CommandObjectProcessInterrupt (CommandInterpreter &interpreter) :
1212 CommandObject (interpreter,
1213 "process interrupt",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001214 "Interrupt the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +00001215 "process interrupt",
1216 eFlagProcessMustBeLaunched)
1217 {
1218 }
1219
1220 ~CommandObjectProcessInterrupt ()
1221 {
1222 }
1223
1224 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001225 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001226 CommandReturnObject &result)
1227 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001228 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001229 if (process == NULL)
1230 {
1231 result.AppendError ("no process to halt");
1232 result.SetStatus (eReturnStatusFailed);
1233 return false;
1234 }
1235
1236 if (command.GetArgumentCount() == 0)
1237 {
1238 Error error(process->Halt ());
1239 if (error.Success())
1240 {
1241 result.SetStatus (eReturnStatusSuccessFinishResult);
1242
1243 // Maybe we should add a "SuspendThreadPlans so we
1244 // can halt, and keep in place all the current thread plans.
1245 process->GetThreadList().DiscardThreadPlans();
1246 }
1247 else
1248 {
1249 result.AppendErrorWithFormat ("Failed to halt process: %s\n", error.AsCString());
1250 result.SetStatus (eReturnStatusFailed);
1251 }
1252 }
1253 else
1254 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001255 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner24943d22010-06-08 16:52:24 +00001256 m_cmd_name.c_str(),
1257 m_cmd_syntax.c_str());
1258 result.SetStatus (eReturnStatusFailed);
1259 }
1260 return result.Succeeded();
1261 }
1262};
1263
1264//-------------------------------------------------------------------------
1265// CommandObjectProcessKill
1266//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001267#pragma mark CommandObjectProcessKill
Chris Lattner24943d22010-06-08 16:52:24 +00001268
1269class CommandObjectProcessKill : public CommandObject
1270{
1271public:
1272
Greg Clayton238c0a12010-09-18 01:14:36 +00001273 CommandObjectProcessKill (CommandInterpreter &interpreter) :
1274 CommandObject (interpreter,
1275 "process kill",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001276 "Terminate the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +00001277 "process kill",
1278 eFlagProcessMustBeLaunched)
1279 {
1280 }
1281
1282 ~CommandObjectProcessKill ()
1283 {
1284 }
1285
1286 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001287 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001288 CommandReturnObject &result)
1289 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001290 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001291 if (process == NULL)
1292 {
1293 result.AppendError ("no process to kill");
1294 result.SetStatus (eReturnStatusFailed);
1295 return false;
1296 }
1297
1298 if (command.GetArgumentCount() == 0)
1299 {
1300 Error error (process->Destroy());
1301 if (error.Success())
1302 {
1303 result.SetStatus (eReturnStatusSuccessFinishResult);
1304 }
1305 else
1306 {
1307 result.AppendErrorWithFormat ("Failed to kill process: %s\n", error.AsCString());
1308 result.SetStatus (eReturnStatusFailed);
1309 }
1310 }
1311 else
1312 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001313 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner24943d22010-06-08 16:52:24 +00001314 m_cmd_name.c_str(),
1315 m_cmd_syntax.c_str());
1316 result.SetStatus (eReturnStatusFailed);
1317 }
1318 return result.Succeeded();
1319 }
1320};
1321
1322//-------------------------------------------------------------------------
Jim Ingham41313fc2010-06-18 01:23:09 +00001323// CommandObjectProcessStatus
1324//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001325#pragma mark CommandObjectProcessStatus
1326
Jim Ingham41313fc2010-06-18 01:23:09 +00001327class CommandObjectProcessStatus : public CommandObject
1328{
1329public:
Greg Clayton238c0a12010-09-18 01:14:36 +00001330 CommandObjectProcessStatus (CommandInterpreter &interpreter) :
1331 CommandObject (interpreter,
1332 "process status",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001333 "Show the current status and location of executing process.",
1334 "process status",
Jim Ingham41313fc2010-06-18 01:23:09 +00001335 0)
1336 {
1337 }
1338
1339 ~CommandObjectProcessStatus()
1340 {
1341 }
1342
1343
1344 bool
1345 Execute
1346 (
1347 Args& command,
Jim Ingham41313fc2010-06-18 01:23:09 +00001348 CommandReturnObject &result
1349 )
1350 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001351 Stream &strm = result.GetOutputStream();
Jim Ingham41313fc2010-06-18 01:23:09 +00001352 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Claytonb72d0f02011-04-12 05:54:46 +00001353 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
Greg Clayton567e7f32011-09-22 04:58:26 +00001354 Process *process = exe_ctx.GetProcessPtr();
1355 if (process)
Jim Ingham41313fc2010-06-18 01:23:09 +00001356 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001357 const bool only_threads_with_stop_reason = true;
1358 const uint32_t start_frame = 0;
1359 const uint32_t num_frames = 1;
1360 const uint32_t num_frames_with_source = 1;
Greg Clayton567e7f32011-09-22 04:58:26 +00001361 process->GetStatus(strm);
1362 process->GetThreadStatus (strm,
1363 only_threads_with_stop_reason,
1364 start_frame,
1365 num_frames,
1366 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001367
Jim Ingham41313fc2010-06-18 01:23:09 +00001368 }
1369 else
1370 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001371 result.AppendError ("No process.");
Jim Ingham41313fc2010-06-18 01:23:09 +00001372 result.SetStatus (eReturnStatusFailed);
1373 }
1374 return result.Succeeded();
1375 }
1376};
1377
1378//-------------------------------------------------------------------------
Caroline Tice23d6f272010-10-13 20:44:39 +00001379// CommandObjectProcessHandle
1380//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001381#pragma mark CommandObjectProcessHandle
Caroline Tice23d6f272010-10-13 20:44:39 +00001382
1383class CommandObjectProcessHandle : public CommandObject
1384{
1385public:
1386
1387 class CommandOptions : public Options
1388 {
1389 public:
1390
Greg Claytonf15996e2011-04-07 22:46:35 +00001391 CommandOptions (CommandInterpreter &interpreter) :
1392 Options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001393 {
Greg Clayton143fcc32011-04-13 00:18:08 +00001394 OptionParsingStarting ();
Caroline Tice23d6f272010-10-13 20:44:39 +00001395 }
1396
1397 ~CommandOptions ()
1398 {
1399 }
1400
1401 Error
Greg Clayton143fcc32011-04-13 00:18:08 +00001402 SetOptionValue (uint32_t option_idx, const char *option_arg)
Caroline Tice23d6f272010-10-13 20:44:39 +00001403 {
1404 Error error;
1405 char short_option = (char) m_getopt_table[option_idx].val;
1406
1407 switch (short_option)
1408 {
1409 case 's':
1410 stop = option_arg;
1411 break;
1412 case 'n':
1413 notify = option_arg;
1414 break;
1415 case 'p':
1416 pass = option_arg;
1417 break;
1418 default:
Greg Clayton9c236732011-10-26 00:56:27 +00001419 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Caroline Tice23d6f272010-10-13 20:44:39 +00001420 break;
1421 }
1422 return error;
1423 }
1424
1425 void
Greg Clayton143fcc32011-04-13 00:18:08 +00001426 OptionParsingStarting ()
Caroline Tice23d6f272010-10-13 20:44:39 +00001427 {
Caroline Tice23d6f272010-10-13 20:44:39 +00001428 stop.clear();
1429 notify.clear();
1430 pass.clear();
1431 }
1432
Greg Claytonb3448432011-03-24 21:19:54 +00001433 const OptionDefinition*
Caroline Tice23d6f272010-10-13 20:44:39 +00001434 GetDefinitions ()
1435 {
1436 return g_option_table;
1437 }
1438
1439 // Options table: Required for subclasses of Options.
1440
Greg Claytonb3448432011-03-24 21:19:54 +00001441 static OptionDefinition g_option_table[];
Caroline Tice23d6f272010-10-13 20:44:39 +00001442
1443 // Instance variables to hold the values for command options.
1444
1445 std::string stop;
1446 std::string notify;
1447 std::string pass;
1448 };
1449
1450
1451 CommandObjectProcessHandle (CommandInterpreter &interpreter) :
1452 CommandObject (interpreter,
1453 "process handle",
Caroline Ticee7471982010-10-14 21:31:13 +00001454 "Show or update what the process and debugger should do with various signals received from the OS.",
Greg Claytonf15996e2011-04-07 22:46:35 +00001455 NULL),
1456 m_options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001457 {
Caroline Ticee7471982010-10-14 21:31:13 +00001458 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 +00001459 CommandArgumentEntry arg;
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001460 CommandArgumentData signal_arg;
Caroline Tice23d6f272010-10-13 20:44:39 +00001461
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001462 signal_arg.arg_type = eArgTypeUnixSignal;
1463 signal_arg.arg_repetition = eArgRepeatStar;
Caroline Tice23d6f272010-10-13 20:44:39 +00001464
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001465 arg.push_back (signal_arg);
Caroline Tice23d6f272010-10-13 20:44:39 +00001466
1467 m_arguments.push_back (arg);
1468 }
1469
1470 ~CommandObjectProcessHandle ()
1471 {
1472 }
1473
1474 Options *
1475 GetOptions ()
1476 {
1477 return &m_options;
1478 }
1479
1480 bool
Caroline Ticee7471982010-10-14 21:31:13 +00001481 VerifyCommandOptionValue (const std::string &option, int &real_value)
Caroline Tice23d6f272010-10-13 20:44:39 +00001482 {
1483 bool okay = true;
1484
Caroline Ticee7471982010-10-14 21:31:13 +00001485 bool success = false;
1486 bool tmp_value = Args::StringToBoolean (option.c_str(), false, &success);
1487
1488 if (success && tmp_value)
1489 real_value = 1;
1490 else if (success && !tmp_value)
1491 real_value = 0;
Caroline Tice23d6f272010-10-13 20:44:39 +00001492 else
1493 {
1494 // If the value isn't 'true' or 'false', it had better be 0 or 1.
Caroline Ticee7471982010-10-14 21:31:13 +00001495 real_value = Args::StringToUInt32 (option.c_str(), 3);
1496 if (real_value != 0 && real_value != 1)
Caroline Tice23d6f272010-10-13 20:44:39 +00001497 okay = false;
1498 }
1499
1500 return okay;
1501 }
1502
Caroline Ticee7471982010-10-14 21:31:13 +00001503 void
1504 PrintSignalHeader (Stream &str)
1505 {
1506 str.Printf ("NAME PASS STOP NOTIFY\n");
1507 str.Printf ("========== ===== ===== ======\n");
1508 }
1509
1510 void
1511 PrintSignal (Stream &str, int32_t signo, const char *sig_name, UnixSignals &signals)
1512 {
1513 bool stop;
1514 bool suppress;
1515 bool notify;
1516
1517 str.Printf ("%-10s ", sig_name);
1518 if (signals.GetSignalInfo (signo, suppress, stop, notify))
1519 {
1520 bool pass = !suppress;
1521 str.Printf ("%s %s %s",
1522 (pass ? "true " : "false"),
1523 (stop ? "true " : "false"),
1524 (notify ? "true " : "false"));
1525 }
1526 str.Printf ("\n");
1527 }
1528
1529 void
1530 PrintSignalInformation (Stream &str, Args &signal_args, int num_valid_signals, UnixSignals &signals)
1531 {
1532 PrintSignalHeader (str);
1533
1534 if (num_valid_signals > 0)
1535 {
1536 size_t num_args = signal_args.GetArgumentCount();
1537 for (size_t i = 0; i < num_args; ++i)
1538 {
1539 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1540 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
1541 PrintSignal (str, signo, signal_args.GetArgumentAtIndex (i), signals);
1542 }
1543 }
1544 else // Print info for ALL signals
1545 {
1546 int32_t signo = signals.GetFirstSignalNumber();
1547 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1548 {
1549 PrintSignal (str, signo, signals.GetSignalAsCString (signo), signals);
1550 signo = signals.GetNextSignalNumber (signo);
1551 }
1552 }
1553 }
1554
Caroline Tice23d6f272010-10-13 20:44:39 +00001555 bool
1556 Execute (Args &signal_args, CommandReturnObject &result)
1557 {
1558 TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
1559
1560 if (!target_sp)
1561 {
1562 result.AppendError ("No current target;"
1563 " cannot handle signals until you have a valid target and process.\n");
1564 result.SetStatus (eReturnStatusFailed);
1565 return false;
1566 }
1567
1568 ProcessSP process_sp = target_sp->GetProcessSP();
1569
1570 if (!process_sp)
1571 {
1572 result.AppendError ("No current process; cannot handle signals until you have a valid process.\n");
1573 result.SetStatus (eReturnStatusFailed);
1574 return false;
1575 }
1576
Caroline Tice23d6f272010-10-13 20:44:39 +00001577 int stop_action = -1; // -1 means leave the current setting alone
Caroline Ticee7471982010-10-14 21:31:13 +00001578 int pass_action = -1; // -1 means leave the current setting alone
Caroline Tice23d6f272010-10-13 20:44:39 +00001579 int notify_action = -1; // -1 means leave the current setting alone
1580
1581 if (! m_options.stop.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001582 && ! VerifyCommandOptionValue (m_options.stop, stop_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001583 {
1584 result.AppendError ("Invalid argument for command option --stop; must be true or false.\n");
1585 result.SetStatus (eReturnStatusFailed);
1586 return false;
1587 }
1588
1589 if (! m_options.notify.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001590 && ! VerifyCommandOptionValue (m_options.notify, notify_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001591 {
1592 result.AppendError ("Invalid argument for command option --notify; must be true or false.\n");
1593 result.SetStatus (eReturnStatusFailed);
1594 return false;
1595 }
1596
1597 if (! m_options.pass.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001598 && ! VerifyCommandOptionValue (m_options.pass, pass_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001599 {
1600 result.AppendError ("Invalid argument for command option --pass; must be true or false.\n");
1601 result.SetStatus (eReturnStatusFailed);
1602 return false;
1603 }
1604
1605 size_t num_args = signal_args.GetArgumentCount();
1606 UnixSignals &signals = process_sp->GetUnixSignals();
1607 int num_signals_set = 0;
1608
Caroline Ticee7471982010-10-14 21:31:13 +00001609 if (num_args > 0)
Caroline Tice23d6f272010-10-13 20:44:39 +00001610 {
Caroline Ticee7471982010-10-14 21:31:13 +00001611 for (size_t i = 0; i < num_args; ++i)
Caroline Tice23d6f272010-10-13 20:44:39 +00001612 {
Caroline Ticee7471982010-10-14 21:31:13 +00001613 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1614 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
Caroline Tice23d6f272010-10-13 20:44:39 +00001615 {
Caroline Ticee7471982010-10-14 21:31:13 +00001616 // Casting the actions as bools here should be okay, because VerifyCommandOptionValue guarantees
1617 // the value is either 0 or 1.
1618 if (stop_action != -1)
1619 signals.SetShouldStop (signo, (bool) stop_action);
1620 if (pass_action != -1)
1621 {
1622 bool suppress = ! ((bool) pass_action);
1623 signals.SetShouldSuppress (signo, suppress);
1624 }
1625 if (notify_action != -1)
1626 signals.SetShouldNotify (signo, (bool) notify_action);
1627 ++num_signals_set;
Caroline Tice23d6f272010-10-13 20:44:39 +00001628 }
Caroline Ticee7471982010-10-14 21:31:13 +00001629 else
1630 {
1631 result.AppendErrorWithFormat ("Invalid signal name '%s'\n", signal_args.GetArgumentAtIndex (i));
1632 }
Caroline Tice23d6f272010-10-13 20:44:39 +00001633 }
1634 }
Caroline Ticee7471982010-10-14 21:31:13 +00001635 else
1636 {
1637 // No signal specified, if any command options were specified, update ALL signals.
1638 if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1))
1639 {
1640 if (m_interpreter.Confirm ("Do you really want to update all the signals?", false))
1641 {
1642 int32_t signo = signals.GetFirstSignalNumber();
1643 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1644 {
1645 if (notify_action != -1)
1646 signals.SetShouldNotify (signo, (bool) notify_action);
1647 if (stop_action != -1)
1648 signals.SetShouldStop (signo, (bool) stop_action);
1649 if (pass_action != -1)
1650 {
1651 bool suppress = ! ((bool) pass_action);
1652 signals.SetShouldSuppress (signo, suppress);
1653 }
1654 signo = signals.GetNextSignalNumber (signo);
1655 }
1656 }
1657 }
1658 }
1659
1660 PrintSignalInformation (result.GetOutputStream(), signal_args, num_signals_set, signals);
Caroline Tice23d6f272010-10-13 20:44:39 +00001661
1662 if (num_signals_set > 0)
1663 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1664 else
1665 result.SetStatus (eReturnStatusFailed);
1666
1667 return result.Succeeded();
1668 }
1669
1670protected:
1671
1672 CommandOptions m_options;
1673};
1674
Greg Claytonb3448432011-03-24 21:19:54 +00001675OptionDefinition
Caroline Tice23d6f272010-10-13 20:44:39 +00001676CommandObjectProcessHandle::CommandOptions::g_option_table[] =
1677{
1678{ 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." },
1679{ 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." },
1680{ LLDB_OPT_SET_1, false, "pass", 'p', required_argument, NULL, 0, eArgTypeBoolean, "Whether or not the signal should be passed to the process." },
1681{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1682};
1683
1684//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001685// CommandObjectMultiwordProcess
1686//-------------------------------------------------------------------------
1687
Greg Clayton63094e02010-06-23 01:19:29 +00001688CommandObjectMultiwordProcess::CommandObjectMultiwordProcess (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00001689 CommandObjectMultiword (interpreter,
1690 "process",
1691 "A set of commands for operating on a process.",
1692 "process <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00001693{
Greg Claytona9eb8272011-07-02 21:07:54 +00001694 LoadSubCommand ("attach", CommandObjectSP (new CommandObjectProcessAttach (interpreter)));
1695 LoadSubCommand ("launch", CommandObjectSP (new CommandObjectProcessLaunch (interpreter)));
1696 LoadSubCommand ("continue", CommandObjectSP (new CommandObjectProcessContinue (interpreter)));
1697 LoadSubCommand ("connect", CommandObjectSP (new CommandObjectProcessConnect (interpreter)));
1698 LoadSubCommand ("detach", CommandObjectSP (new CommandObjectProcessDetach (interpreter)));
1699 LoadSubCommand ("load", CommandObjectSP (new CommandObjectProcessLoad (interpreter)));
1700 LoadSubCommand ("unload", CommandObjectSP (new CommandObjectProcessUnload (interpreter)));
1701 LoadSubCommand ("signal", CommandObjectSP (new CommandObjectProcessSignal (interpreter)));
1702 LoadSubCommand ("handle", CommandObjectSP (new CommandObjectProcessHandle (interpreter)));
1703 LoadSubCommand ("status", CommandObjectSP (new CommandObjectProcessStatus (interpreter)));
Greg Clayton238c0a12010-09-18 01:14:36 +00001704 LoadSubCommand ("interrupt", CommandObjectSP (new CommandObjectProcessInterrupt (interpreter)));
Greg Claytona9eb8272011-07-02 21:07:54 +00001705 LoadSubCommand ("kill", CommandObjectSP (new CommandObjectProcessKill (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00001706}
1707
1708CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess ()
1709{
1710}
1711