blob: 249c8dac92e223250acc3c11541a53ecda11512a [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 Ingham5a15e692012-02-16 06:50:00 +000033#pragma mark CommandObjectProcessLaunch
Chris Lattner24943d22010-06-08 16:52:24 +000034class CommandObjectProcessLaunch : public CommandObject
35{
36public:
37
Greg Clayton238c0a12010-09-18 01:14:36 +000038 CommandObjectProcessLaunch (CommandInterpreter &interpreter) :
39 CommandObject (interpreter,
40 "process launch",
Caroline Ticeabb507a2010-09-08 21:06:11 +000041 "Launch the executable in the debugger.",
Greg Claytonf15996e2011-04-07 22:46:35 +000042 NULL),
43 m_options (interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +000044 {
Caroline Tice43b014a2010-10-04 22:28:36 +000045 CommandArgumentEntry arg;
46 CommandArgumentData run_args_arg;
47
48 // Define the first (and only) variant of this arg.
49 run_args_arg.arg_type = eArgTypeRunArgs;
50 run_args_arg.arg_repetition = eArgRepeatOptional;
51
52 // There is only one variant this argument could be; put it into the argument entry.
53 arg.push_back (run_args_arg);
54
55 // Push the data for the first argument into the m_arguments vector.
56 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +000057 }
58
59
60 ~CommandObjectProcessLaunch ()
61 {
62 }
63
64 Options *
65 GetOptions ()
66 {
67 return &m_options;
68 }
69
70 bool
Greg Claytond8c62532010-10-07 04:19:01 +000071 Execute (Args& launch_args, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +000072 {
Greg Claytonabb33022011-11-08 02:43:13 +000073 Debugger &debugger = m_interpreter.GetDebugger();
74 Target *target = debugger.GetSelectedTarget().get();
75 Error error;
Chris Lattner24943d22010-06-08 16:52:24 +000076
77 if (target == NULL)
78 {
Greg Claytone1f50b92011-05-03 22:09:39 +000079 result.AppendError ("invalid target, create a debug target using the 'target create' command");
Chris Lattner24943d22010-06-08 16:52:24 +000080 result.SetStatus (eReturnStatusFailed);
81 return false;
82 }
Chris Lattner24943d22010-06-08 16:52:24 +000083 // If our listener is NULL, users aren't allows to launch
Chris Lattner24943d22010-06-08 16:52:24 +000084 char filename[PATH_MAX];
Greg Clayton5beb99d2011-08-11 02:48:45 +000085 const Module *exe_module = target->GetExecutableModulePointer();
Greg Claytona2f74232011-02-24 22:24:29 +000086
87 if (exe_module == NULL)
88 {
Greg Claytone1f50b92011-05-03 22:09:39 +000089 result.AppendError ("no file in target, create a debug target using the 'target create' command");
Greg Claytona2f74232011-02-24 22:24:29 +000090 result.SetStatus (eReturnStatusFailed);
91 return false;
92 }
93
Greg Clayton36bc5ea2011-11-03 21:22:33 +000094 exe_module->GetFileSpec().GetPath (filename, sizeof(filename));
Chris Lattner24943d22010-06-08 16:52:24 +000095
Greg Clayton36bc5ea2011-11-03 21:22:33 +000096 const bool add_exe_file_as_first_arg = true;
Greg Clayton1d1f39e2011-11-29 04:03:30 +000097 m_options.launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), add_exe_file_as_first_arg);
Greg Clayton36bc5ea2011-11-03 21:22:33 +000098
Greg Claytona2f74232011-02-24 22:24:29 +000099 StateType state = eStateInvalid;
Greg Clayton567e7f32011-09-22 04:58:26 +0000100 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytona2f74232011-02-24 22:24:29 +0000101 if (process)
102 {
103 state = process->GetState();
104
105 if (process->IsAlive() && state != eStateConnected)
106 {
107 char message[1024];
108 if (process->GetState() == eStateAttaching)
109 ::strncpy (message, "There is a pending attach, abort it and launch a new process?", sizeof(message));
110 else
111 ::strncpy (message, "There is a running process, kill it and restart?", sizeof(message));
112
113 if (!m_interpreter.Confirm (message, true))
Jim Ingham22dc9722010-12-09 18:58:16 +0000114 {
Greg Claytona2f74232011-02-24 22:24:29 +0000115 result.SetStatus (eReturnStatusFailed);
116 return false;
Jim Ingham22dc9722010-12-09 18:58:16 +0000117 }
118 else
119 {
Greg Claytonabb33022011-11-08 02:43:13 +0000120 Error destroy_error (process->Destroy());
121 if (destroy_error.Success())
Greg Claytona2f74232011-02-24 22:24:29 +0000122 {
123 result.SetStatus (eReturnStatusSuccessFinishResult);
124 }
125 else
126 {
Greg Claytonabb33022011-11-08 02:43:13 +0000127 result.AppendErrorWithFormat ("Failed to kill process: %s\n", destroy_error.AsCString());
Greg Claytona2f74232011-02-24 22:24:29 +0000128 result.SetStatus (eReturnStatusFailed);
129 }
Jim Ingham22dc9722010-12-09 18:58:16 +0000130 }
131 }
Chris Lattner24943d22010-06-08 16:52:24 +0000132 }
Jim Ingham22dc9722010-12-09 18:58:16 +0000133
Greg Clayton527154d2011-11-15 03:53:30 +0000134 if (launch_args.GetArgumentCount() == 0)
135 {
136 const Args &process_args = target->GetRunArguments();
137 if (process_args.GetArgumentCount() > 0)
138 m_options.launch_info.GetArguments().AppendArguments (process_args);
139 }
140 else
Greg Claytonabb33022011-11-08 02:43:13 +0000141 {
Greg Clayton3e6f2cc2011-11-21 21:51:18 +0000142 // Save the arguments for subsequent runs in the current target.
143 target->SetRunArguments (launch_args);
144
Greg Claytonabb33022011-11-08 02:43:13 +0000145 m_options.launch_info.GetArguments().AppendArguments (launch_args);
146 }
Greg Claytonabb33022011-11-08 02:43:13 +0000147
Greg Clayton527154d2011-11-15 03:53:30 +0000148 if (target->GetDisableASLR())
149 m_options.launch_info.GetFlags().Set (eLaunchFlagDisableASLR);
150
151 if (target->GetDisableSTDIO())
152 m_options.launch_info.GetFlags().Set (eLaunchFlagDisableSTDIO);
153
154 m_options.launch_info.GetFlags().Set (eLaunchFlagDebug);
155
156 Args environment;
157 target->GetEnvironmentAsArgs (environment);
158 if (environment.GetArgumentCount() > 0)
159 m_options.launch_info.GetEnvironmentEntries ().AppendArguments (environment);
160
Greg Clayton464c6162011-11-17 22:14:31 +0000161 // Finalize the file actions, and if none were given, default to opening
162 // up a pseudo terminal
163 const bool default_to_use_pty = true;
164 m_options.launch_info.FinalizeFileActions (target, default_to_use_pty);
Greg Clayton527154d2011-11-15 03:53:30 +0000165
Greg Claytonabb33022011-11-08 02:43:13 +0000166 if (state == eStateConnected)
167 {
168 if (m_options.launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
169 {
170 result.AppendWarning("can't launch in tty when launching through a remote connection");
171 m_options.launch_info.GetFlags().Clear (eLaunchFlagLaunchInTTY);
172 }
173 }
174 else
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000175 {
Greg Clayton527154d2011-11-15 03:53:30 +0000176 if (!m_options.launch_info.GetArchitecture().IsValid())
Greg Clayton2d9adb72011-11-12 02:10:56 +0000177 m_options.launch_info.GetArchitecture() = target->GetArchitecture();
178
Greg Clayton75d8c252011-11-28 01:45:00 +0000179 PlatformSP platform_sp (target->GetPlatform());
180
181 if (platform_sp && platform_sp->CanDebugProcess ())
182 {
183 process = target->GetPlatform()->DebugProcess (m_options.launch_info,
184 debugger,
185 target,
186 debugger.GetListener(),
187 error).get();
188 }
189 else
190 {
191 const char *plugin_name = m_options.launch_info.GetProcessPluginName();
Greg Clayton46c9a352012-02-09 06:16:32 +0000192 process = target->CreateProcess (debugger.GetListener(), plugin_name, NULL).get();
Greg Clayton75d8c252011-11-28 01:45:00 +0000193 if (process)
194 error = process->Launch (m_options.launch_info);
195 }
Greg Claytonabb33022011-11-08 02:43:13 +0000196
Greg Claytona2f74232011-02-24 22:24:29 +0000197 if (process == NULL)
198 {
Greg Clayton527154d2011-11-15 03:53:30 +0000199 result.SetError (error, "failed to launch or debug process");
Greg Claytona2f74232011-02-24 22:24:29 +0000200 return false;
201 }
Chris Lattner24943d22010-06-08 16:52:24 +0000202 }
Greg Claytonabb33022011-11-08 02:43:13 +0000203
Greg Clayton238c0a12010-09-18 01:14:36 +0000204 if (error.Success())
205 {
Greg Clayton940b1032011-02-23 00:35:02 +0000206 const char *archname = exe_module->GetArchitecture().GetArchitectureName();
Greg Claytonc1d37752010-10-18 01:45:30 +0000207
Greg Clayton444e35b2011-10-19 18:09:39 +0000208 result.AppendMessageWithFormat ("Process %llu launched: '%s' (%s)\n", process->GetID(), filename, archname);
Greg Claytond8c62532010-10-07 04:19:01 +0000209 result.SetDidChangeProcessState (true);
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000210 if (m_options.launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
Greg Clayton238c0a12010-09-18 01:14:36 +0000211 {
Greg Claytond8c62532010-10-07 04:19:01 +0000212 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
Greg Clayton238c0a12010-09-18 01:14:36 +0000213 StateType state = process->WaitForProcessToStop (NULL);
214
215 if (state == eStateStopped)
216 {
Greg Claytond8c62532010-10-07 04:19:01 +0000217 error = process->Resume();
218 if (error.Success())
219 {
220 bool synchronous_execution = m_interpreter.GetSynchronous ();
221 if (synchronous_execution)
222 {
223 state = process->WaitForProcessToStop (NULL);
Greg Clayton20206082011-11-17 01:23:07 +0000224 const bool must_be_alive = true;
225 if (!StateIsStoppedState(state, must_be_alive))
Greg Clayton395fc332011-02-15 21:59:32 +0000226 {
Greg Clayton527154d2011-11-15 03:53:30 +0000227 result.AppendErrorWithFormat ("process isn't stopped: %s", StateAsCString(state));
Greg Clayton395fc332011-02-15 21:59:32 +0000228 }
Greg Claytond8c62532010-10-07 04:19:01 +0000229 result.SetDidChangeProcessState (true);
230 result.SetStatus (eReturnStatusSuccessFinishResult);
231 }
232 else
233 {
234 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
235 }
236 }
Greg Clayton395fc332011-02-15 21:59:32 +0000237 else
238 {
Greg Clayton527154d2011-11-15 03:53:30 +0000239 result.AppendErrorWithFormat ("process resume at entry point failed: %s", error.AsCString());
Greg Clayton395fc332011-02-15 21:59:32 +0000240 result.SetStatus (eReturnStatusFailed);
241 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000242 }
Greg Clayton395fc332011-02-15 21:59:32 +0000243 else
244 {
Greg Clayton527154d2011-11-15 03:53:30 +0000245 result.AppendErrorWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
Greg Clayton395fc332011-02-15 21:59:32 +0000246 result.SetStatus (eReturnStatusFailed);
247 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000248 }
249 }
Greg Clayton395fc332011-02-15 21:59:32 +0000250 else
251 {
Greg Claytona9eb8272011-07-02 21:07:54 +0000252 result.AppendErrorWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton395fc332011-02-15 21:59:32 +0000253 result.SetStatus (eReturnStatusFailed);
254 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000255
Chris Lattner24943d22010-06-08 16:52:24 +0000256 return result.Succeeded();
257 }
258
Jim Ingham767af882010-07-07 03:36:20 +0000259 virtual const char *GetRepeatCommand (Args &current_command_args, uint32_t index)
260 {
261 // No repeat for "process launch"...
262 return "";
263 }
264
Chris Lattner24943d22010-06-08 16:52:24 +0000265protected:
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000266 ProcessLaunchCommandOptions m_options;
Chris Lattner24943d22010-06-08 16:52:24 +0000267};
268
269
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000270//#define SET1 LLDB_OPT_SET_1
271//#define SET2 LLDB_OPT_SET_2
272//#define SET3 LLDB_OPT_SET_3
273//
274//OptionDefinition
275//CommandObjectProcessLaunch::CommandOptions::g_option_table[] =
276//{
277//{ 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."},
278//{ SET1 , false, "stdin", 'i', required_argument, NULL, 0, eArgTypePath, "Redirect stdin for the process to <path>."},
279//{ SET1 , false, "stdout", 'o', required_argument, NULL, 0, eArgTypePath, "Redirect stdout for the process to <path>."},
280//{ SET1 , false, "stderr", 'e', required_argument, NULL, 0, eArgTypePath, "Redirect stderr for the process to <path>."},
281//{ SET1 | SET2 | SET3, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
282//{ 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."},
283//{ SET3, false, "no-stdio", 'n', no_argument, NULL, 0, eArgTypeNone, "Do not set up for terminal I/O to go to running process."},
284//{ SET1 | SET2 | SET3, false, "working-dir", 'w', required_argument, NULL, 0, eArgTypePath, "Set the current working directory to <path> when running the inferior."},
285//{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
286//};
287//
288//#undef SET1
289//#undef SET2
290//#undef SET3
Chris Lattner24943d22010-06-08 16:52:24 +0000291
292//-------------------------------------------------------------------------
293// CommandObjectProcessAttach
294//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000295#pragma mark CommandObjectProcessAttach
Chris Lattner24943d22010-06-08 16:52:24 +0000296class CommandObjectProcessAttach : public CommandObject
297{
298public:
299
Chris Lattner24943d22010-06-08 16:52:24 +0000300 class CommandOptions : public Options
301 {
302 public:
303
Greg Claytonf15996e2011-04-07 22:46:35 +0000304 CommandOptions (CommandInterpreter &interpreter) :
305 Options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000306 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000307 // Keep default values of all options in one place: OptionParsingStarting ()
308 OptionParsingStarting ();
Chris Lattner24943d22010-06-08 16:52:24 +0000309 }
310
311 ~CommandOptions ()
312 {
313 }
314
315 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000316 SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +0000317 {
318 Error error;
319 char short_option = (char) m_getopt_table[option_idx].val;
320 bool success = false;
321 switch (short_option)
322 {
Johnny Chen7c099972012-05-24 00:43:00 +0000323 case 'c':
324 attach_info.SetContinueOnceAttached(true);
325 break;
326
Chris Lattner24943d22010-06-08 16:52:24 +0000327 case 'p':
Chris Lattner24943d22010-06-08 16:52:24 +0000328 {
Greg Clayton527154d2011-11-15 03:53:30 +0000329 lldb::pid_t pid = Args::StringToUInt32 (option_arg, LLDB_INVALID_PROCESS_ID, 0, &success);
330 if (!success || pid == LLDB_INVALID_PROCESS_ID)
331 {
332 error.SetErrorStringWithFormat("invalid process ID '%s'", option_arg);
333 }
334 else
335 {
336 attach_info.SetProcessID (pid);
337 }
Chris Lattner24943d22010-06-08 16:52:24 +0000338 }
339 break;
340
341 case 'P':
Greg Clayton527154d2011-11-15 03:53:30 +0000342 attach_info.SetProcessPluginName (option_arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000343 break;
344
345 case 'n':
Greg Clayton527154d2011-11-15 03:53:30 +0000346 attach_info.GetExecutableFile().SetFile(option_arg, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000347 break;
348
349 case 'w':
Greg Clayton527154d2011-11-15 03:53:30 +0000350 attach_info.SetWaitForLaunch(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000351 break;
352
353 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000354 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner24943d22010-06-08 16:52:24 +0000355 break;
356 }
357 return error;
358 }
359
360 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000361 OptionParsingStarting ()
Chris Lattner24943d22010-06-08 16:52:24 +0000362 {
Greg Clayton527154d2011-11-15 03:53:30 +0000363 attach_info.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000364 }
365
Greg Claytonb3448432011-03-24 21:19:54 +0000366 const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +0000367 GetDefinitions ()
368 {
369 return g_option_table;
370 }
371
Jim Ingham7508e732010-08-09 23:31:02 +0000372 virtual bool
Greg Claytonf15996e2011-04-07 22:46:35 +0000373 HandleOptionArgumentCompletion (Args &input,
Jim Ingham7508e732010-08-09 23:31:02 +0000374 int cursor_index,
375 int char_pos,
376 OptionElementVector &opt_element_vector,
377 int opt_element_index,
378 int match_start_point,
379 int max_return_elements,
380 bool &word_complete,
381 StringList &matches)
382 {
383 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
384 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
385
386 // We are only completing the name option for now...
387
Greg Claytonb3448432011-03-24 21:19:54 +0000388 const OptionDefinition *opt_defs = GetDefinitions();
Jim Ingham7508e732010-08-09 23:31:02 +0000389 if (opt_defs[opt_defs_index].short_option == 'n')
390 {
391 // Are we in the name?
392
393 // Look to see if there is a -P argument provided, and if so use that plugin, otherwise
394 // use the default plugin.
Jim Ingham7508e732010-08-09 23:31:02 +0000395
396 const char *partial_name = NULL;
397 partial_name = input.GetArgumentAtIndex(opt_arg_pos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000398
Greg Claytonb72d0f02011-04-12 05:54:46 +0000399 PlatformSP platform_sp (m_interpreter.GetPlatform (true));
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000400 if (platform_sp)
Jim Ingham7508e732010-08-09 23:31:02 +0000401 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000402 ProcessInstanceInfoList process_infos;
403 ProcessInstanceInfoMatch match_info;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000404 if (partial_name)
405 {
Greg Clayton527154d2011-11-15 03:53:30 +0000406 match_info.GetProcessInfo().GetExecutableFile().SetFile(partial_name, false);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000407 match_info.SetNameMatchType(eNameMatchStartsWith);
408 }
409 platform_sp->FindProcesses (match_info, process_infos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000410 const uint32_t num_matches = process_infos.GetSize();
411 if (num_matches > 0)
412 {
413 for (uint32_t i=0; i<num_matches; ++i)
414 {
415 matches.AppendString (process_infos.GetProcessNameAtIndex(i),
416 process_infos.GetProcessNameLengthAtIndex(i));
417 }
418 }
Jim Ingham7508e732010-08-09 23:31:02 +0000419 }
420 }
421
422 return false;
423 }
424
Chris Lattner24943d22010-06-08 16:52:24 +0000425 // Options table: Required for subclasses of Options.
426
Greg Claytonb3448432011-03-24 21:19:54 +0000427 static OptionDefinition g_option_table[];
Chris Lattner24943d22010-06-08 16:52:24 +0000428
429 // Instance variables to hold the values for command options.
430
Greg Clayton527154d2011-11-15 03:53:30 +0000431 ProcessAttachInfo attach_info;
Chris Lattner24943d22010-06-08 16:52:24 +0000432 };
433
Greg Clayton238c0a12010-09-18 01:14:36 +0000434 CommandObjectProcessAttach (CommandInterpreter &interpreter) :
435 CommandObject (interpreter,
436 "process attach",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000437 "Attach to a process.",
Greg Claytonf15996e2011-04-07 22:46:35 +0000438 "process attach <cmd-options>"),
439 m_options (interpreter)
Jim Ingham7508e732010-08-09 23:31:02 +0000440 {
Jim Ingham7508e732010-08-09 23:31:02 +0000441 }
442
443 ~CommandObjectProcessAttach ()
444 {
445 }
446
447 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000448 Execute (Args& command,
Jim Ingham7508e732010-08-09 23:31:02 +0000449 CommandReturnObject &result)
450 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000451 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Jim Inghamee940e22011-09-15 01:08:57 +0000452 // N.B. The attach should be synchronous. It doesn't help much to get the prompt back between initiating the attach
453 // and the target actually stopping. So even if the interpreter is set to be asynchronous, we wait for the stop
454 // ourselves here.
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000455
Greg Clayton567e7f32011-09-22 04:58:26 +0000456 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytona2f74232011-02-24 22:24:29 +0000457 StateType state = eStateInvalid;
Jim Ingham7508e732010-08-09 23:31:02 +0000458 if (process)
459 {
Greg Claytona2f74232011-02-24 22:24:29 +0000460 state = process->GetState();
461 if (process->IsAlive() && state != eStateConnected)
Jim Ingham7508e732010-08-09 23:31:02 +0000462 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000463 result.AppendErrorWithFormat ("Process %llu is currently being debugged, kill the process before attaching.\n",
Jim Ingham7508e732010-08-09 23:31:02 +0000464 process->GetID());
465 result.SetStatus (eReturnStatusFailed);
466 return false;
467 }
468 }
469
470 if (target == NULL)
471 {
472 // If there isn't a current target create one.
473 TargetSP new_target_sp;
474 FileSpec emptyFileSpec;
Jim Ingham7508e732010-08-09 23:31:02 +0000475 Error error;
476
Greg Clayton238c0a12010-09-18 01:14:36 +0000477 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
478 emptyFileSpec,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000479 NULL,
Greg Clayton238c0a12010-09-18 01:14:36 +0000480 false,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000481 NULL, // No platform options
Greg Clayton238c0a12010-09-18 01:14:36 +0000482 new_target_sp);
Jim Ingham7508e732010-08-09 23:31:02 +0000483 target = new_target_sp.get();
484 if (target == NULL || error.Fail())
485 {
Greg Claytone71e2582011-02-04 01:58:07 +0000486 result.AppendError(error.AsCString("Error creating target"));
Jim Ingham7508e732010-08-09 23:31:02 +0000487 return false;
488 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000489 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target);
Jim Ingham7508e732010-08-09 23:31:02 +0000490 }
491
492 // Record the old executable module, we want to issue a warning if the process of attaching changed the
493 // current executable (like somebody said "file foo" then attached to a PID whose executable was bar.)
494
495 ModuleSP old_exec_module_sp = target->GetExecutableModule();
496 ArchSpec old_arch_spec = target->GetArchitecture();
497
498 if (command.GetArgumentCount())
499 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000500 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 +0000501 result.SetStatus (eReturnStatusFailed);
502 }
503 else
504 {
Greg Claytona2f74232011-02-24 22:24:29 +0000505 if (state != eStateConnected)
506 {
Greg Clayton527154d2011-11-15 03:53:30 +0000507 const char *plugin_name = m_options.attach_info.GetProcessPluginName();
Greg Clayton46c9a352012-02-09 06:16:32 +0000508 process = target->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name, NULL).get();
Greg Claytona2f74232011-02-24 22:24:29 +0000509 }
Jim Ingham7508e732010-08-09 23:31:02 +0000510
511 if (process)
512 {
513 Error error;
Greg Clayton527154d2011-11-15 03:53:30 +0000514 // If no process info was specified, then use the target executable
515 // name as the process to attach to by default
516 if (!m_options.attach_info.ProcessInfoSpecified ())
Jim Ingham4805a1c2010-09-15 01:34:14 +0000517 {
518 if (old_exec_module_sp)
Greg Clayton1d1f39e2011-11-29 04:03:30 +0000519 m_options.attach_info.GetExecutableFile().GetFilename() = old_exec_module_sp->GetPlatformFileSpec().GetFilename();
Jim Ingham4805a1c2010-09-15 01:34:14 +0000520
Greg Clayton527154d2011-11-15 03:53:30 +0000521 if (!m_options.attach_info.ProcessInfoSpecified ())
522 {
523 error.SetErrorString ("no process specified, create a target with a file, or specify the --pid or --name command option");
524 }
525 }
526
527 if (error.Success())
528 {
529 error = process->Attach (m_options.attach_info);
530
Jim Ingham4805a1c2010-09-15 01:34:14 +0000531 if (error.Success())
532 {
533 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
534 }
Jim Ingham7508e732010-08-09 23:31:02 +0000535 else
536 {
Greg Clayton527154d2011-11-15 03:53:30 +0000537 result.AppendErrorWithFormat ("attach failed: %s\n", error.AsCString());
Jim Ingham4805a1c2010-09-15 01:34:14 +0000538 result.SetStatus (eReturnStatusFailed);
539 return false;
Jim Ingham7508e732010-08-09 23:31:02 +0000540 }
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000541 // If we're synchronous, wait for the stopped event and report that.
542 // Otherwise just return.
543 // FIXME: in the async case it will now be possible to get to the command
544 // interpreter with a state eStateAttaching. Make sure we handle that correctly.
Jim Inghamee940e22011-09-15 01:08:57 +0000545 StateType state = process->WaitForProcessToStop (NULL);
Greg Clayton527154d2011-11-15 03:53:30 +0000546
Jim Inghamee940e22011-09-15 01:08:57 +0000547 result.SetDidChangeProcessState (true);
Johnny Chen9986a3b2012-05-18 00:51:36 +0000548
549 if (state == eStateStopped)
550 {
551 result.AppendMessageWithFormat ("Process %llu %s\n", process->GetID(), StateAsCString (state));
552 result.SetStatus (eReturnStatusSuccessFinishNoResult);
553 }
554 else
555 {
556 result.AppendError ("attach failed: process did not stop (no such process or permission problem?)");
557 result.SetStatus (eReturnStatusFailed);
558 return false;
559 }
Jim Ingham7508e732010-08-09 23:31:02 +0000560 }
Jim Ingham7508e732010-08-09 23:31:02 +0000561 }
562 }
563
564 if (result.Succeeded())
565 {
566 // Okay, we're done. Last step is to warn if the executable module has changed:
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000567 char new_path[PATH_MAX];
Greg Clayton5beb99d2011-08-11 02:48:45 +0000568 ModuleSP new_exec_module_sp (target->GetExecutableModule());
Jim Ingham7508e732010-08-09 23:31:02 +0000569 if (!old_exec_module_sp)
570 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000571 // We might not have a module if we attached to a raw pid...
Greg Clayton5beb99d2011-08-11 02:48:45 +0000572 if (new_exec_module_sp)
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000573 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000574 new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000575 result.AppendMessageWithFormat("Executable module set to \"%s\".\n", new_path);
576 }
Jim Ingham7508e732010-08-09 23:31:02 +0000577 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000578 else if (old_exec_module_sp->GetFileSpec() != new_exec_module_sp->GetFileSpec())
Jim Ingham7508e732010-08-09 23:31:02 +0000579 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000580 char old_path[PATH_MAX];
Jim Ingham7508e732010-08-09 23:31:02 +0000581
Greg Clayton5beb99d2011-08-11 02:48:45 +0000582 old_exec_module_sp->GetFileSpec().GetPath (old_path, PATH_MAX);
583 new_exec_module_sp->GetFileSpec().GetPath (new_path, PATH_MAX);
Jim Ingham7508e732010-08-09 23:31:02 +0000584
585 result.AppendWarningWithFormat("Executable module changed from \"%s\" to \"%s\".\n",
586 old_path, new_path);
587 }
588
589 if (!old_arch_spec.IsValid())
590 {
Greg Clayton940b1032011-02-23 00:35:02 +0000591 result.AppendMessageWithFormat ("Architecture set to: %s.\n", target->GetArchitecture().GetArchitectureName());
Jim Ingham7508e732010-08-09 23:31:02 +0000592 }
593 else if (old_arch_spec != target->GetArchitecture())
594 {
595 result.AppendWarningWithFormat("Architecture changed from %s to %s.\n",
Greg Clayton940b1032011-02-23 00:35:02 +0000596 old_arch_spec.GetArchitectureName(), target->GetArchitecture().GetArchitectureName());
Jim Ingham7508e732010-08-09 23:31:02 +0000597 }
Johnny Chen7c099972012-05-24 00:43:00 +0000598
599 // This supports the use-case scenario of immediately continuing the process once attached.
600 if (m_options.attach_info.GetContinueOnceAttached())
Sean Callanan4336d932012-05-31 01:30:08 +0000601 m_interpreter.HandleCommand("process continue", eLazyBoolNo, result);
Jim Ingham7508e732010-08-09 23:31:02 +0000602 }
603 return result.Succeeded();
604 }
605
606 Options *
607 GetOptions ()
608 {
609 return &m_options;
610 }
611
Chris Lattner24943d22010-06-08 16:52:24 +0000612protected:
613
614 CommandOptions m_options;
615};
616
617
Greg Claytonb3448432011-03-24 21:19:54 +0000618OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000619CommandObjectProcessAttach::CommandOptions::g_option_table[] =
620{
Johnny Chen7c099972012-05-24 00:43:00 +0000621{ LLDB_OPT_SET_ALL, false, "continue",'c', no_argument, NULL, 0, eArgTypeNone, "Immediately continue the process once attached."},
622{ LLDB_OPT_SET_ALL, false, "plugin", 'P', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
623{ LLDB_OPT_SET_1, false, "pid", 'p', required_argument, NULL, 0, eArgTypePid, "The process ID of an existing process to attach to."},
624{ LLDB_OPT_SET_2, false, "name", 'n', required_argument, NULL, 0, eArgTypeProcessName, "The name of the process to attach to."},
625{ LLDB_OPT_SET_2, false, "waitfor", 'w', no_argument, NULL, 0, eArgTypeNone, "Wait for the the process with <process-name> to launch."},
Caroline Tice4d6675c2010-10-01 19:59:14 +0000626{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000627};
628
629//-------------------------------------------------------------------------
630// CommandObjectProcessContinue
631//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000632#pragma mark CommandObjectProcessContinue
Chris Lattner24943d22010-06-08 16:52:24 +0000633
634class CommandObjectProcessContinue : public CommandObject
635{
636public:
637
Greg Clayton238c0a12010-09-18 01:14:36 +0000638 CommandObjectProcessContinue (CommandInterpreter &interpreter) :
639 CommandObject (interpreter,
640 "process continue",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000641 "Continue execution of all threads in the current process.",
Chris Lattner24943d22010-06-08 16:52:24 +0000642 "process continue",
643 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
644 {
645 }
646
647
648 ~CommandObjectProcessContinue ()
649 {
650 }
651
652 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000653 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000654 CommandReturnObject &result)
655 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000656 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton238c0a12010-09-18 01:14:36 +0000657 bool synchronous_execution = m_interpreter.GetSynchronous ();
Chris Lattner24943d22010-06-08 16:52:24 +0000658
659 if (process == NULL)
660 {
661 result.AppendError ("no process to continue");
662 result.SetStatus (eReturnStatusFailed);
663 return false;
664 }
665
666 StateType state = process->GetState();
667 if (state == eStateStopped)
668 {
669 if (command.GetArgumentCount() != 0)
670 {
671 result.AppendErrorWithFormat ("The '%s' command does not take any arguments.\n", m_cmd_name.c_str());
672 result.SetStatus (eReturnStatusFailed);
673 return false;
674 }
675
676 const uint32_t num_threads = process->GetThreadList().GetSize();
677
678 // Set the actions that the threads should each take when resuming
679 for (uint32_t idx=0; idx<num_threads; ++idx)
680 {
681 process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState (eStateRunning);
682 }
683
684 Error error(process->Resume());
685 if (error.Success())
686 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000687 result.AppendMessageWithFormat ("Process %llu resuming\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000688 if (synchronous_execution)
689 {
Greg Claytonbef15832010-07-14 00:18:15 +0000690 state = process->WaitForProcessToStop (NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000691
692 result.SetDidChangeProcessState (true);
Greg Clayton444e35b2011-10-19 18:09:39 +0000693 result.AppendMessageWithFormat ("Process %llu %s\n", process->GetID(), StateAsCString (state));
Chris Lattner24943d22010-06-08 16:52:24 +0000694 result.SetStatus (eReturnStatusSuccessFinishNoResult);
695 }
696 else
697 {
698 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
699 }
700 }
701 else
702 {
703 result.AppendErrorWithFormat("Failed to resume process: %s.\n", error.AsCString());
704 result.SetStatus (eReturnStatusFailed);
705 }
706 }
707 else
708 {
709 result.AppendErrorWithFormat ("Process cannot be continued from its current state (%s).\n",
710 StateAsCString(state));
711 result.SetStatus (eReturnStatusFailed);
712 }
713 return result.Succeeded();
714 }
715};
716
717//-------------------------------------------------------------------------
718// CommandObjectProcessDetach
719//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000720#pragma mark CommandObjectProcessDetach
Chris Lattner24943d22010-06-08 16:52:24 +0000721
722class CommandObjectProcessDetach : public CommandObject
723{
724public:
725
Greg Clayton238c0a12010-09-18 01:14:36 +0000726 CommandObjectProcessDetach (CommandInterpreter &interpreter) :
727 CommandObject (interpreter,
728 "process detach",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000729 "Detach from the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +0000730 "process detach",
731 eFlagProcessMustBeLaunched)
732 {
733 }
734
735 ~CommandObjectProcessDetach ()
736 {
737 }
738
739 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000740 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000741 CommandReturnObject &result)
742 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000743 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000744 if (process == NULL)
745 {
746 result.AppendError ("must have a valid process in order to detach");
747 result.SetStatus (eReturnStatusFailed);
748 return false;
749 }
750
Greg Clayton444e35b2011-10-19 18:09:39 +0000751 result.AppendMessageWithFormat ("Detaching from process %llu\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000752 Error error (process->Detach());
753 if (error.Success())
754 {
755 result.SetStatus (eReturnStatusSuccessFinishResult);
756 }
757 else
758 {
759 result.AppendErrorWithFormat ("Detach failed: %s\n", error.AsCString());
760 result.SetStatus (eReturnStatusFailed);
761 return false;
762 }
763 return result.Succeeded();
764 }
765};
766
767//-------------------------------------------------------------------------
Greg Claytone71e2582011-02-04 01:58:07 +0000768// CommandObjectProcessConnect
769//-------------------------------------------------------------------------
770#pragma mark CommandObjectProcessConnect
771
772class CommandObjectProcessConnect : public CommandObject
773{
774public:
775
776 class CommandOptions : public Options
777 {
778 public:
779
Greg Claytonf15996e2011-04-07 22:46:35 +0000780 CommandOptions (CommandInterpreter &interpreter) :
781 Options(interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000782 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000783 // Keep default values of all options in one place: OptionParsingStarting ()
784 OptionParsingStarting ();
Greg Claytone71e2582011-02-04 01:58:07 +0000785 }
786
787 ~CommandOptions ()
788 {
789 }
790
791 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000792 SetOptionValue (uint32_t option_idx, const char *option_arg)
Greg Claytone71e2582011-02-04 01:58:07 +0000793 {
794 Error error;
795 char short_option = (char) m_getopt_table[option_idx].val;
796
797 switch (short_option)
798 {
799 case 'p':
800 plugin_name.assign (option_arg);
801 break;
802
803 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000804 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Greg Claytone71e2582011-02-04 01:58:07 +0000805 break;
806 }
807 return error;
808 }
809
810 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000811 OptionParsingStarting ()
Greg Claytone71e2582011-02-04 01:58:07 +0000812 {
Greg Claytone71e2582011-02-04 01:58:07 +0000813 plugin_name.clear();
814 }
815
Greg Claytonb3448432011-03-24 21:19:54 +0000816 const OptionDefinition*
Greg Claytone71e2582011-02-04 01:58:07 +0000817 GetDefinitions ()
818 {
819 return g_option_table;
820 }
821
822 // Options table: Required for subclasses of Options.
823
Greg Claytonb3448432011-03-24 21:19:54 +0000824 static OptionDefinition g_option_table[];
Greg Claytone71e2582011-02-04 01:58:07 +0000825
826 // Instance variables to hold the values for command options.
827
828 std::string plugin_name;
829 };
830
831 CommandObjectProcessConnect (CommandInterpreter &interpreter) :
Greg Claytonf15996e2011-04-07 22:46:35 +0000832 CommandObject (interpreter,
833 "process connect",
834 "Connect to a remote debug service.",
835 "process connect <remote-url>",
836 0),
837 m_options (interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000838 {
839 }
840
841 ~CommandObjectProcessConnect ()
842 {
843 }
844
845
846 bool
847 Execute (Args& command,
848 CommandReturnObject &result)
849 {
850
851 TargetSP target_sp (m_interpreter.GetDebugger().GetSelectedTarget());
852 Error error;
Greg Clayton567e7f32011-09-22 04:58:26 +0000853 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytone71e2582011-02-04 01:58:07 +0000854 if (process)
855 {
856 if (process->IsAlive())
857 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000858 result.AppendErrorWithFormat ("Process %llu is currently being debugged, kill the process before connecting.\n",
Greg Claytone71e2582011-02-04 01:58:07 +0000859 process->GetID());
860 result.SetStatus (eReturnStatusFailed);
861 return false;
862 }
863 }
864
865 if (!target_sp)
866 {
867 // If there isn't a current target create one.
868 FileSpec emptyFileSpec;
Greg Claytone71e2582011-02-04 01:58:07 +0000869
870 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
871 emptyFileSpec,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000872 NULL,
Greg Claytone71e2582011-02-04 01:58:07 +0000873 false,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000874 NULL, // No platform options
Greg Claytone71e2582011-02-04 01:58:07 +0000875 target_sp);
876 if (!target_sp || error.Fail())
877 {
878 result.AppendError(error.AsCString("Error creating target"));
879 result.SetStatus (eReturnStatusFailed);
880 return false;
881 }
882 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target_sp.get());
883 }
884
885 if (command.GetArgumentCount() == 1)
886 {
887 const char *plugin_name = NULL;
888 if (!m_options.plugin_name.empty())
889 plugin_name = m_options.plugin_name.c_str();
890
891 const char *remote_url = command.GetArgumentAtIndex(0);
Greg Clayton46c9a352012-02-09 06:16:32 +0000892 process = target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name, NULL).get();
Greg Claytone71e2582011-02-04 01:58:07 +0000893
894 if (process)
895 {
896 error = process->ConnectRemote (remote_url);
897
898 if (error.Fail())
899 {
900 result.AppendError(error.AsCString("Remote connect failed"));
901 result.SetStatus (eReturnStatusFailed);
Greg Clayton0cbb93b2012-03-31 00:10:30 +0000902 target_sp->DeleteCurrentProcess();
Greg Claytone71e2582011-02-04 01:58:07 +0000903 return false;
904 }
905 }
906 else
907 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000908 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",
909 m_cmd_name.c_str());
Greg Claytone71e2582011-02-04 01:58:07 +0000910 result.SetStatus (eReturnStatusFailed);
911 }
912 }
913 else
914 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000915 result.AppendErrorWithFormat ("'%s' takes exactly one argument:\nUsage: %s\n",
Greg Claytone71e2582011-02-04 01:58:07 +0000916 m_cmd_name.c_str(),
917 m_cmd_syntax.c_str());
918 result.SetStatus (eReturnStatusFailed);
919 }
920 return result.Succeeded();
921 }
922
923 Options *
924 GetOptions ()
925 {
926 return &m_options;
927 }
928
929protected:
930
931 CommandOptions m_options;
932};
933
934
Greg Claytonb3448432011-03-24 21:19:54 +0000935OptionDefinition
Greg Claytone71e2582011-02-04 01:58:07 +0000936CommandObjectProcessConnect::CommandOptions::g_option_table[] =
937{
938 { LLDB_OPT_SET_ALL, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
939 { 0, false, NULL, 0 , 0, NULL, 0, eArgTypeNone, NULL }
940};
941
942//-------------------------------------------------------------------------
Greg Clayton0baa3942010-11-04 01:54:29 +0000943// CommandObjectProcessLoad
944//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000945#pragma mark CommandObjectProcessLoad
Greg Clayton0baa3942010-11-04 01:54:29 +0000946
947class CommandObjectProcessLoad : public CommandObject
948{
949public:
950
951 CommandObjectProcessLoad (CommandInterpreter &interpreter) :
952 CommandObject (interpreter,
953 "process load",
954 "Load a shared library into the current process.",
955 "process load <filename> [<filename> ...]",
956 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
957 {
958 }
959
960 ~CommandObjectProcessLoad ()
961 {
962 }
963
964 bool
965 Execute (Args& command,
966 CommandReturnObject &result)
967 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000968 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton0baa3942010-11-04 01:54:29 +0000969 if (process == NULL)
970 {
971 result.AppendError ("must have a valid process in order to load a shared library");
972 result.SetStatus (eReturnStatusFailed);
973 return false;
974 }
975
976 const uint32_t argc = command.GetArgumentCount();
977
978 for (uint32_t i=0; i<argc; ++i)
979 {
980 Error error;
981 const char *image_path = command.GetArgumentAtIndex(i);
982 FileSpec image_spec (image_path, false);
Greg Claytonf2bf8702011-08-11 16:25:18 +0000983 process->GetTarget().GetPlatform()->ResolveRemotePath(image_spec, image_spec);
Greg Clayton0baa3942010-11-04 01:54:29 +0000984 uint32_t image_token = process->LoadImage(image_spec, error);
985 if (image_token != LLDB_INVALID_IMAGE_TOKEN)
986 {
987 result.AppendMessageWithFormat ("Loading \"%s\"...ok\nImage %u loaded.\n", image_path, image_token);
988 result.SetStatus (eReturnStatusSuccessFinishResult);
989 }
990 else
991 {
992 result.AppendErrorWithFormat ("failed to load '%s': %s", image_path, error.AsCString());
993 result.SetStatus (eReturnStatusFailed);
994 }
995 }
996 return result.Succeeded();
997 }
998};
999
1000
1001//-------------------------------------------------------------------------
1002// CommandObjectProcessUnload
1003//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001004#pragma mark CommandObjectProcessUnload
Greg Clayton0baa3942010-11-04 01:54:29 +00001005
1006class CommandObjectProcessUnload : public CommandObject
1007{
1008public:
1009
1010 CommandObjectProcessUnload (CommandInterpreter &interpreter) :
1011 CommandObject (interpreter,
1012 "process unload",
1013 "Unload a shared library from the current process using the index returned by a previous call to \"process load\".",
1014 "process unload <index>",
1015 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
1016 {
1017 }
1018
1019 ~CommandObjectProcessUnload ()
1020 {
1021 }
1022
1023 bool
1024 Execute (Args& command,
1025 CommandReturnObject &result)
1026 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001027 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton0baa3942010-11-04 01:54:29 +00001028 if (process == NULL)
1029 {
1030 result.AppendError ("must have a valid process in order to load a shared library");
1031 result.SetStatus (eReturnStatusFailed);
1032 return false;
1033 }
1034
1035 const uint32_t argc = command.GetArgumentCount();
1036
1037 for (uint32_t i=0; i<argc; ++i)
1038 {
1039 const char *image_token_cstr = command.GetArgumentAtIndex(i);
1040 uint32_t image_token = Args::StringToUInt32(image_token_cstr, LLDB_INVALID_IMAGE_TOKEN, 0);
1041 if (image_token == LLDB_INVALID_IMAGE_TOKEN)
1042 {
1043 result.AppendErrorWithFormat ("invalid image index argument '%s'", image_token_cstr);
1044 result.SetStatus (eReturnStatusFailed);
1045 break;
1046 }
1047 else
1048 {
1049 Error error (process->UnloadImage(image_token));
1050 if (error.Success())
1051 {
1052 result.AppendMessageWithFormat ("Unloading shared library with index %u...ok\n", image_token);
1053 result.SetStatus (eReturnStatusSuccessFinishResult);
1054 }
1055 else
1056 {
1057 result.AppendErrorWithFormat ("failed to unload image: %s", error.AsCString());
1058 result.SetStatus (eReturnStatusFailed);
1059 break;
1060 }
1061 }
1062 }
1063 return result.Succeeded();
1064 }
1065};
1066
1067//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001068// CommandObjectProcessSignal
1069//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001070#pragma mark CommandObjectProcessSignal
Chris Lattner24943d22010-06-08 16:52:24 +00001071
1072class CommandObjectProcessSignal : public CommandObject
1073{
1074public:
1075
Greg Clayton238c0a12010-09-18 01:14:36 +00001076 CommandObjectProcessSignal (CommandInterpreter &interpreter) :
1077 CommandObject (interpreter,
1078 "process signal",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001079 "Send a UNIX signal to the current process being debugged.",
Caroline Tice43b014a2010-10-04 22:28:36 +00001080 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001081 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001082 CommandArgumentEntry arg;
1083 CommandArgumentData signal_arg;
1084
1085 // Define the first (and only) variant of this arg.
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001086 signal_arg.arg_type = eArgTypeUnixSignal;
Caroline Tice43b014a2010-10-04 22:28:36 +00001087 signal_arg.arg_repetition = eArgRepeatPlain;
1088
1089 // There is only one variant this argument could be; put it into the argument entry.
1090 arg.push_back (signal_arg);
1091
1092 // Push the data for the first argument into the m_arguments vector.
1093 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001094 }
1095
1096 ~CommandObjectProcessSignal ()
1097 {
1098 }
1099
1100 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001101 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001102 CommandReturnObject &result)
1103 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001104 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001105 if (process == NULL)
1106 {
1107 result.AppendError ("no process to signal");
1108 result.SetStatus (eReturnStatusFailed);
1109 return false;
1110 }
1111
1112 if (command.GetArgumentCount() == 1)
1113 {
Greg Clayton8f6be2a2010-10-09 01:40:57 +00001114 int signo = LLDB_INVALID_SIGNAL_NUMBER;
1115
1116 const char *signal_name = command.GetArgumentAtIndex(0);
1117 if (::isxdigit (signal_name[0]))
1118 signo = Args::StringToSInt32(signal_name, LLDB_INVALID_SIGNAL_NUMBER, 0);
1119 else
1120 signo = process->GetUnixSignals().GetSignalNumberFromName (signal_name);
1121
1122 if (signo == LLDB_INVALID_SIGNAL_NUMBER)
Chris Lattner24943d22010-06-08 16:52:24 +00001123 {
1124 result.AppendErrorWithFormat ("Invalid signal argument '%s'.\n", command.GetArgumentAtIndex(0));
1125 result.SetStatus (eReturnStatusFailed);
1126 }
1127 else
1128 {
1129 Error error (process->Signal (signo));
1130 if (error.Success())
1131 {
1132 result.SetStatus (eReturnStatusSuccessFinishResult);
1133 }
1134 else
1135 {
1136 result.AppendErrorWithFormat ("Failed to send signal %i: %s\n", signo, error.AsCString());
1137 result.SetStatus (eReturnStatusFailed);
1138 }
1139 }
1140 }
1141 else
1142 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001143 result.AppendErrorWithFormat("'%s' takes exactly one signal number argument:\nUsage: %s\n", m_cmd_name.c_str(),
Chris Lattner24943d22010-06-08 16:52:24 +00001144 m_cmd_syntax.c_str());
1145 result.SetStatus (eReturnStatusFailed);
1146 }
1147 return result.Succeeded();
1148 }
1149};
1150
1151
1152//-------------------------------------------------------------------------
1153// CommandObjectProcessInterrupt
1154//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001155#pragma mark CommandObjectProcessInterrupt
Chris Lattner24943d22010-06-08 16:52:24 +00001156
1157class CommandObjectProcessInterrupt : public CommandObject
1158{
1159public:
1160
1161
Greg Clayton238c0a12010-09-18 01:14:36 +00001162 CommandObjectProcessInterrupt (CommandInterpreter &interpreter) :
1163 CommandObject (interpreter,
1164 "process interrupt",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001165 "Interrupt the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +00001166 "process interrupt",
1167 eFlagProcessMustBeLaunched)
1168 {
1169 }
1170
1171 ~CommandObjectProcessInterrupt ()
1172 {
1173 }
1174
1175 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001176 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001177 CommandReturnObject &result)
1178 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001179 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001180 if (process == NULL)
1181 {
1182 result.AppendError ("no process to halt");
1183 result.SetStatus (eReturnStatusFailed);
1184 return false;
1185 }
1186
1187 if (command.GetArgumentCount() == 0)
1188 {
1189 Error error(process->Halt ());
1190 if (error.Success())
1191 {
1192 result.SetStatus (eReturnStatusSuccessFinishResult);
1193
1194 // Maybe we should add a "SuspendThreadPlans so we
1195 // can halt, and keep in place all the current thread plans.
1196 process->GetThreadList().DiscardThreadPlans();
1197 }
1198 else
1199 {
1200 result.AppendErrorWithFormat ("Failed to halt process: %s\n", error.AsCString());
1201 result.SetStatus (eReturnStatusFailed);
1202 }
1203 }
1204 else
1205 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001206 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner24943d22010-06-08 16:52:24 +00001207 m_cmd_name.c_str(),
1208 m_cmd_syntax.c_str());
1209 result.SetStatus (eReturnStatusFailed);
1210 }
1211 return result.Succeeded();
1212 }
1213};
1214
1215//-------------------------------------------------------------------------
1216// CommandObjectProcessKill
1217//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001218#pragma mark CommandObjectProcessKill
Chris Lattner24943d22010-06-08 16:52:24 +00001219
1220class CommandObjectProcessKill : public CommandObject
1221{
1222public:
1223
Greg Clayton238c0a12010-09-18 01:14:36 +00001224 CommandObjectProcessKill (CommandInterpreter &interpreter) :
1225 CommandObject (interpreter,
1226 "process kill",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001227 "Terminate the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +00001228 "process kill",
1229 eFlagProcessMustBeLaunched)
1230 {
1231 }
1232
1233 ~CommandObjectProcessKill ()
1234 {
1235 }
1236
1237 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001238 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001239 CommandReturnObject &result)
1240 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001241 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001242 if (process == NULL)
1243 {
1244 result.AppendError ("no process to kill");
1245 result.SetStatus (eReturnStatusFailed);
1246 return false;
1247 }
1248
1249 if (command.GetArgumentCount() == 0)
1250 {
1251 Error error (process->Destroy());
1252 if (error.Success())
1253 {
1254 result.SetStatus (eReturnStatusSuccessFinishResult);
1255 }
1256 else
1257 {
1258 result.AppendErrorWithFormat ("Failed to kill process: %s\n", error.AsCString());
1259 result.SetStatus (eReturnStatusFailed);
1260 }
1261 }
1262 else
1263 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001264 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner24943d22010-06-08 16:52:24 +00001265 m_cmd_name.c_str(),
1266 m_cmd_syntax.c_str());
1267 result.SetStatus (eReturnStatusFailed);
1268 }
1269 return result.Succeeded();
1270 }
1271};
1272
1273//-------------------------------------------------------------------------
Jim Ingham41313fc2010-06-18 01:23:09 +00001274// CommandObjectProcessStatus
1275//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001276#pragma mark CommandObjectProcessStatus
1277
Jim Ingham41313fc2010-06-18 01:23:09 +00001278class CommandObjectProcessStatus : public CommandObject
1279{
1280public:
Greg Clayton238c0a12010-09-18 01:14:36 +00001281 CommandObjectProcessStatus (CommandInterpreter &interpreter) :
1282 CommandObject (interpreter,
1283 "process status",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001284 "Show the current status and location of executing process.",
1285 "process status",
Jim Ingham41313fc2010-06-18 01:23:09 +00001286 0)
1287 {
1288 }
1289
1290 ~CommandObjectProcessStatus()
1291 {
1292 }
1293
1294
1295 bool
1296 Execute
1297 (
1298 Args& command,
Jim Ingham41313fc2010-06-18 01:23:09 +00001299 CommandReturnObject &result
1300 )
1301 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001302 Stream &strm = result.GetOutputStream();
Jim Ingham41313fc2010-06-18 01:23:09 +00001303 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Claytonb72d0f02011-04-12 05:54:46 +00001304 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
Greg Clayton567e7f32011-09-22 04:58:26 +00001305 Process *process = exe_ctx.GetProcessPtr();
1306 if (process)
Jim Ingham41313fc2010-06-18 01:23:09 +00001307 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001308 const bool only_threads_with_stop_reason = true;
1309 const uint32_t start_frame = 0;
1310 const uint32_t num_frames = 1;
1311 const uint32_t num_frames_with_source = 1;
Greg Clayton567e7f32011-09-22 04:58:26 +00001312 process->GetStatus(strm);
1313 process->GetThreadStatus (strm,
1314 only_threads_with_stop_reason,
1315 start_frame,
1316 num_frames,
1317 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001318
Jim Ingham41313fc2010-06-18 01:23:09 +00001319 }
1320 else
1321 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001322 result.AppendError ("No process.");
Jim Ingham41313fc2010-06-18 01:23:09 +00001323 result.SetStatus (eReturnStatusFailed);
1324 }
1325 return result.Succeeded();
1326 }
1327};
1328
1329//-------------------------------------------------------------------------
Caroline Tice23d6f272010-10-13 20:44:39 +00001330// CommandObjectProcessHandle
1331//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001332#pragma mark CommandObjectProcessHandle
Caroline Tice23d6f272010-10-13 20:44:39 +00001333
1334class CommandObjectProcessHandle : public CommandObject
1335{
1336public:
1337
1338 class CommandOptions : public Options
1339 {
1340 public:
1341
Greg Claytonf15996e2011-04-07 22:46:35 +00001342 CommandOptions (CommandInterpreter &interpreter) :
1343 Options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001344 {
Greg Clayton143fcc32011-04-13 00:18:08 +00001345 OptionParsingStarting ();
Caroline Tice23d6f272010-10-13 20:44:39 +00001346 }
1347
1348 ~CommandOptions ()
1349 {
1350 }
1351
1352 Error
Greg Clayton143fcc32011-04-13 00:18:08 +00001353 SetOptionValue (uint32_t option_idx, const char *option_arg)
Caroline Tice23d6f272010-10-13 20:44:39 +00001354 {
1355 Error error;
1356 char short_option = (char) m_getopt_table[option_idx].val;
1357
1358 switch (short_option)
1359 {
1360 case 's':
1361 stop = option_arg;
1362 break;
1363 case 'n':
1364 notify = option_arg;
1365 break;
1366 case 'p':
1367 pass = option_arg;
1368 break;
1369 default:
Greg Clayton9c236732011-10-26 00:56:27 +00001370 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Caroline Tice23d6f272010-10-13 20:44:39 +00001371 break;
1372 }
1373 return error;
1374 }
1375
1376 void
Greg Clayton143fcc32011-04-13 00:18:08 +00001377 OptionParsingStarting ()
Caroline Tice23d6f272010-10-13 20:44:39 +00001378 {
Caroline Tice23d6f272010-10-13 20:44:39 +00001379 stop.clear();
1380 notify.clear();
1381 pass.clear();
1382 }
1383
Greg Claytonb3448432011-03-24 21:19:54 +00001384 const OptionDefinition*
Caroline Tice23d6f272010-10-13 20:44:39 +00001385 GetDefinitions ()
1386 {
1387 return g_option_table;
1388 }
1389
1390 // Options table: Required for subclasses of Options.
1391
Greg Claytonb3448432011-03-24 21:19:54 +00001392 static OptionDefinition g_option_table[];
Caroline Tice23d6f272010-10-13 20:44:39 +00001393
1394 // Instance variables to hold the values for command options.
1395
1396 std::string stop;
1397 std::string notify;
1398 std::string pass;
1399 };
1400
1401
1402 CommandObjectProcessHandle (CommandInterpreter &interpreter) :
1403 CommandObject (interpreter,
1404 "process handle",
Caroline Ticee7471982010-10-14 21:31:13 +00001405 "Show or update what the process and debugger should do with various signals received from the OS.",
Greg Claytonf15996e2011-04-07 22:46:35 +00001406 NULL),
1407 m_options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001408 {
Caroline Ticee7471982010-10-14 21:31:13 +00001409 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 +00001410 CommandArgumentEntry arg;
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001411 CommandArgumentData signal_arg;
Caroline Tice23d6f272010-10-13 20:44:39 +00001412
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001413 signal_arg.arg_type = eArgTypeUnixSignal;
1414 signal_arg.arg_repetition = eArgRepeatStar;
Caroline Tice23d6f272010-10-13 20:44:39 +00001415
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001416 arg.push_back (signal_arg);
Caroline Tice23d6f272010-10-13 20:44:39 +00001417
1418 m_arguments.push_back (arg);
1419 }
1420
1421 ~CommandObjectProcessHandle ()
1422 {
1423 }
1424
1425 Options *
1426 GetOptions ()
1427 {
1428 return &m_options;
1429 }
1430
1431 bool
Caroline Ticee7471982010-10-14 21:31:13 +00001432 VerifyCommandOptionValue (const std::string &option, int &real_value)
Caroline Tice23d6f272010-10-13 20:44:39 +00001433 {
1434 bool okay = true;
1435
Caroline Ticee7471982010-10-14 21:31:13 +00001436 bool success = false;
1437 bool tmp_value = Args::StringToBoolean (option.c_str(), false, &success);
1438
1439 if (success && tmp_value)
1440 real_value = 1;
1441 else if (success && !tmp_value)
1442 real_value = 0;
Caroline Tice23d6f272010-10-13 20:44:39 +00001443 else
1444 {
1445 // If the value isn't 'true' or 'false', it had better be 0 or 1.
Caroline Ticee7471982010-10-14 21:31:13 +00001446 real_value = Args::StringToUInt32 (option.c_str(), 3);
1447 if (real_value != 0 && real_value != 1)
Caroline Tice23d6f272010-10-13 20:44:39 +00001448 okay = false;
1449 }
1450
1451 return okay;
1452 }
1453
Caroline Ticee7471982010-10-14 21:31:13 +00001454 void
1455 PrintSignalHeader (Stream &str)
1456 {
1457 str.Printf ("NAME PASS STOP NOTIFY\n");
1458 str.Printf ("========== ===== ===== ======\n");
1459 }
1460
1461 void
1462 PrintSignal (Stream &str, int32_t signo, const char *sig_name, UnixSignals &signals)
1463 {
1464 bool stop;
1465 bool suppress;
1466 bool notify;
1467
1468 str.Printf ("%-10s ", sig_name);
1469 if (signals.GetSignalInfo (signo, suppress, stop, notify))
1470 {
1471 bool pass = !suppress;
1472 str.Printf ("%s %s %s",
1473 (pass ? "true " : "false"),
1474 (stop ? "true " : "false"),
1475 (notify ? "true " : "false"));
1476 }
1477 str.Printf ("\n");
1478 }
1479
1480 void
1481 PrintSignalInformation (Stream &str, Args &signal_args, int num_valid_signals, UnixSignals &signals)
1482 {
1483 PrintSignalHeader (str);
1484
1485 if (num_valid_signals > 0)
1486 {
1487 size_t num_args = signal_args.GetArgumentCount();
1488 for (size_t i = 0; i < num_args; ++i)
1489 {
1490 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1491 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
1492 PrintSignal (str, signo, signal_args.GetArgumentAtIndex (i), signals);
1493 }
1494 }
1495 else // Print info for ALL signals
1496 {
1497 int32_t signo = signals.GetFirstSignalNumber();
1498 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1499 {
1500 PrintSignal (str, signo, signals.GetSignalAsCString (signo), signals);
1501 signo = signals.GetNextSignalNumber (signo);
1502 }
1503 }
1504 }
1505
Caroline Tice23d6f272010-10-13 20:44:39 +00001506 bool
1507 Execute (Args &signal_args, CommandReturnObject &result)
1508 {
1509 TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
1510
1511 if (!target_sp)
1512 {
1513 result.AppendError ("No current target;"
1514 " cannot handle signals until you have a valid target and process.\n");
1515 result.SetStatus (eReturnStatusFailed);
1516 return false;
1517 }
1518
1519 ProcessSP process_sp = target_sp->GetProcessSP();
1520
1521 if (!process_sp)
1522 {
1523 result.AppendError ("No current process; cannot handle signals until you have a valid process.\n");
1524 result.SetStatus (eReturnStatusFailed);
1525 return false;
1526 }
1527
Caroline Tice23d6f272010-10-13 20:44:39 +00001528 int stop_action = -1; // -1 means leave the current setting alone
Caroline Ticee7471982010-10-14 21:31:13 +00001529 int pass_action = -1; // -1 means leave the current setting alone
Caroline Tice23d6f272010-10-13 20:44:39 +00001530 int notify_action = -1; // -1 means leave the current setting alone
1531
1532 if (! m_options.stop.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001533 && ! VerifyCommandOptionValue (m_options.stop, stop_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001534 {
1535 result.AppendError ("Invalid argument for command option --stop; must be true or false.\n");
1536 result.SetStatus (eReturnStatusFailed);
1537 return false;
1538 }
1539
1540 if (! m_options.notify.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001541 && ! VerifyCommandOptionValue (m_options.notify, notify_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001542 {
1543 result.AppendError ("Invalid argument for command option --notify; must be true or false.\n");
1544 result.SetStatus (eReturnStatusFailed);
1545 return false;
1546 }
1547
1548 if (! m_options.pass.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001549 && ! VerifyCommandOptionValue (m_options.pass, pass_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001550 {
1551 result.AppendError ("Invalid argument for command option --pass; must be true or false.\n");
1552 result.SetStatus (eReturnStatusFailed);
1553 return false;
1554 }
1555
1556 size_t num_args = signal_args.GetArgumentCount();
1557 UnixSignals &signals = process_sp->GetUnixSignals();
1558 int num_signals_set = 0;
1559
Caroline Ticee7471982010-10-14 21:31:13 +00001560 if (num_args > 0)
Caroline Tice23d6f272010-10-13 20:44:39 +00001561 {
Caroline Ticee7471982010-10-14 21:31:13 +00001562 for (size_t i = 0; i < num_args; ++i)
Caroline Tice23d6f272010-10-13 20:44:39 +00001563 {
Caroline Ticee7471982010-10-14 21:31:13 +00001564 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1565 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
Caroline Tice23d6f272010-10-13 20:44:39 +00001566 {
Caroline Ticee7471982010-10-14 21:31:13 +00001567 // Casting the actions as bools here should be okay, because VerifyCommandOptionValue guarantees
1568 // the value is either 0 or 1.
1569 if (stop_action != -1)
1570 signals.SetShouldStop (signo, (bool) stop_action);
1571 if (pass_action != -1)
1572 {
1573 bool suppress = ! ((bool) pass_action);
1574 signals.SetShouldSuppress (signo, suppress);
1575 }
1576 if (notify_action != -1)
1577 signals.SetShouldNotify (signo, (bool) notify_action);
1578 ++num_signals_set;
Caroline Tice23d6f272010-10-13 20:44:39 +00001579 }
Caroline Ticee7471982010-10-14 21:31:13 +00001580 else
1581 {
1582 result.AppendErrorWithFormat ("Invalid signal name '%s'\n", signal_args.GetArgumentAtIndex (i));
1583 }
Caroline Tice23d6f272010-10-13 20:44:39 +00001584 }
1585 }
Caroline Ticee7471982010-10-14 21:31:13 +00001586 else
1587 {
1588 // No signal specified, if any command options were specified, update ALL signals.
1589 if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1))
1590 {
1591 if (m_interpreter.Confirm ("Do you really want to update all the signals?", false))
1592 {
1593 int32_t signo = signals.GetFirstSignalNumber();
1594 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1595 {
1596 if (notify_action != -1)
1597 signals.SetShouldNotify (signo, (bool) notify_action);
1598 if (stop_action != -1)
1599 signals.SetShouldStop (signo, (bool) stop_action);
1600 if (pass_action != -1)
1601 {
1602 bool suppress = ! ((bool) pass_action);
1603 signals.SetShouldSuppress (signo, suppress);
1604 }
1605 signo = signals.GetNextSignalNumber (signo);
1606 }
1607 }
1608 }
1609 }
1610
1611 PrintSignalInformation (result.GetOutputStream(), signal_args, num_signals_set, signals);
Caroline Tice23d6f272010-10-13 20:44:39 +00001612
1613 if (num_signals_set > 0)
1614 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1615 else
1616 result.SetStatus (eReturnStatusFailed);
1617
1618 return result.Succeeded();
1619 }
1620
1621protected:
1622
1623 CommandOptions m_options;
1624};
1625
Greg Claytonb3448432011-03-24 21:19:54 +00001626OptionDefinition
Caroline Tice23d6f272010-10-13 20:44:39 +00001627CommandObjectProcessHandle::CommandOptions::g_option_table[] =
1628{
1629{ 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." },
1630{ 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." },
1631{ LLDB_OPT_SET_1, false, "pass", 'p', required_argument, NULL, 0, eArgTypeBoolean, "Whether or not the signal should be passed to the process." },
1632{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1633};
1634
1635//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001636// CommandObjectMultiwordProcess
1637//-------------------------------------------------------------------------
1638
Greg Clayton63094e02010-06-23 01:19:29 +00001639CommandObjectMultiwordProcess::CommandObjectMultiwordProcess (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00001640 CommandObjectMultiword (interpreter,
1641 "process",
1642 "A set of commands for operating on a process.",
1643 "process <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00001644{
Greg Claytona9eb8272011-07-02 21:07:54 +00001645 LoadSubCommand ("attach", CommandObjectSP (new CommandObjectProcessAttach (interpreter)));
1646 LoadSubCommand ("launch", CommandObjectSP (new CommandObjectProcessLaunch (interpreter)));
1647 LoadSubCommand ("continue", CommandObjectSP (new CommandObjectProcessContinue (interpreter)));
1648 LoadSubCommand ("connect", CommandObjectSP (new CommandObjectProcessConnect (interpreter)));
1649 LoadSubCommand ("detach", CommandObjectSP (new CommandObjectProcessDetach (interpreter)));
1650 LoadSubCommand ("load", CommandObjectSP (new CommandObjectProcessLoad (interpreter)));
1651 LoadSubCommand ("unload", CommandObjectSP (new CommandObjectProcessUnload (interpreter)));
1652 LoadSubCommand ("signal", CommandObjectSP (new CommandObjectProcessSignal (interpreter)));
1653 LoadSubCommand ("handle", CommandObjectSP (new CommandObjectProcessHandle (interpreter)));
1654 LoadSubCommand ("status", CommandObjectSP (new CommandObjectProcessStatus (interpreter)));
Greg Clayton238c0a12010-09-18 01:14:36 +00001655 LoadSubCommand ("interrupt", CommandObjectSP (new CommandObjectProcessInterrupt (interpreter)));
Greg Claytona9eb8272011-07-02 21:07:54 +00001656 LoadSubCommand ("kill", CommandObjectSP (new CommandObjectProcessKill (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00001657}
1658
1659CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess ()
1660{
1661}
1662