blob: f45e926adc63ce3256f9ba7b5d12a5726b2f0bff [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
Jim Inghamda26bd22012-06-08 21:56:10 +000034class CommandObjectProcessLaunch : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +000035{
36public:
37
Greg Clayton238c0a12010-09-18 01:14:36 +000038 CommandObjectProcessLaunch (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +000039 CommandObjectParsed (interpreter,
40 "process launch",
41 "Launch the executable in the debugger.",
42 NULL),
Greg Claytonf15996e2011-04-07 22:46:35 +000043 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
Jim Inghamda26bd22012-06-08 21:56:10 +000070 virtual const char *GetRepeatCommand (Args &current_command_args, uint32_t index)
71 {
72 // No repeat for "process launch"...
73 return "";
74 }
75
76protected:
Chris Lattner24943d22010-06-08 16:52:24 +000077 bool
Jim Inghamda26bd22012-06-08 21:56:10 +000078 DoExecute (Args& launch_args, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +000079 {
Greg Claytonabb33022011-11-08 02:43:13 +000080 Debugger &debugger = m_interpreter.GetDebugger();
81 Target *target = debugger.GetSelectedTarget().get();
82 Error error;
Chris Lattner24943d22010-06-08 16:52:24 +000083
84 if (target == NULL)
85 {
Greg Claytone1f50b92011-05-03 22:09:39 +000086 result.AppendError ("invalid target, create a debug target using the 'target create' command");
Chris Lattner24943d22010-06-08 16:52:24 +000087 result.SetStatus (eReturnStatusFailed);
88 return false;
89 }
Chris Lattner24943d22010-06-08 16:52:24 +000090 // If our listener is NULL, users aren't allows to launch
Chris Lattner24943d22010-06-08 16:52:24 +000091 char filename[PATH_MAX];
Greg Clayton5beb99d2011-08-11 02:48:45 +000092 const Module *exe_module = target->GetExecutableModulePointer();
Greg Claytona2f74232011-02-24 22:24:29 +000093
94 if (exe_module == NULL)
95 {
Greg Claytone1f50b92011-05-03 22:09:39 +000096 result.AppendError ("no file in target, create a debug target using the 'target create' command");
Greg Claytona2f74232011-02-24 22:24:29 +000097 result.SetStatus (eReturnStatusFailed);
98 return false;
99 }
100
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000101 exe_module->GetFileSpec().GetPath (filename, sizeof(filename));
Chris Lattner24943d22010-06-08 16:52:24 +0000102
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000103 const bool add_exe_file_as_first_arg = true;
Greg Clayton1d1f39e2011-11-29 04:03:30 +0000104 m_options.launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), add_exe_file_as_first_arg);
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000105
Greg Claytona2f74232011-02-24 22:24:29 +0000106 StateType state = eStateInvalid;
Greg Clayton567e7f32011-09-22 04:58:26 +0000107 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytona2f74232011-02-24 22:24:29 +0000108 if (process)
109 {
110 state = process->GetState();
111
112 if (process->IsAlive() && state != eStateConnected)
113 {
114 char message[1024];
115 if (process->GetState() == eStateAttaching)
116 ::strncpy (message, "There is a pending attach, abort it and launch a new process?", sizeof(message));
117 else
118 ::strncpy (message, "There is a running process, kill it and restart?", sizeof(message));
119
120 if (!m_interpreter.Confirm (message, true))
Jim Ingham22dc9722010-12-09 18:58:16 +0000121 {
Greg Claytona2f74232011-02-24 22:24:29 +0000122 result.SetStatus (eReturnStatusFailed);
123 return false;
Jim Ingham22dc9722010-12-09 18:58:16 +0000124 }
125 else
126 {
Greg Claytonabb33022011-11-08 02:43:13 +0000127 Error destroy_error (process->Destroy());
128 if (destroy_error.Success())
Greg Claytona2f74232011-02-24 22:24:29 +0000129 {
130 result.SetStatus (eReturnStatusSuccessFinishResult);
131 }
132 else
133 {
Greg Claytonabb33022011-11-08 02:43:13 +0000134 result.AppendErrorWithFormat ("Failed to kill process: %s\n", destroy_error.AsCString());
Greg Claytona2f74232011-02-24 22:24:29 +0000135 result.SetStatus (eReturnStatusFailed);
136 }
Jim Ingham22dc9722010-12-09 18:58:16 +0000137 }
138 }
Chris Lattner24943d22010-06-08 16:52:24 +0000139 }
Jim Ingham22dc9722010-12-09 18:58:16 +0000140
Greg Clayton527154d2011-11-15 03:53:30 +0000141 if (launch_args.GetArgumentCount() == 0)
142 {
143 const Args &process_args = target->GetRunArguments();
144 if (process_args.GetArgumentCount() > 0)
145 m_options.launch_info.GetArguments().AppendArguments (process_args);
146 }
147 else
Greg Claytonabb33022011-11-08 02:43:13 +0000148 {
Greg Clayton3e6f2cc2011-11-21 21:51:18 +0000149 // Save the arguments for subsequent runs in the current target.
150 target->SetRunArguments (launch_args);
151
Greg Claytonabb33022011-11-08 02:43:13 +0000152 m_options.launch_info.GetArguments().AppendArguments (launch_args);
153 }
Greg Claytonabb33022011-11-08 02:43:13 +0000154
Greg Clayton527154d2011-11-15 03:53:30 +0000155 if (target->GetDisableASLR())
156 m_options.launch_info.GetFlags().Set (eLaunchFlagDisableASLR);
157
158 if (target->GetDisableSTDIO())
159 m_options.launch_info.GetFlags().Set (eLaunchFlagDisableSTDIO);
160
161 m_options.launch_info.GetFlags().Set (eLaunchFlagDebug);
162
163 Args environment;
164 target->GetEnvironmentAsArgs (environment);
165 if (environment.GetArgumentCount() > 0)
166 m_options.launch_info.GetEnvironmentEntries ().AppendArguments (environment);
167
Greg Clayton464c6162011-11-17 22:14:31 +0000168 // Finalize the file actions, and if none were given, default to opening
169 // up a pseudo terminal
170 const bool default_to_use_pty = true;
171 m_options.launch_info.FinalizeFileActions (target, default_to_use_pty);
Greg Clayton527154d2011-11-15 03:53:30 +0000172
Greg Claytonabb33022011-11-08 02:43:13 +0000173 if (state == eStateConnected)
174 {
175 if (m_options.launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
176 {
177 result.AppendWarning("can't launch in tty when launching through a remote connection");
178 m_options.launch_info.GetFlags().Clear (eLaunchFlagLaunchInTTY);
179 }
180 }
181 else
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000182 {
Greg Clayton527154d2011-11-15 03:53:30 +0000183 if (!m_options.launch_info.GetArchitecture().IsValid())
Greg Clayton2d9adb72011-11-12 02:10:56 +0000184 m_options.launch_info.GetArchitecture() = target->GetArchitecture();
185
Greg Clayton75d8c252011-11-28 01:45:00 +0000186 PlatformSP platform_sp (target->GetPlatform());
187
188 if (platform_sp && platform_sp->CanDebugProcess ())
189 {
190 process = target->GetPlatform()->DebugProcess (m_options.launch_info,
191 debugger,
192 target,
193 debugger.GetListener(),
194 error).get();
195 }
196 else
197 {
198 const char *plugin_name = m_options.launch_info.GetProcessPluginName();
Greg Clayton46c9a352012-02-09 06:16:32 +0000199 process = target->CreateProcess (debugger.GetListener(), plugin_name, NULL).get();
Greg Clayton75d8c252011-11-28 01:45:00 +0000200 if (process)
201 error = process->Launch (m_options.launch_info);
202 }
Greg Claytonabb33022011-11-08 02:43:13 +0000203
Greg Claytona2f74232011-02-24 22:24:29 +0000204 if (process == NULL)
205 {
Greg Clayton527154d2011-11-15 03:53:30 +0000206 result.SetError (error, "failed to launch or debug process");
Greg Claytona2f74232011-02-24 22:24:29 +0000207 return false;
208 }
Chris Lattner24943d22010-06-08 16:52:24 +0000209 }
Greg Claytonabb33022011-11-08 02:43:13 +0000210
Greg Clayton238c0a12010-09-18 01:14:36 +0000211 if (error.Success())
212 {
Greg Clayton940b1032011-02-23 00:35:02 +0000213 const char *archname = exe_module->GetArchitecture().GetArchitectureName();
Greg Claytonc1d37752010-10-18 01:45:30 +0000214
Greg Clayton444e35b2011-10-19 18:09:39 +0000215 result.AppendMessageWithFormat ("Process %llu launched: '%s' (%s)\n", process->GetID(), filename, archname);
Greg Claytond8c62532010-10-07 04:19:01 +0000216 result.SetDidChangeProcessState (true);
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000217 if (m_options.launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
Greg Clayton238c0a12010-09-18 01:14:36 +0000218 {
Greg Claytond8c62532010-10-07 04:19:01 +0000219 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
Greg Clayton238c0a12010-09-18 01:14:36 +0000220 StateType state = process->WaitForProcessToStop (NULL);
221
222 if (state == eStateStopped)
223 {
Greg Claytond8c62532010-10-07 04:19:01 +0000224 error = process->Resume();
225 if (error.Success())
226 {
227 bool synchronous_execution = m_interpreter.GetSynchronous ();
228 if (synchronous_execution)
229 {
230 state = process->WaitForProcessToStop (NULL);
Greg Clayton20206082011-11-17 01:23:07 +0000231 const bool must_be_alive = true;
232 if (!StateIsStoppedState(state, must_be_alive))
Greg Clayton395fc332011-02-15 21:59:32 +0000233 {
Greg Clayton527154d2011-11-15 03:53:30 +0000234 result.AppendErrorWithFormat ("process isn't stopped: %s", StateAsCString(state));
Greg Clayton395fc332011-02-15 21:59:32 +0000235 }
Greg Claytond8c62532010-10-07 04:19:01 +0000236 result.SetDidChangeProcessState (true);
237 result.SetStatus (eReturnStatusSuccessFinishResult);
238 }
239 else
240 {
241 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
242 }
243 }
Greg Clayton395fc332011-02-15 21:59:32 +0000244 else
245 {
Greg Clayton527154d2011-11-15 03:53:30 +0000246 result.AppendErrorWithFormat ("process resume at entry point failed: %s", error.AsCString());
Greg Clayton395fc332011-02-15 21:59:32 +0000247 result.SetStatus (eReturnStatusFailed);
248 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000249 }
Greg Clayton395fc332011-02-15 21:59:32 +0000250 else
251 {
Greg Clayton527154d2011-11-15 03:53:30 +0000252 result.AppendErrorWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
Greg Clayton395fc332011-02-15 21:59:32 +0000253 result.SetStatus (eReturnStatusFailed);
254 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000255 }
256 }
Greg Clayton395fc332011-02-15 21:59:32 +0000257 else
258 {
Greg Claytona9eb8272011-07-02 21:07:54 +0000259 result.AppendErrorWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton395fc332011-02-15 21:59:32 +0000260 result.SetStatus (eReturnStatusFailed);
261 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000262
Chris Lattner24943d22010-06-08 16:52:24 +0000263 return result.Succeeded();
264 }
265
266protected:
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000267 ProcessLaunchCommandOptions m_options;
Chris Lattner24943d22010-06-08 16:52:24 +0000268};
269
270
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000271//#define SET1 LLDB_OPT_SET_1
272//#define SET2 LLDB_OPT_SET_2
273//#define SET3 LLDB_OPT_SET_3
274//
275//OptionDefinition
276//CommandObjectProcessLaunch::CommandOptions::g_option_table[] =
277//{
278//{ 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."},
279//{ SET1 , false, "stdin", 'i', required_argument, NULL, 0, eArgTypePath, "Redirect stdin for the process to <path>."},
280//{ SET1 , false, "stdout", 'o', required_argument, NULL, 0, eArgTypePath, "Redirect stdout for the process to <path>."},
281//{ SET1 , false, "stderr", 'e', required_argument, NULL, 0, eArgTypePath, "Redirect stderr for the process to <path>."},
282//{ SET1 | SET2 | SET3, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
283//{ 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."},
284//{ SET3, false, "no-stdio", 'n', no_argument, NULL, 0, eArgTypeNone, "Do not set up for terminal I/O to go to running process."},
285//{ SET1 | SET2 | SET3, false, "working-dir", 'w', required_argument, NULL, 0, eArgTypePath, "Set the current working directory to <path> when running the inferior."},
286//{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
287//};
288//
289//#undef SET1
290//#undef SET2
291//#undef SET3
Chris Lattner24943d22010-06-08 16:52:24 +0000292
293//-------------------------------------------------------------------------
294// CommandObjectProcessAttach
295//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000296#pragma mark CommandObjectProcessAttach
Jim Inghamda26bd22012-06-08 21:56:10 +0000297class CommandObjectProcessAttach : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000298{
299public:
300
Chris Lattner24943d22010-06-08 16:52:24 +0000301 class CommandOptions : public Options
302 {
303 public:
304
Greg Claytonf15996e2011-04-07 22:46:35 +0000305 CommandOptions (CommandInterpreter &interpreter) :
306 Options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000307 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000308 // Keep default values of all options in one place: OptionParsingStarting ()
309 OptionParsingStarting ();
Chris Lattner24943d22010-06-08 16:52:24 +0000310 }
311
312 ~CommandOptions ()
313 {
314 }
315
316 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000317 SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +0000318 {
319 Error error;
320 char short_option = (char) m_getopt_table[option_idx].val;
321 bool success = false;
322 switch (short_option)
323 {
Johnny Chen7c099972012-05-24 00:43:00 +0000324 case 'c':
325 attach_info.SetContinueOnceAttached(true);
326 break;
327
Chris Lattner24943d22010-06-08 16:52:24 +0000328 case 'p':
Chris Lattner24943d22010-06-08 16:52:24 +0000329 {
Greg Clayton527154d2011-11-15 03:53:30 +0000330 lldb::pid_t pid = Args::StringToUInt32 (option_arg, LLDB_INVALID_PROCESS_ID, 0, &success);
331 if (!success || pid == LLDB_INVALID_PROCESS_ID)
332 {
333 error.SetErrorStringWithFormat("invalid process ID '%s'", option_arg);
334 }
335 else
336 {
337 attach_info.SetProcessID (pid);
338 }
Chris Lattner24943d22010-06-08 16:52:24 +0000339 }
340 break;
341
342 case 'P':
Greg Clayton527154d2011-11-15 03:53:30 +0000343 attach_info.SetProcessPluginName (option_arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000344 break;
345
346 case 'n':
Greg Clayton527154d2011-11-15 03:53:30 +0000347 attach_info.GetExecutableFile().SetFile(option_arg, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000348 break;
349
350 case 'w':
Greg Clayton527154d2011-11-15 03:53:30 +0000351 attach_info.SetWaitForLaunch(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000352 break;
Jim Ingham3a458eb2012-07-20 21:37:13 +0000353
354 case 'i':
355 attach_info.SetIgnoreExisting(false);
356 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000357
358 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000359 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner24943d22010-06-08 16:52:24 +0000360 break;
361 }
362 return error;
363 }
364
365 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000366 OptionParsingStarting ()
Chris Lattner24943d22010-06-08 16:52:24 +0000367 {
Greg Clayton527154d2011-11-15 03:53:30 +0000368 attach_info.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000369 }
370
Greg Claytonb3448432011-03-24 21:19:54 +0000371 const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +0000372 GetDefinitions ()
373 {
374 return g_option_table;
375 }
376
Jim Ingham7508e732010-08-09 23:31:02 +0000377 virtual bool
Greg Claytonf15996e2011-04-07 22:46:35 +0000378 HandleOptionArgumentCompletion (Args &input,
Jim Ingham7508e732010-08-09 23:31:02 +0000379 int cursor_index,
380 int char_pos,
381 OptionElementVector &opt_element_vector,
382 int opt_element_index,
383 int match_start_point,
384 int max_return_elements,
385 bool &word_complete,
386 StringList &matches)
387 {
388 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
389 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
390
391 // We are only completing the name option for now...
392
Greg Claytonb3448432011-03-24 21:19:54 +0000393 const OptionDefinition *opt_defs = GetDefinitions();
Jim Ingham7508e732010-08-09 23:31:02 +0000394 if (opt_defs[opt_defs_index].short_option == 'n')
395 {
396 // Are we in the name?
397
398 // Look to see if there is a -P argument provided, and if so use that plugin, otherwise
399 // use the default plugin.
Jim Ingham7508e732010-08-09 23:31:02 +0000400
401 const char *partial_name = NULL;
402 partial_name = input.GetArgumentAtIndex(opt_arg_pos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000403
Greg Claytonb72d0f02011-04-12 05:54:46 +0000404 PlatformSP platform_sp (m_interpreter.GetPlatform (true));
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000405 if (platform_sp)
Jim Ingham7508e732010-08-09 23:31:02 +0000406 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000407 ProcessInstanceInfoList process_infos;
408 ProcessInstanceInfoMatch match_info;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000409 if (partial_name)
410 {
Greg Clayton527154d2011-11-15 03:53:30 +0000411 match_info.GetProcessInfo().GetExecutableFile().SetFile(partial_name, false);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000412 match_info.SetNameMatchType(eNameMatchStartsWith);
413 }
414 platform_sp->FindProcesses (match_info, process_infos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000415 const uint32_t num_matches = process_infos.GetSize();
416 if (num_matches > 0)
417 {
418 for (uint32_t i=0; i<num_matches; ++i)
419 {
420 matches.AppendString (process_infos.GetProcessNameAtIndex(i),
421 process_infos.GetProcessNameLengthAtIndex(i));
422 }
423 }
Jim Ingham7508e732010-08-09 23:31:02 +0000424 }
425 }
426
427 return false;
428 }
429
Chris Lattner24943d22010-06-08 16:52:24 +0000430 // Options table: Required for subclasses of Options.
431
Greg Claytonb3448432011-03-24 21:19:54 +0000432 static OptionDefinition g_option_table[];
Chris Lattner24943d22010-06-08 16:52:24 +0000433
434 // Instance variables to hold the values for command options.
435
Greg Clayton527154d2011-11-15 03:53:30 +0000436 ProcessAttachInfo attach_info;
Chris Lattner24943d22010-06-08 16:52:24 +0000437 };
438
Greg Clayton238c0a12010-09-18 01:14:36 +0000439 CommandObjectProcessAttach (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000440 CommandObjectParsed (interpreter,
441 "process attach",
442 "Attach to a process.",
443 "process attach <cmd-options>"),
Greg Claytonf15996e2011-04-07 22:46:35 +0000444 m_options (interpreter)
Jim Ingham7508e732010-08-09 23:31:02 +0000445 {
Jim Ingham7508e732010-08-09 23:31:02 +0000446 }
447
448 ~CommandObjectProcessAttach ()
449 {
450 }
451
Jim Inghamda26bd22012-06-08 21:56:10 +0000452 Options *
453 GetOptions ()
454 {
455 return &m_options;
456 }
457
458protected:
Jim Ingham7508e732010-08-09 23:31:02 +0000459 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000460 DoExecute (Args& command,
Jim Ingham7508e732010-08-09 23:31:02 +0000461 CommandReturnObject &result)
462 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000463 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Jim Inghamee940e22011-09-15 01:08:57 +0000464 // N.B. The attach should be synchronous. It doesn't help much to get the prompt back between initiating the attach
465 // and the target actually stopping. So even if the interpreter is set to be asynchronous, we wait for the stop
466 // ourselves here.
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000467
Greg Clayton567e7f32011-09-22 04:58:26 +0000468 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytona2f74232011-02-24 22:24:29 +0000469 StateType state = eStateInvalid;
Jim Ingham7508e732010-08-09 23:31:02 +0000470 if (process)
471 {
Greg Claytona2f74232011-02-24 22:24:29 +0000472 state = process->GetState();
473 if (process->IsAlive() && state != eStateConnected)
Jim Ingham7508e732010-08-09 23:31:02 +0000474 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000475 result.AppendErrorWithFormat ("Process %llu is currently being debugged, kill the process before attaching.\n",
Jim Ingham7508e732010-08-09 23:31:02 +0000476 process->GetID());
477 result.SetStatus (eReturnStatusFailed);
478 return false;
479 }
480 }
481
482 if (target == NULL)
483 {
484 // If there isn't a current target create one.
485 TargetSP new_target_sp;
486 FileSpec emptyFileSpec;
Jim Ingham7508e732010-08-09 23:31:02 +0000487 Error error;
488
Greg Clayton238c0a12010-09-18 01:14:36 +0000489 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
490 emptyFileSpec,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000491 NULL,
Greg Clayton238c0a12010-09-18 01:14:36 +0000492 false,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000493 NULL, // No platform options
Greg Clayton238c0a12010-09-18 01:14:36 +0000494 new_target_sp);
Jim Ingham7508e732010-08-09 23:31:02 +0000495 target = new_target_sp.get();
496 if (target == NULL || error.Fail())
497 {
Greg Claytone71e2582011-02-04 01:58:07 +0000498 result.AppendError(error.AsCString("Error creating target"));
Jim Ingham7508e732010-08-09 23:31:02 +0000499 return false;
500 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000501 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target);
Jim Ingham7508e732010-08-09 23:31:02 +0000502 }
503
504 // Record the old executable module, we want to issue a warning if the process of attaching changed the
505 // current executable (like somebody said "file foo" then attached to a PID whose executable was bar.)
506
507 ModuleSP old_exec_module_sp = target->GetExecutableModule();
508 ArchSpec old_arch_spec = target->GetArchitecture();
509
510 if (command.GetArgumentCount())
511 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000512 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 +0000513 result.SetStatus (eReturnStatusFailed);
514 }
515 else
516 {
Greg Claytona2f74232011-02-24 22:24:29 +0000517 if (state != eStateConnected)
518 {
Greg Clayton527154d2011-11-15 03:53:30 +0000519 const char *plugin_name = m_options.attach_info.GetProcessPluginName();
Greg Clayton46c9a352012-02-09 06:16:32 +0000520 process = target->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name, NULL).get();
Greg Claytona2f74232011-02-24 22:24:29 +0000521 }
Jim Ingham7508e732010-08-09 23:31:02 +0000522
523 if (process)
524 {
525 Error error;
Greg Clayton527154d2011-11-15 03:53:30 +0000526 // If no process info was specified, then use the target executable
527 // name as the process to attach to by default
528 if (!m_options.attach_info.ProcessInfoSpecified ())
Jim Ingham4805a1c2010-09-15 01:34:14 +0000529 {
530 if (old_exec_module_sp)
Greg Clayton1d1f39e2011-11-29 04:03:30 +0000531 m_options.attach_info.GetExecutableFile().GetFilename() = old_exec_module_sp->GetPlatformFileSpec().GetFilename();
Jim Ingham4805a1c2010-09-15 01:34:14 +0000532
Greg Clayton527154d2011-11-15 03:53:30 +0000533 if (!m_options.attach_info.ProcessInfoSpecified ())
534 {
535 error.SetErrorString ("no process specified, create a target with a file, or specify the --pid or --name command option");
536 }
537 }
538
539 if (error.Success())
540 {
541 error = process->Attach (m_options.attach_info);
542
Jim Ingham4805a1c2010-09-15 01:34:14 +0000543 if (error.Success())
544 {
545 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
546 }
Jim Ingham7508e732010-08-09 23:31:02 +0000547 else
548 {
Greg Clayton527154d2011-11-15 03:53:30 +0000549 result.AppendErrorWithFormat ("attach failed: %s\n", error.AsCString());
Jim Ingham4805a1c2010-09-15 01:34:14 +0000550 result.SetStatus (eReturnStatusFailed);
551 return false;
Jim Ingham7508e732010-08-09 23:31:02 +0000552 }
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000553 // If we're synchronous, wait for the stopped event and report that.
554 // Otherwise just return.
555 // FIXME: in the async case it will now be possible to get to the command
556 // interpreter with a state eStateAttaching. Make sure we handle that correctly.
Jim Inghamee940e22011-09-15 01:08:57 +0000557 StateType state = process->WaitForProcessToStop (NULL);
Greg Clayton527154d2011-11-15 03:53:30 +0000558
Jim Inghamee940e22011-09-15 01:08:57 +0000559 result.SetDidChangeProcessState (true);
Johnny Chen9986a3b2012-05-18 00:51:36 +0000560
561 if (state == eStateStopped)
562 {
563 result.AppendMessageWithFormat ("Process %llu %s\n", process->GetID(), StateAsCString (state));
564 result.SetStatus (eReturnStatusSuccessFinishNoResult);
565 }
566 else
567 {
568 result.AppendError ("attach failed: process did not stop (no such process or permission problem?)");
569 result.SetStatus (eReturnStatusFailed);
570 return false;
571 }
Jim Ingham7508e732010-08-09 23:31:02 +0000572 }
Jim Ingham7508e732010-08-09 23:31:02 +0000573 }
574 }
575
576 if (result.Succeeded())
577 {
578 // Okay, we're done. Last step is to warn if the executable module has changed:
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000579 char new_path[PATH_MAX];
Greg Clayton5beb99d2011-08-11 02:48:45 +0000580 ModuleSP new_exec_module_sp (target->GetExecutableModule());
Jim Ingham7508e732010-08-09 23:31:02 +0000581 if (!old_exec_module_sp)
582 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000583 // We might not have a module if we attached to a raw pid...
Greg Clayton5beb99d2011-08-11 02:48:45 +0000584 if (new_exec_module_sp)
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000585 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000586 new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000587 result.AppendMessageWithFormat("Executable module set to \"%s\".\n", new_path);
588 }
Jim Ingham7508e732010-08-09 23:31:02 +0000589 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000590 else if (old_exec_module_sp->GetFileSpec() != new_exec_module_sp->GetFileSpec())
Jim Ingham7508e732010-08-09 23:31:02 +0000591 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000592 char old_path[PATH_MAX];
Jim Ingham7508e732010-08-09 23:31:02 +0000593
Greg Clayton5beb99d2011-08-11 02:48:45 +0000594 old_exec_module_sp->GetFileSpec().GetPath (old_path, PATH_MAX);
595 new_exec_module_sp->GetFileSpec().GetPath (new_path, PATH_MAX);
Jim Ingham7508e732010-08-09 23:31:02 +0000596
597 result.AppendWarningWithFormat("Executable module changed from \"%s\" to \"%s\".\n",
598 old_path, new_path);
599 }
600
601 if (!old_arch_spec.IsValid())
602 {
Greg Clayton940b1032011-02-23 00:35:02 +0000603 result.AppendMessageWithFormat ("Architecture set to: %s.\n", target->GetArchitecture().GetArchitectureName());
Jim Ingham7508e732010-08-09 23:31:02 +0000604 }
605 else if (old_arch_spec != target->GetArchitecture())
606 {
607 result.AppendWarningWithFormat("Architecture changed from %s to %s.\n",
Greg Clayton940b1032011-02-23 00:35:02 +0000608 old_arch_spec.GetArchitectureName(), target->GetArchitecture().GetArchitectureName());
Jim Ingham7508e732010-08-09 23:31:02 +0000609 }
Johnny Chen7c099972012-05-24 00:43:00 +0000610
611 // This supports the use-case scenario of immediately continuing the process once attached.
612 if (m_options.attach_info.GetContinueOnceAttached())
Sean Callanan4336d932012-05-31 01:30:08 +0000613 m_interpreter.HandleCommand("process continue", eLazyBoolNo, result);
Jim Ingham7508e732010-08-09 23:31:02 +0000614 }
615 return result.Succeeded();
616 }
617
Chris Lattner24943d22010-06-08 16:52:24 +0000618 CommandOptions m_options;
619};
620
621
Greg Claytonb3448432011-03-24 21:19:54 +0000622OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000623CommandObjectProcessAttach::CommandOptions::g_option_table[] =
624{
Jim Ingham3a458eb2012-07-20 21:37:13 +0000625{ LLDB_OPT_SET_ALL, false, "continue",'c', no_argument, NULL, 0, eArgTypeNone, "Immediately continue the process once attached."},
626{ LLDB_OPT_SET_ALL, false, "plugin", 'P', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
627{ LLDB_OPT_SET_1, false, "pid", 'p', required_argument, NULL, 0, eArgTypePid, "The process ID of an existing process to attach to."},
628{ LLDB_OPT_SET_2, false, "name", 'n', required_argument, NULL, 0, eArgTypeProcessName, "The name of the process to attach to."},
629{ LLDB_OPT_SET_2, false, "include-existing", 'i', no_argument, NULL, 0, eArgTypeNone, "Include existing processes when doing attach -w."},
630{ LLDB_OPT_SET_2, false, "waitfor", 'w', no_argument, NULL, 0, eArgTypeNone, "Wait for the process with <process-name> to launch."},
Caroline Tice4d6675c2010-10-01 19:59:14 +0000631{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000632};
633
634//-------------------------------------------------------------------------
635// CommandObjectProcessContinue
636//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000637#pragma mark CommandObjectProcessContinue
Chris Lattner24943d22010-06-08 16:52:24 +0000638
Jim Inghamda26bd22012-06-08 21:56:10 +0000639class CommandObjectProcessContinue : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000640{
641public:
642
Greg Clayton238c0a12010-09-18 01:14:36 +0000643 CommandObjectProcessContinue (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000644 CommandObjectParsed (interpreter,
645 "process continue",
646 "Continue execution of all threads in the current process.",
647 "process continue",
648 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
Chris Lattner24943d22010-06-08 16:52:24 +0000649 {
650 }
651
652
653 ~CommandObjectProcessContinue ()
654 {
655 }
656
Jim Inghamda26bd22012-06-08 21:56:10 +0000657protected:
Chris Lattner24943d22010-06-08 16:52:24 +0000658 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000659 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000660 CommandReturnObject &result)
661 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000662 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton238c0a12010-09-18 01:14:36 +0000663 bool synchronous_execution = m_interpreter.GetSynchronous ();
Chris Lattner24943d22010-06-08 16:52:24 +0000664
665 if (process == NULL)
666 {
667 result.AppendError ("no process to continue");
668 result.SetStatus (eReturnStatusFailed);
669 return false;
670 }
671
672 StateType state = process->GetState();
673 if (state == eStateStopped)
674 {
675 if (command.GetArgumentCount() != 0)
676 {
677 result.AppendErrorWithFormat ("The '%s' command does not take any arguments.\n", m_cmd_name.c_str());
678 result.SetStatus (eReturnStatusFailed);
679 return false;
680 }
681
682 const uint32_t num_threads = process->GetThreadList().GetSize();
683
684 // Set the actions that the threads should each take when resuming
685 for (uint32_t idx=0; idx<num_threads; ++idx)
686 {
687 process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState (eStateRunning);
688 }
689
690 Error error(process->Resume());
691 if (error.Success())
692 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000693 result.AppendMessageWithFormat ("Process %llu resuming\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000694 if (synchronous_execution)
695 {
Greg Claytonbef15832010-07-14 00:18:15 +0000696 state = process->WaitForProcessToStop (NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000697
698 result.SetDidChangeProcessState (true);
Greg Clayton444e35b2011-10-19 18:09:39 +0000699 result.AppendMessageWithFormat ("Process %llu %s\n", process->GetID(), StateAsCString (state));
Chris Lattner24943d22010-06-08 16:52:24 +0000700 result.SetStatus (eReturnStatusSuccessFinishNoResult);
701 }
702 else
703 {
704 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
705 }
706 }
707 else
708 {
709 result.AppendErrorWithFormat("Failed to resume process: %s.\n", error.AsCString());
710 result.SetStatus (eReturnStatusFailed);
711 }
712 }
713 else
714 {
715 result.AppendErrorWithFormat ("Process cannot be continued from its current state (%s).\n",
716 StateAsCString(state));
717 result.SetStatus (eReturnStatusFailed);
718 }
719 return result.Succeeded();
720 }
721};
722
723//-------------------------------------------------------------------------
724// CommandObjectProcessDetach
725//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000726#pragma mark CommandObjectProcessDetach
Chris Lattner24943d22010-06-08 16:52:24 +0000727
Jim Inghamda26bd22012-06-08 21:56:10 +0000728class CommandObjectProcessDetach : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000729{
730public:
731
Greg Clayton238c0a12010-09-18 01:14:36 +0000732 CommandObjectProcessDetach (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000733 CommandObjectParsed (interpreter,
734 "process detach",
735 "Detach from the current process being debugged.",
736 "process detach",
737 eFlagProcessMustBeLaunched)
Chris Lattner24943d22010-06-08 16:52:24 +0000738 {
739 }
740
741 ~CommandObjectProcessDetach ()
742 {
743 }
744
Jim Inghamda26bd22012-06-08 21:56:10 +0000745protected:
Chris Lattner24943d22010-06-08 16:52:24 +0000746 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000747 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000748 CommandReturnObject &result)
749 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000750 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000751 if (process == NULL)
752 {
753 result.AppendError ("must have a valid process in order to detach");
754 result.SetStatus (eReturnStatusFailed);
755 return false;
756 }
757
Greg Clayton444e35b2011-10-19 18:09:39 +0000758 result.AppendMessageWithFormat ("Detaching from process %llu\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000759 Error error (process->Detach());
760 if (error.Success())
761 {
762 result.SetStatus (eReturnStatusSuccessFinishResult);
763 }
764 else
765 {
766 result.AppendErrorWithFormat ("Detach failed: %s\n", error.AsCString());
767 result.SetStatus (eReturnStatusFailed);
768 return false;
769 }
770 return result.Succeeded();
771 }
772};
773
774//-------------------------------------------------------------------------
Greg Claytone71e2582011-02-04 01:58:07 +0000775// CommandObjectProcessConnect
776//-------------------------------------------------------------------------
777#pragma mark CommandObjectProcessConnect
778
Jim Inghamda26bd22012-06-08 21:56:10 +0000779class CommandObjectProcessConnect : public CommandObjectParsed
Greg Claytone71e2582011-02-04 01:58:07 +0000780{
781public:
782
783 class CommandOptions : public Options
784 {
785 public:
786
Greg Claytonf15996e2011-04-07 22:46:35 +0000787 CommandOptions (CommandInterpreter &interpreter) :
788 Options(interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000789 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000790 // Keep default values of all options in one place: OptionParsingStarting ()
791 OptionParsingStarting ();
Greg Claytone71e2582011-02-04 01:58:07 +0000792 }
793
794 ~CommandOptions ()
795 {
796 }
797
798 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000799 SetOptionValue (uint32_t option_idx, const char *option_arg)
Greg Claytone71e2582011-02-04 01:58:07 +0000800 {
801 Error error;
802 char short_option = (char) m_getopt_table[option_idx].val;
803
804 switch (short_option)
805 {
806 case 'p':
807 plugin_name.assign (option_arg);
808 break;
809
810 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000811 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Greg Claytone71e2582011-02-04 01:58:07 +0000812 break;
813 }
814 return error;
815 }
816
817 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000818 OptionParsingStarting ()
Greg Claytone71e2582011-02-04 01:58:07 +0000819 {
Greg Claytone71e2582011-02-04 01:58:07 +0000820 plugin_name.clear();
821 }
822
Greg Claytonb3448432011-03-24 21:19:54 +0000823 const OptionDefinition*
Greg Claytone71e2582011-02-04 01:58:07 +0000824 GetDefinitions ()
825 {
826 return g_option_table;
827 }
828
829 // Options table: Required for subclasses of Options.
830
Greg Claytonb3448432011-03-24 21:19:54 +0000831 static OptionDefinition g_option_table[];
Greg Claytone71e2582011-02-04 01:58:07 +0000832
833 // Instance variables to hold the values for command options.
834
835 std::string plugin_name;
836 };
837
838 CommandObjectProcessConnect (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000839 CommandObjectParsed (interpreter,
840 "process connect",
841 "Connect to a remote debug service.",
842 "process connect <remote-url>",
843 0),
Greg Claytonf15996e2011-04-07 22:46:35 +0000844 m_options (interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000845 {
846 }
847
848 ~CommandObjectProcessConnect ()
849 {
850 }
851
852
Jim Inghamda26bd22012-06-08 21:56:10 +0000853 Options *
854 GetOptions ()
855 {
856 return &m_options;
857 }
858
859protected:
Greg Claytone71e2582011-02-04 01:58:07 +0000860 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000861 DoExecute (Args& command,
Greg Claytone71e2582011-02-04 01:58:07 +0000862 CommandReturnObject &result)
863 {
864
865 TargetSP target_sp (m_interpreter.GetDebugger().GetSelectedTarget());
866 Error error;
Greg Clayton567e7f32011-09-22 04:58:26 +0000867 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytone71e2582011-02-04 01:58:07 +0000868 if (process)
869 {
870 if (process->IsAlive())
871 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000872 result.AppendErrorWithFormat ("Process %llu is currently being debugged, kill the process before connecting.\n",
Greg Claytone71e2582011-02-04 01:58:07 +0000873 process->GetID());
874 result.SetStatus (eReturnStatusFailed);
875 return false;
876 }
877 }
878
879 if (!target_sp)
880 {
881 // If there isn't a current target create one.
882 FileSpec emptyFileSpec;
Greg Claytone71e2582011-02-04 01:58:07 +0000883
884 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
885 emptyFileSpec,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000886 NULL,
Greg Claytone71e2582011-02-04 01:58:07 +0000887 false,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000888 NULL, // No platform options
Greg Claytone71e2582011-02-04 01:58:07 +0000889 target_sp);
890 if (!target_sp || error.Fail())
891 {
892 result.AppendError(error.AsCString("Error creating target"));
893 result.SetStatus (eReturnStatusFailed);
894 return false;
895 }
896 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target_sp.get());
897 }
898
899 if (command.GetArgumentCount() == 1)
900 {
901 const char *plugin_name = NULL;
902 if (!m_options.plugin_name.empty())
903 plugin_name = m_options.plugin_name.c_str();
904
905 const char *remote_url = command.GetArgumentAtIndex(0);
Greg Clayton46c9a352012-02-09 06:16:32 +0000906 process = target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name, NULL).get();
Greg Claytone71e2582011-02-04 01:58:07 +0000907
908 if (process)
909 {
910 error = process->ConnectRemote (remote_url);
911
912 if (error.Fail())
913 {
914 result.AppendError(error.AsCString("Remote connect failed"));
915 result.SetStatus (eReturnStatusFailed);
Greg Clayton0cbb93b2012-03-31 00:10:30 +0000916 target_sp->DeleteCurrentProcess();
Greg Claytone71e2582011-02-04 01:58:07 +0000917 return false;
918 }
919 }
920 else
921 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000922 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",
923 m_cmd_name.c_str());
Greg Claytone71e2582011-02-04 01:58:07 +0000924 result.SetStatus (eReturnStatusFailed);
925 }
926 }
927 else
928 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000929 result.AppendErrorWithFormat ("'%s' takes exactly one argument:\nUsage: %s\n",
Greg Claytone71e2582011-02-04 01:58:07 +0000930 m_cmd_name.c_str(),
931 m_cmd_syntax.c_str());
932 result.SetStatus (eReturnStatusFailed);
933 }
934 return result.Succeeded();
935 }
Greg Claytone71e2582011-02-04 01:58:07 +0000936
937 CommandOptions m_options;
938};
939
940
Greg Claytonb3448432011-03-24 21:19:54 +0000941OptionDefinition
Greg Claytone71e2582011-02-04 01:58:07 +0000942CommandObjectProcessConnect::CommandOptions::g_option_table[] =
943{
944 { LLDB_OPT_SET_ALL, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
945 { 0, false, NULL, 0 , 0, NULL, 0, eArgTypeNone, NULL }
946};
947
948//-------------------------------------------------------------------------
Greg Clayton0baa3942010-11-04 01:54:29 +0000949// CommandObjectProcessLoad
950//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000951#pragma mark CommandObjectProcessLoad
Greg Clayton0baa3942010-11-04 01:54:29 +0000952
Jim Inghamda26bd22012-06-08 21:56:10 +0000953class CommandObjectProcessLoad : public CommandObjectParsed
Greg Clayton0baa3942010-11-04 01:54:29 +0000954{
955public:
956
957 CommandObjectProcessLoad (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000958 CommandObjectParsed (interpreter,
959 "process load",
960 "Load a shared library into the current process.",
961 "process load <filename> [<filename> ...]",
962 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
Greg Clayton0baa3942010-11-04 01:54:29 +0000963 {
964 }
965
966 ~CommandObjectProcessLoad ()
967 {
968 }
969
Jim Inghamda26bd22012-06-08 21:56:10 +0000970protected:
Greg Clayton0baa3942010-11-04 01:54:29 +0000971 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000972 DoExecute (Args& command,
Greg Clayton0baa3942010-11-04 01:54:29 +0000973 CommandReturnObject &result)
974 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000975 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton0baa3942010-11-04 01:54:29 +0000976 if (process == NULL)
977 {
978 result.AppendError ("must have a valid process in order to load a shared library");
979 result.SetStatus (eReturnStatusFailed);
980 return false;
981 }
982
983 const uint32_t argc = command.GetArgumentCount();
984
985 for (uint32_t i=0; i<argc; ++i)
986 {
987 Error error;
988 const char *image_path = command.GetArgumentAtIndex(i);
989 FileSpec image_spec (image_path, false);
Greg Claytonf2bf8702011-08-11 16:25:18 +0000990 process->GetTarget().GetPlatform()->ResolveRemotePath(image_spec, image_spec);
Greg Clayton0baa3942010-11-04 01:54:29 +0000991 uint32_t image_token = process->LoadImage(image_spec, error);
992 if (image_token != LLDB_INVALID_IMAGE_TOKEN)
993 {
994 result.AppendMessageWithFormat ("Loading \"%s\"...ok\nImage %u loaded.\n", image_path, image_token);
995 result.SetStatus (eReturnStatusSuccessFinishResult);
996 }
997 else
998 {
999 result.AppendErrorWithFormat ("failed to load '%s': %s", image_path, error.AsCString());
1000 result.SetStatus (eReturnStatusFailed);
1001 }
1002 }
1003 return result.Succeeded();
1004 }
1005};
1006
1007
1008//-------------------------------------------------------------------------
1009// CommandObjectProcessUnload
1010//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001011#pragma mark CommandObjectProcessUnload
Greg Clayton0baa3942010-11-04 01:54:29 +00001012
Jim Inghamda26bd22012-06-08 21:56:10 +00001013class CommandObjectProcessUnload : public CommandObjectParsed
Greg Clayton0baa3942010-11-04 01:54:29 +00001014{
1015public:
1016
1017 CommandObjectProcessUnload (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001018 CommandObjectParsed (interpreter,
1019 "process unload",
1020 "Unload a shared library from the current process using the index returned by a previous call to \"process load\".",
1021 "process unload <index>",
1022 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
Greg Clayton0baa3942010-11-04 01:54:29 +00001023 {
1024 }
1025
1026 ~CommandObjectProcessUnload ()
1027 {
1028 }
1029
Jim Inghamda26bd22012-06-08 21:56:10 +00001030protected:
Greg Clayton0baa3942010-11-04 01:54:29 +00001031 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001032 DoExecute (Args& command,
Greg Clayton0baa3942010-11-04 01:54:29 +00001033 CommandReturnObject &result)
1034 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001035 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton0baa3942010-11-04 01:54:29 +00001036 if (process == NULL)
1037 {
1038 result.AppendError ("must have a valid process in order to load a shared library");
1039 result.SetStatus (eReturnStatusFailed);
1040 return false;
1041 }
1042
1043 const uint32_t argc = command.GetArgumentCount();
1044
1045 for (uint32_t i=0; i<argc; ++i)
1046 {
1047 const char *image_token_cstr = command.GetArgumentAtIndex(i);
1048 uint32_t image_token = Args::StringToUInt32(image_token_cstr, LLDB_INVALID_IMAGE_TOKEN, 0);
1049 if (image_token == LLDB_INVALID_IMAGE_TOKEN)
1050 {
1051 result.AppendErrorWithFormat ("invalid image index argument '%s'", image_token_cstr);
1052 result.SetStatus (eReturnStatusFailed);
1053 break;
1054 }
1055 else
1056 {
1057 Error error (process->UnloadImage(image_token));
1058 if (error.Success())
1059 {
1060 result.AppendMessageWithFormat ("Unloading shared library with index %u...ok\n", image_token);
1061 result.SetStatus (eReturnStatusSuccessFinishResult);
1062 }
1063 else
1064 {
1065 result.AppendErrorWithFormat ("failed to unload image: %s", error.AsCString());
1066 result.SetStatus (eReturnStatusFailed);
1067 break;
1068 }
1069 }
1070 }
1071 return result.Succeeded();
1072 }
1073};
1074
1075//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001076// CommandObjectProcessSignal
1077//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001078#pragma mark CommandObjectProcessSignal
Chris Lattner24943d22010-06-08 16:52:24 +00001079
Jim Inghamda26bd22012-06-08 21:56:10 +00001080class CommandObjectProcessSignal : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001081{
1082public:
1083
Greg Clayton238c0a12010-09-18 01:14:36 +00001084 CommandObjectProcessSignal (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001085 CommandObjectParsed (interpreter,
1086 "process signal",
1087 "Send a UNIX signal to the current process being debugged.",
1088 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001089 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001090 CommandArgumentEntry arg;
1091 CommandArgumentData signal_arg;
1092
1093 // Define the first (and only) variant of this arg.
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001094 signal_arg.arg_type = eArgTypeUnixSignal;
Caroline Tice43b014a2010-10-04 22:28:36 +00001095 signal_arg.arg_repetition = eArgRepeatPlain;
1096
1097 // There is only one variant this argument could be; put it into the argument entry.
1098 arg.push_back (signal_arg);
1099
1100 // Push the data for the first argument into the m_arguments vector.
1101 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001102 }
1103
1104 ~CommandObjectProcessSignal ()
1105 {
1106 }
1107
Jim Inghamda26bd22012-06-08 21:56:10 +00001108protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001109 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001110 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001111 CommandReturnObject &result)
1112 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001113 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001114 if (process == NULL)
1115 {
1116 result.AppendError ("no process to signal");
1117 result.SetStatus (eReturnStatusFailed);
1118 return false;
1119 }
1120
1121 if (command.GetArgumentCount() == 1)
1122 {
Greg Clayton8f6be2a2010-10-09 01:40:57 +00001123 int signo = LLDB_INVALID_SIGNAL_NUMBER;
1124
1125 const char *signal_name = command.GetArgumentAtIndex(0);
1126 if (::isxdigit (signal_name[0]))
1127 signo = Args::StringToSInt32(signal_name, LLDB_INVALID_SIGNAL_NUMBER, 0);
1128 else
1129 signo = process->GetUnixSignals().GetSignalNumberFromName (signal_name);
1130
1131 if (signo == LLDB_INVALID_SIGNAL_NUMBER)
Chris Lattner24943d22010-06-08 16:52:24 +00001132 {
1133 result.AppendErrorWithFormat ("Invalid signal argument '%s'.\n", command.GetArgumentAtIndex(0));
1134 result.SetStatus (eReturnStatusFailed);
1135 }
1136 else
1137 {
1138 Error error (process->Signal (signo));
1139 if (error.Success())
1140 {
1141 result.SetStatus (eReturnStatusSuccessFinishResult);
1142 }
1143 else
1144 {
1145 result.AppendErrorWithFormat ("Failed to send signal %i: %s\n", signo, error.AsCString());
1146 result.SetStatus (eReturnStatusFailed);
1147 }
1148 }
1149 }
1150 else
1151 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001152 result.AppendErrorWithFormat("'%s' takes exactly one signal number argument:\nUsage: %s\n", m_cmd_name.c_str(),
Chris Lattner24943d22010-06-08 16:52:24 +00001153 m_cmd_syntax.c_str());
1154 result.SetStatus (eReturnStatusFailed);
1155 }
1156 return result.Succeeded();
1157 }
1158};
1159
1160
1161//-------------------------------------------------------------------------
1162// CommandObjectProcessInterrupt
1163//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001164#pragma mark CommandObjectProcessInterrupt
Chris Lattner24943d22010-06-08 16:52:24 +00001165
Jim Inghamda26bd22012-06-08 21:56:10 +00001166class CommandObjectProcessInterrupt : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001167{
1168public:
1169
1170
Greg Clayton238c0a12010-09-18 01:14:36 +00001171 CommandObjectProcessInterrupt (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001172 CommandObjectParsed (interpreter,
1173 "process interrupt",
1174 "Interrupt the current process being debugged.",
1175 "process interrupt",
1176 eFlagProcessMustBeLaunched)
Chris Lattner24943d22010-06-08 16:52:24 +00001177 {
1178 }
1179
1180 ~CommandObjectProcessInterrupt ()
1181 {
1182 }
1183
Jim Inghamda26bd22012-06-08 21:56:10 +00001184protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001185 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001186 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001187 CommandReturnObject &result)
1188 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001189 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001190 if (process == NULL)
1191 {
1192 result.AppendError ("no process to halt");
1193 result.SetStatus (eReturnStatusFailed);
1194 return false;
1195 }
1196
1197 if (command.GetArgumentCount() == 0)
1198 {
1199 Error error(process->Halt ());
1200 if (error.Success())
1201 {
1202 result.SetStatus (eReturnStatusSuccessFinishResult);
1203
1204 // Maybe we should add a "SuspendThreadPlans so we
1205 // can halt, and keep in place all the current thread plans.
1206 process->GetThreadList().DiscardThreadPlans();
1207 }
1208 else
1209 {
1210 result.AppendErrorWithFormat ("Failed to halt process: %s\n", error.AsCString());
1211 result.SetStatus (eReturnStatusFailed);
1212 }
1213 }
1214 else
1215 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001216 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner24943d22010-06-08 16:52:24 +00001217 m_cmd_name.c_str(),
1218 m_cmd_syntax.c_str());
1219 result.SetStatus (eReturnStatusFailed);
1220 }
1221 return result.Succeeded();
1222 }
1223};
1224
1225//-------------------------------------------------------------------------
1226// CommandObjectProcessKill
1227//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001228#pragma mark CommandObjectProcessKill
Chris Lattner24943d22010-06-08 16:52:24 +00001229
Jim Inghamda26bd22012-06-08 21:56:10 +00001230class CommandObjectProcessKill : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001231{
1232public:
1233
Greg Clayton238c0a12010-09-18 01:14:36 +00001234 CommandObjectProcessKill (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001235 CommandObjectParsed (interpreter,
1236 "process kill",
1237 "Terminate the current process being debugged.",
1238 "process kill",
1239 eFlagProcessMustBeLaunched)
Chris Lattner24943d22010-06-08 16:52:24 +00001240 {
1241 }
1242
1243 ~CommandObjectProcessKill ()
1244 {
1245 }
1246
Jim Inghamda26bd22012-06-08 21:56:10 +00001247protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001248 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001249 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001250 CommandReturnObject &result)
1251 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001252 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001253 if (process == NULL)
1254 {
1255 result.AppendError ("no process to kill");
1256 result.SetStatus (eReturnStatusFailed);
1257 return false;
1258 }
1259
1260 if (command.GetArgumentCount() == 0)
1261 {
1262 Error error (process->Destroy());
1263 if (error.Success())
1264 {
1265 result.SetStatus (eReturnStatusSuccessFinishResult);
1266 }
1267 else
1268 {
1269 result.AppendErrorWithFormat ("Failed to kill process: %s\n", error.AsCString());
1270 result.SetStatus (eReturnStatusFailed);
1271 }
1272 }
1273 else
1274 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001275 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner24943d22010-06-08 16:52:24 +00001276 m_cmd_name.c_str(),
1277 m_cmd_syntax.c_str());
1278 result.SetStatus (eReturnStatusFailed);
1279 }
1280 return result.Succeeded();
1281 }
1282};
1283
1284//-------------------------------------------------------------------------
Jim Ingham41313fc2010-06-18 01:23:09 +00001285// CommandObjectProcessStatus
1286//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001287#pragma mark CommandObjectProcessStatus
1288
Jim Inghamda26bd22012-06-08 21:56:10 +00001289class CommandObjectProcessStatus : public CommandObjectParsed
Jim Ingham41313fc2010-06-18 01:23:09 +00001290{
1291public:
Greg Clayton238c0a12010-09-18 01:14:36 +00001292 CommandObjectProcessStatus (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001293 CommandObjectParsed (interpreter,
1294 "process status",
1295 "Show the current status and location of executing process.",
1296 "process status",
1297 0)
Jim Ingham41313fc2010-06-18 01:23:09 +00001298 {
1299 }
1300
1301 ~CommandObjectProcessStatus()
1302 {
1303 }
1304
1305
1306 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001307 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham41313fc2010-06-18 01:23:09 +00001308 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001309 Stream &strm = result.GetOutputStream();
Jim Ingham41313fc2010-06-18 01:23:09 +00001310 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Claytonb72d0f02011-04-12 05:54:46 +00001311 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
Greg Clayton567e7f32011-09-22 04:58:26 +00001312 Process *process = exe_ctx.GetProcessPtr();
1313 if (process)
Jim Ingham41313fc2010-06-18 01:23:09 +00001314 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001315 const bool only_threads_with_stop_reason = true;
1316 const uint32_t start_frame = 0;
1317 const uint32_t num_frames = 1;
1318 const uint32_t num_frames_with_source = 1;
Greg Clayton567e7f32011-09-22 04:58:26 +00001319 process->GetStatus(strm);
1320 process->GetThreadStatus (strm,
1321 only_threads_with_stop_reason,
1322 start_frame,
1323 num_frames,
1324 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001325
Jim Ingham41313fc2010-06-18 01:23:09 +00001326 }
1327 else
1328 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001329 result.AppendError ("No process.");
Jim Ingham41313fc2010-06-18 01:23:09 +00001330 result.SetStatus (eReturnStatusFailed);
1331 }
1332 return result.Succeeded();
1333 }
1334};
1335
1336//-------------------------------------------------------------------------
Caroline Tice23d6f272010-10-13 20:44:39 +00001337// CommandObjectProcessHandle
1338//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001339#pragma mark CommandObjectProcessHandle
Caroline Tice23d6f272010-10-13 20:44:39 +00001340
Jim Inghamda26bd22012-06-08 21:56:10 +00001341class CommandObjectProcessHandle : public CommandObjectParsed
Caroline Tice23d6f272010-10-13 20:44:39 +00001342{
1343public:
1344
1345 class CommandOptions : public Options
1346 {
1347 public:
1348
Greg Claytonf15996e2011-04-07 22:46:35 +00001349 CommandOptions (CommandInterpreter &interpreter) :
1350 Options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001351 {
Greg Clayton143fcc32011-04-13 00:18:08 +00001352 OptionParsingStarting ();
Caroline Tice23d6f272010-10-13 20:44:39 +00001353 }
1354
1355 ~CommandOptions ()
1356 {
1357 }
1358
1359 Error
Greg Clayton143fcc32011-04-13 00:18:08 +00001360 SetOptionValue (uint32_t option_idx, const char *option_arg)
Caroline Tice23d6f272010-10-13 20:44:39 +00001361 {
1362 Error error;
1363 char short_option = (char) m_getopt_table[option_idx].val;
1364
1365 switch (short_option)
1366 {
1367 case 's':
1368 stop = option_arg;
1369 break;
1370 case 'n':
1371 notify = option_arg;
1372 break;
1373 case 'p':
1374 pass = option_arg;
1375 break;
1376 default:
Greg Clayton9c236732011-10-26 00:56:27 +00001377 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Caroline Tice23d6f272010-10-13 20:44:39 +00001378 break;
1379 }
1380 return error;
1381 }
1382
1383 void
Greg Clayton143fcc32011-04-13 00:18:08 +00001384 OptionParsingStarting ()
Caroline Tice23d6f272010-10-13 20:44:39 +00001385 {
Caroline Tice23d6f272010-10-13 20:44:39 +00001386 stop.clear();
1387 notify.clear();
1388 pass.clear();
1389 }
1390
Greg Claytonb3448432011-03-24 21:19:54 +00001391 const OptionDefinition*
Caroline Tice23d6f272010-10-13 20:44:39 +00001392 GetDefinitions ()
1393 {
1394 return g_option_table;
1395 }
1396
1397 // Options table: Required for subclasses of Options.
1398
Greg Claytonb3448432011-03-24 21:19:54 +00001399 static OptionDefinition g_option_table[];
Caroline Tice23d6f272010-10-13 20:44:39 +00001400
1401 // Instance variables to hold the values for command options.
1402
1403 std::string stop;
1404 std::string notify;
1405 std::string pass;
1406 };
1407
1408
1409 CommandObjectProcessHandle (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001410 CommandObjectParsed (interpreter,
1411 "process handle",
1412 "Show or update what the process and debugger should do with various signals received from the OS.",
1413 NULL),
Greg Claytonf15996e2011-04-07 22:46:35 +00001414 m_options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001415 {
Caroline Ticee7471982010-10-14 21:31:13 +00001416 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 +00001417 CommandArgumentEntry arg;
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001418 CommandArgumentData signal_arg;
Caroline Tice23d6f272010-10-13 20:44:39 +00001419
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001420 signal_arg.arg_type = eArgTypeUnixSignal;
1421 signal_arg.arg_repetition = eArgRepeatStar;
Caroline Tice23d6f272010-10-13 20:44:39 +00001422
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001423 arg.push_back (signal_arg);
Caroline Tice23d6f272010-10-13 20:44:39 +00001424
1425 m_arguments.push_back (arg);
1426 }
1427
1428 ~CommandObjectProcessHandle ()
1429 {
1430 }
1431
1432 Options *
1433 GetOptions ()
1434 {
1435 return &m_options;
1436 }
1437
1438 bool
Caroline Ticee7471982010-10-14 21:31:13 +00001439 VerifyCommandOptionValue (const std::string &option, int &real_value)
Caroline Tice23d6f272010-10-13 20:44:39 +00001440 {
1441 bool okay = true;
1442
Caroline Ticee7471982010-10-14 21:31:13 +00001443 bool success = false;
1444 bool tmp_value = Args::StringToBoolean (option.c_str(), false, &success);
1445
1446 if (success && tmp_value)
1447 real_value = 1;
1448 else if (success && !tmp_value)
1449 real_value = 0;
Caroline Tice23d6f272010-10-13 20:44:39 +00001450 else
1451 {
1452 // If the value isn't 'true' or 'false', it had better be 0 or 1.
Caroline Ticee7471982010-10-14 21:31:13 +00001453 real_value = Args::StringToUInt32 (option.c_str(), 3);
1454 if (real_value != 0 && real_value != 1)
Caroline Tice23d6f272010-10-13 20:44:39 +00001455 okay = false;
1456 }
1457
1458 return okay;
1459 }
1460
Caroline Ticee7471982010-10-14 21:31:13 +00001461 void
1462 PrintSignalHeader (Stream &str)
1463 {
1464 str.Printf ("NAME PASS STOP NOTIFY\n");
1465 str.Printf ("========== ===== ===== ======\n");
1466 }
1467
1468 void
1469 PrintSignal (Stream &str, int32_t signo, const char *sig_name, UnixSignals &signals)
1470 {
1471 bool stop;
1472 bool suppress;
1473 bool notify;
1474
1475 str.Printf ("%-10s ", sig_name);
1476 if (signals.GetSignalInfo (signo, suppress, stop, notify))
1477 {
1478 bool pass = !suppress;
1479 str.Printf ("%s %s %s",
1480 (pass ? "true " : "false"),
1481 (stop ? "true " : "false"),
1482 (notify ? "true " : "false"));
1483 }
1484 str.Printf ("\n");
1485 }
1486
1487 void
1488 PrintSignalInformation (Stream &str, Args &signal_args, int num_valid_signals, UnixSignals &signals)
1489 {
1490 PrintSignalHeader (str);
1491
1492 if (num_valid_signals > 0)
1493 {
1494 size_t num_args = signal_args.GetArgumentCount();
1495 for (size_t i = 0; i < num_args; ++i)
1496 {
1497 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1498 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
1499 PrintSignal (str, signo, signal_args.GetArgumentAtIndex (i), signals);
1500 }
1501 }
1502 else // Print info for ALL signals
1503 {
1504 int32_t signo = signals.GetFirstSignalNumber();
1505 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1506 {
1507 PrintSignal (str, signo, signals.GetSignalAsCString (signo), signals);
1508 signo = signals.GetNextSignalNumber (signo);
1509 }
1510 }
1511 }
1512
Jim Inghamda26bd22012-06-08 21:56:10 +00001513protected:
Caroline Tice23d6f272010-10-13 20:44:39 +00001514 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001515 DoExecute (Args &signal_args, CommandReturnObject &result)
Caroline Tice23d6f272010-10-13 20:44:39 +00001516 {
1517 TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
1518
1519 if (!target_sp)
1520 {
1521 result.AppendError ("No current target;"
1522 " cannot handle signals until you have a valid target and process.\n");
1523 result.SetStatus (eReturnStatusFailed);
1524 return false;
1525 }
1526
1527 ProcessSP process_sp = target_sp->GetProcessSP();
1528
1529 if (!process_sp)
1530 {
1531 result.AppendError ("No current process; cannot handle signals until you have a valid process.\n");
1532 result.SetStatus (eReturnStatusFailed);
1533 return false;
1534 }
1535
Caroline Tice23d6f272010-10-13 20:44:39 +00001536 int stop_action = -1; // -1 means leave the current setting alone
Caroline Ticee7471982010-10-14 21:31:13 +00001537 int pass_action = -1; // -1 means leave the current setting alone
Caroline Tice23d6f272010-10-13 20:44:39 +00001538 int notify_action = -1; // -1 means leave the current setting alone
1539
1540 if (! m_options.stop.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001541 && ! VerifyCommandOptionValue (m_options.stop, stop_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001542 {
1543 result.AppendError ("Invalid argument for command option --stop; must be true or false.\n");
1544 result.SetStatus (eReturnStatusFailed);
1545 return false;
1546 }
1547
1548 if (! m_options.notify.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001549 && ! VerifyCommandOptionValue (m_options.notify, notify_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001550 {
1551 result.AppendError ("Invalid argument for command option --notify; must be true or false.\n");
1552 result.SetStatus (eReturnStatusFailed);
1553 return false;
1554 }
1555
1556 if (! m_options.pass.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001557 && ! VerifyCommandOptionValue (m_options.pass, pass_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001558 {
1559 result.AppendError ("Invalid argument for command option --pass; must be true or false.\n");
1560 result.SetStatus (eReturnStatusFailed);
1561 return false;
1562 }
1563
1564 size_t num_args = signal_args.GetArgumentCount();
1565 UnixSignals &signals = process_sp->GetUnixSignals();
1566 int num_signals_set = 0;
1567
Caroline Ticee7471982010-10-14 21:31:13 +00001568 if (num_args > 0)
Caroline Tice23d6f272010-10-13 20:44:39 +00001569 {
Caroline Ticee7471982010-10-14 21:31:13 +00001570 for (size_t i = 0; i < num_args; ++i)
Caroline Tice23d6f272010-10-13 20:44:39 +00001571 {
Caroline Ticee7471982010-10-14 21:31:13 +00001572 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1573 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
Caroline Tice23d6f272010-10-13 20:44:39 +00001574 {
Caroline Ticee7471982010-10-14 21:31:13 +00001575 // Casting the actions as bools here should be okay, because VerifyCommandOptionValue guarantees
1576 // the value is either 0 or 1.
1577 if (stop_action != -1)
1578 signals.SetShouldStop (signo, (bool) stop_action);
1579 if (pass_action != -1)
1580 {
1581 bool suppress = ! ((bool) pass_action);
1582 signals.SetShouldSuppress (signo, suppress);
1583 }
1584 if (notify_action != -1)
1585 signals.SetShouldNotify (signo, (bool) notify_action);
1586 ++num_signals_set;
Caroline Tice23d6f272010-10-13 20:44:39 +00001587 }
Caroline Ticee7471982010-10-14 21:31:13 +00001588 else
1589 {
1590 result.AppendErrorWithFormat ("Invalid signal name '%s'\n", signal_args.GetArgumentAtIndex (i));
1591 }
Caroline Tice23d6f272010-10-13 20:44:39 +00001592 }
1593 }
Caroline Ticee7471982010-10-14 21:31:13 +00001594 else
1595 {
1596 // No signal specified, if any command options were specified, update ALL signals.
1597 if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1))
1598 {
1599 if (m_interpreter.Confirm ("Do you really want to update all the signals?", false))
1600 {
1601 int32_t signo = signals.GetFirstSignalNumber();
1602 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1603 {
1604 if (notify_action != -1)
1605 signals.SetShouldNotify (signo, (bool) notify_action);
1606 if (stop_action != -1)
1607 signals.SetShouldStop (signo, (bool) stop_action);
1608 if (pass_action != -1)
1609 {
1610 bool suppress = ! ((bool) pass_action);
1611 signals.SetShouldSuppress (signo, suppress);
1612 }
1613 signo = signals.GetNextSignalNumber (signo);
1614 }
1615 }
1616 }
1617 }
1618
1619 PrintSignalInformation (result.GetOutputStream(), signal_args, num_signals_set, signals);
Caroline Tice23d6f272010-10-13 20:44:39 +00001620
1621 if (num_signals_set > 0)
1622 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1623 else
1624 result.SetStatus (eReturnStatusFailed);
1625
1626 return result.Succeeded();
1627 }
1628
Caroline Tice23d6f272010-10-13 20:44:39 +00001629 CommandOptions m_options;
1630};
1631
Greg Claytonb3448432011-03-24 21:19:54 +00001632OptionDefinition
Caroline Tice23d6f272010-10-13 20:44:39 +00001633CommandObjectProcessHandle::CommandOptions::g_option_table[] =
1634{
1635{ 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." },
1636{ 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." },
1637{ LLDB_OPT_SET_1, false, "pass", 'p', required_argument, NULL, 0, eArgTypeBoolean, "Whether or not the signal should be passed to the process." },
1638{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1639};
1640
1641//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001642// CommandObjectMultiwordProcess
1643//-------------------------------------------------------------------------
1644
Greg Clayton63094e02010-06-23 01:19:29 +00001645CommandObjectMultiwordProcess::CommandObjectMultiwordProcess (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00001646 CommandObjectMultiword (interpreter,
1647 "process",
1648 "A set of commands for operating on a process.",
1649 "process <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00001650{
Greg Claytona9eb8272011-07-02 21:07:54 +00001651 LoadSubCommand ("attach", CommandObjectSP (new CommandObjectProcessAttach (interpreter)));
1652 LoadSubCommand ("launch", CommandObjectSP (new CommandObjectProcessLaunch (interpreter)));
1653 LoadSubCommand ("continue", CommandObjectSP (new CommandObjectProcessContinue (interpreter)));
1654 LoadSubCommand ("connect", CommandObjectSP (new CommandObjectProcessConnect (interpreter)));
1655 LoadSubCommand ("detach", CommandObjectSP (new CommandObjectProcessDetach (interpreter)));
1656 LoadSubCommand ("load", CommandObjectSP (new CommandObjectProcessLoad (interpreter)));
1657 LoadSubCommand ("unload", CommandObjectSP (new CommandObjectProcessUnload (interpreter)));
1658 LoadSubCommand ("signal", CommandObjectSP (new CommandObjectProcessSignal (interpreter)));
1659 LoadSubCommand ("handle", CommandObjectSP (new CommandObjectProcessHandle (interpreter)));
1660 LoadSubCommand ("status", CommandObjectSP (new CommandObjectProcessStatus (interpreter)));
Greg Clayton238c0a12010-09-18 01:14:36 +00001661 LoadSubCommand ("interrupt", CommandObjectSP (new CommandObjectProcessInterrupt (interpreter)));
Greg Claytona9eb8272011-07-02 21:07:54 +00001662 LoadSubCommand ("kill", CommandObjectSP (new CommandObjectProcessKill (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00001663}
1664
1665CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess ()
1666{
1667}
1668