blob: 97ed205a1b00bc09d4eaf98c609c02387f4fe067 [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?)");
Jim Ingham5d90ade2012-07-27 23:57:19 +0000569 process->Destroy();
Johnny Chen9986a3b2012-05-18 00:51:36 +0000570 result.SetStatus (eReturnStatusFailed);
571 return false;
572 }
Jim Ingham7508e732010-08-09 23:31:02 +0000573 }
Jim Ingham7508e732010-08-09 23:31:02 +0000574 }
575 }
576
577 if (result.Succeeded())
578 {
579 // Okay, we're done. Last step is to warn if the executable module has changed:
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000580 char new_path[PATH_MAX];
Greg Clayton5beb99d2011-08-11 02:48:45 +0000581 ModuleSP new_exec_module_sp (target->GetExecutableModule());
Jim Ingham7508e732010-08-09 23:31:02 +0000582 if (!old_exec_module_sp)
583 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000584 // We might not have a module if we attached to a raw pid...
Greg Clayton5beb99d2011-08-11 02:48:45 +0000585 if (new_exec_module_sp)
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000586 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000587 new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000588 result.AppendMessageWithFormat("Executable module set to \"%s\".\n", new_path);
589 }
Jim Ingham7508e732010-08-09 23:31:02 +0000590 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000591 else if (old_exec_module_sp->GetFileSpec() != new_exec_module_sp->GetFileSpec())
Jim Ingham7508e732010-08-09 23:31:02 +0000592 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000593 char old_path[PATH_MAX];
Jim Ingham7508e732010-08-09 23:31:02 +0000594
Greg Clayton5beb99d2011-08-11 02:48:45 +0000595 old_exec_module_sp->GetFileSpec().GetPath (old_path, PATH_MAX);
596 new_exec_module_sp->GetFileSpec().GetPath (new_path, PATH_MAX);
Jim Ingham7508e732010-08-09 23:31:02 +0000597
598 result.AppendWarningWithFormat("Executable module changed from \"%s\" to \"%s\".\n",
599 old_path, new_path);
600 }
601
602 if (!old_arch_spec.IsValid())
603 {
Greg Clayton940b1032011-02-23 00:35:02 +0000604 result.AppendMessageWithFormat ("Architecture set to: %s.\n", target->GetArchitecture().GetArchitectureName());
Jim Ingham7508e732010-08-09 23:31:02 +0000605 }
606 else if (old_arch_spec != target->GetArchitecture())
607 {
608 result.AppendWarningWithFormat("Architecture changed from %s to %s.\n",
Greg Clayton940b1032011-02-23 00:35:02 +0000609 old_arch_spec.GetArchitectureName(), target->GetArchitecture().GetArchitectureName());
Jim Ingham7508e732010-08-09 23:31:02 +0000610 }
Johnny Chen7c099972012-05-24 00:43:00 +0000611
612 // This supports the use-case scenario of immediately continuing the process once attached.
613 if (m_options.attach_info.GetContinueOnceAttached())
Sean Callanan4336d932012-05-31 01:30:08 +0000614 m_interpreter.HandleCommand("process continue", eLazyBoolNo, result);
Jim Ingham7508e732010-08-09 23:31:02 +0000615 }
616 return result.Succeeded();
617 }
618
Chris Lattner24943d22010-06-08 16:52:24 +0000619 CommandOptions m_options;
620};
621
622
Greg Claytonb3448432011-03-24 21:19:54 +0000623OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000624CommandObjectProcessAttach::CommandOptions::g_option_table[] =
625{
Jim Ingham3a458eb2012-07-20 21:37:13 +0000626{ LLDB_OPT_SET_ALL, false, "continue",'c', no_argument, NULL, 0, eArgTypeNone, "Immediately continue the process once attached."},
627{ LLDB_OPT_SET_ALL, false, "plugin", 'P', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
628{ LLDB_OPT_SET_1, false, "pid", 'p', required_argument, NULL, 0, eArgTypePid, "The process ID of an existing process to attach to."},
629{ LLDB_OPT_SET_2, false, "name", 'n', required_argument, NULL, 0, eArgTypeProcessName, "The name of the process to attach to."},
630{ LLDB_OPT_SET_2, false, "include-existing", 'i', no_argument, NULL, 0, eArgTypeNone, "Include existing processes when doing attach -w."},
631{ 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 +0000632{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000633};
634
635//-------------------------------------------------------------------------
636// CommandObjectProcessContinue
637//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000638#pragma mark CommandObjectProcessContinue
Chris Lattner24943d22010-06-08 16:52:24 +0000639
Jim Inghamda26bd22012-06-08 21:56:10 +0000640class CommandObjectProcessContinue : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000641{
642public:
643
Greg Clayton238c0a12010-09-18 01:14:36 +0000644 CommandObjectProcessContinue (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000645 CommandObjectParsed (interpreter,
646 "process continue",
647 "Continue execution of all threads in the current process.",
648 "process continue",
649 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
Chris Lattner24943d22010-06-08 16:52:24 +0000650 {
651 }
652
653
654 ~CommandObjectProcessContinue ()
655 {
656 }
657
Jim Inghamda26bd22012-06-08 21:56:10 +0000658protected:
Chris Lattner24943d22010-06-08 16:52:24 +0000659 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000660 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000661 CommandReturnObject &result)
662 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000663 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton238c0a12010-09-18 01:14:36 +0000664 bool synchronous_execution = m_interpreter.GetSynchronous ();
Chris Lattner24943d22010-06-08 16:52:24 +0000665
666 if (process == NULL)
667 {
668 result.AppendError ("no process to continue");
669 result.SetStatus (eReturnStatusFailed);
670 return false;
671 }
672
673 StateType state = process->GetState();
674 if (state == eStateStopped)
675 {
676 if (command.GetArgumentCount() != 0)
677 {
678 result.AppendErrorWithFormat ("The '%s' command does not take any arguments.\n", m_cmd_name.c_str());
679 result.SetStatus (eReturnStatusFailed);
680 return false;
681 }
682
683 const uint32_t num_threads = process->GetThreadList().GetSize();
684
685 // Set the actions that the threads should each take when resuming
686 for (uint32_t idx=0; idx<num_threads; ++idx)
687 {
688 process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState (eStateRunning);
689 }
690
691 Error error(process->Resume());
692 if (error.Success())
693 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000694 result.AppendMessageWithFormat ("Process %llu resuming\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000695 if (synchronous_execution)
696 {
Greg Claytonbef15832010-07-14 00:18:15 +0000697 state = process->WaitForProcessToStop (NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000698
699 result.SetDidChangeProcessState (true);
Greg Clayton444e35b2011-10-19 18:09:39 +0000700 result.AppendMessageWithFormat ("Process %llu %s\n", process->GetID(), StateAsCString (state));
Chris Lattner24943d22010-06-08 16:52:24 +0000701 result.SetStatus (eReturnStatusSuccessFinishNoResult);
702 }
703 else
704 {
705 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
706 }
707 }
708 else
709 {
710 result.AppendErrorWithFormat("Failed to resume process: %s.\n", error.AsCString());
711 result.SetStatus (eReturnStatusFailed);
712 }
713 }
714 else
715 {
716 result.AppendErrorWithFormat ("Process cannot be continued from its current state (%s).\n",
717 StateAsCString(state));
718 result.SetStatus (eReturnStatusFailed);
719 }
720 return result.Succeeded();
721 }
722};
723
724//-------------------------------------------------------------------------
725// CommandObjectProcessDetach
726//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000727#pragma mark CommandObjectProcessDetach
Chris Lattner24943d22010-06-08 16:52:24 +0000728
Jim Inghamda26bd22012-06-08 21:56:10 +0000729class CommandObjectProcessDetach : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000730{
731public:
732
Greg Clayton238c0a12010-09-18 01:14:36 +0000733 CommandObjectProcessDetach (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000734 CommandObjectParsed (interpreter,
735 "process detach",
736 "Detach from the current process being debugged.",
737 "process detach",
738 eFlagProcessMustBeLaunched)
Chris Lattner24943d22010-06-08 16:52:24 +0000739 {
740 }
741
742 ~CommandObjectProcessDetach ()
743 {
744 }
745
Jim Inghamda26bd22012-06-08 21:56:10 +0000746protected:
Chris Lattner24943d22010-06-08 16:52:24 +0000747 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000748 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000749 CommandReturnObject &result)
750 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000751 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000752 if (process == NULL)
753 {
754 result.AppendError ("must have a valid process in order to detach");
755 result.SetStatus (eReturnStatusFailed);
756 return false;
757 }
758
Greg Clayton444e35b2011-10-19 18:09:39 +0000759 result.AppendMessageWithFormat ("Detaching from process %llu\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000760 Error error (process->Detach());
761 if (error.Success())
762 {
763 result.SetStatus (eReturnStatusSuccessFinishResult);
764 }
765 else
766 {
767 result.AppendErrorWithFormat ("Detach failed: %s\n", error.AsCString());
768 result.SetStatus (eReturnStatusFailed);
769 return false;
770 }
771 return result.Succeeded();
772 }
773};
774
775//-------------------------------------------------------------------------
Greg Claytone71e2582011-02-04 01:58:07 +0000776// CommandObjectProcessConnect
777//-------------------------------------------------------------------------
778#pragma mark CommandObjectProcessConnect
779
Jim Inghamda26bd22012-06-08 21:56:10 +0000780class CommandObjectProcessConnect : public CommandObjectParsed
Greg Claytone71e2582011-02-04 01:58:07 +0000781{
782public:
783
784 class CommandOptions : public Options
785 {
786 public:
787
Greg Claytonf15996e2011-04-07 22:46:35 +0000788 CommandOptions (CommandInterpreter &interpreter) :
789 Options(interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000790 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000791 // Keep default values of all options in one place: OptionParsingStarting ()
792 OptionParsingStarting ();
Greg Claytone71e2582011-02-04 01:58:07 +0000793 }
794
795 ~CommandOptions ()
796 {
797 }
798
799 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000800 SetOptionValue (uint32_t option_idx, const char *option_arg)
Greg Claytone71e2582011-02-04 01:58:07 +0000801 {
802 Error error;
803 char short_option = (char) m_getopt_table[option_idx].val;
804
805 switch (short_option)
806 {
807 case 'p':
808 plugin_name.assign (option_arg);
809 break;
810
811 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000812 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Greg Claytone71e2582011-02-04 01:58:07 +0000813 break;
814 }
815 return error;
816 }
817
818 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000819 OptionParsingStarting ()
Greg Claytone71e2582011-02-04 01:58:07 +0000820 {
Greg Claytone71e2582011-02-04 01:58:07 +0000821 plugin_name.clear();
822 }
823
Greg Claytonb3448432011-03-24 21:19:54 +0000824 const OptionDefinition*
Greg Claytone71e2582011-02-04 01:58:07 +0000825 GetDefinitions ()
826 {
827 return g_option_table;
828 }
829
830 // Options table: Required for subclasses of Options.
831
Greg Claytonb3448432011-03-24 21:19:54 +0000832 static OptionDefinition g_option_table[];
Greg Claytone71e2582011-02-04 01:58:07 +0000833
834 // Instance variables to hold the values for command options.
835
836 std::string plugin_name;
837 };
838
839 CommandObjectProcessConnect (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000840 CommandObjectParsed (interpreter,
841 "process connect",
842 "Connect to a remote debug service.",
843 "process connect <remote-url>",
844 0),
Greg Claytonf15996e2011-04-07 22:46:35 +0000845 m_options (interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000846 {
847 }
848
849 ~CommandObjectProcessConnect ()
850 {
851 }
852
853
Jim Inghamda26bd22012-06-08 21:56:10 +0000854 Options *
855 GetOptions ()
856 {
857 return &m_options;
858 }
859
860protected:
Greg Claytone71e2582011-02-04 01:58:07 +0000861 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000862 DoExecute (Args& command,
Greg Claytone71e2582011-02-04 01:58:07 +0000863 CommandReturnObject &result)
864 {
865
866 TargetSP target_sp (m_interpreter.GetDebugger().GetSelectedTarget());
867 Error error;
Greg Clayton567e7f32011-09-22 04:58:26 +0000868 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytone71e2582011-02-04 01:58:07 +0000869 if (process)
870 {
871 if (process->IsAlive())
872 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000873 result.AppendErrorWithFormat ("Process %llu is currently being debugged, kill the process before connecting.\n",
Greg Claytone71e2582011-02-04 01:58:07 +0000874 process->GetID());
875 result.SetStatus (eReturnStatusFailed);
876 return false;
877 }
878 }
879
880 if (!target_sp)
881 {
882 // If there isn't a current target create one.
883 FileSpec emptyFileSpec;
Greg Claytone71e2582011-02-04 01:58:07 +0000884
885 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
886 emptyFileSpec,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000887 NULL,
Greg Claytone71e2582011-02-04 01:58:07 +0000888 false,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000889 NULL, // No platform options
Greg Claytone71e2582011-02-04 01:58:07 +0000890 target_sp);
891 if (!target_sp || error.Fail())
892 {
893 result.AppendError(error.AsCString("Error creating target"));
894 result.SetStatus (eReturnStatusFailed);
895 return false;
896 }
897 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target_sp.get());
898 }
899
900 if (command.GetArgumentCount() == 1)
901 {
902 const char *plugin_name = NULL;
903 if (!m_options.plugin_name.empty())
904 plugin_name = m_options.plugin_name.c_str();
905
906 const char *remote_url = command.GetArgumentAtIndex(0);
Greg Clayton46c9a352012-02-09 06:16:32 +0000907 process = target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name, NULL).get();
Greg Claytone71e2582011-02-04 01:58:07 +0000908
909 if (process)
910 {
911 error = process->ConnectRemote (remote_url);
912
913 if (error.Fail())
914 {
915 result.AppendError(error.AsCString("Remote connect failed"));
916 result.SetStatus (eReturnStatusFailed);
Greg Clayton0cbb93b2012-03-31 00:10:30 +0000917 target_sp->DeleteCurrentProcess();
Greg Claytone71e2582011-02-04 01:58:07 +0000918 return false;
919 }
920 }
921 else
922 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000923 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",
924 m_cmd_name.c_str());
Greg Claytone71e2582011-02-04 01:58:07 +0000925 result.SetStatus (eReturnStatusFailed);
926 }
927 }
928 else
929 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000930 result.AppendErrorWithFormat ("'%s' takes exactly one argument:\nUsage: %s\n",
Greg Claytone71e2582011-02-04 01:58:07 +0000931 m_cmd_name.c_str(),
932 m_cmd_syntax.c_str());
933 result.SetStatus (eReturnStatusFailed);
934 }
935 return result.Succeeded();
936 }
Greg Claytone71e2582011-02-04 01:58:07 +0000937
938 CommandOptions m_options;
939};
940
941
Greg Claytonb3448432011-03-24 21:19:54 +0000942OptionDefinition
Greg Claytone71e2582011-02-04 01:58:07 +0000943CommandObjectProcessConnect::CommandOptions::g_option_table[] =
944{
945 { LLDB_OPT_SET_ALL, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
946 { 0, false, NULL, 0 , 0, NULL, 0, eArgTypeNone, NULL }
947};
948
949//-------------------------------------------------------------------------
Greg Clayton0baa3942010-11-04 01:54:29 +0000950// CommandObjectProcessLoad
951//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000952#pragma mark CommandObjectProcessLoad
Greg Clayton0baa3942010-11-04 01:54:29 +0000953
Jim Inghamda26bd22012-06-08 21:56:10 +0000954class CommandObjectProcessLoad : public CommandObjectParsed
Greg Clayton0baa3942010-11-04 01:54:29 +0000955{
956public:
957
958 CommandObjectProcessLoad (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000959 CommandObjectParsed (interpreter,
960 "process load",
961 "Load a shared library into the current process.",
962 "process load <filename> [<filename> ...]",
963 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
Greg Clayton0baa3942010-11-04 01:54:29 +0000964 {
965 }
966
967 ~CommandObjectProcessLoad ()
968 {
969 }
970
Jim Inghamda26bd22012-06-08 21:56:10 +0000971protected:
Greg Clayton0baa3942010-11-04 01:54:29 +0000972 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000973 DoExecute (Args& command,
Greg Clayton0baa3942010-11-04 01:54:29 +0000974 CommandReturnObject &result)
975 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000976 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton0baa3942010-11-04 01:54:29 +0000977 if (process == NULL)
978 {
979 result.AppendError ("must have a valid process in order to load a shared library");
980 result.SetStatus (eReturnStatusFailed);
981 return false;
982 }
983
984 const uint32_t argc = command.GetArgumentCount();
985
986 for (uint32_t i=0; i<argc; ++i)
987 {
988 Error error;
989 const char *image_path = command.GetArgumentAtIndex(i);
990 FileSpec image_spec (image_path, false);
Greg Claytonf2bf8702011-08-11 16:25:18 +0000991 process->GetTarget().GetPlatform()->ResolveRemotePath(image_spec, image_spec);
Greg Clayton0baa3942010-11-04 01:54:29 +0000992 uint32_t image_token = process->LoadImage(image_spec, error);
993 if (image_token != LLDB_INVALID_IMAGE_TOKEN)
994 {
995 result.AppendMessageWithFormat ("Loading \"%s\"...ok\nImage %u loaded.\n", image_path, image_token);
996 result.SetStatus (eReturnStatusSuccessFinishResult);
997 }
998 else
999 {
1000 result.AppendErrorWithFormat ("failed to load '%s': %s", image_path, error.AsCString());
1001 result.SetStatus (eReturnStatusFailed);
1002 }
1003 }
1004 return result.Succeeded();
1005 }
1006};
1007
1008
1009//-------------------------------------------------------------------------
1010// CommandObjectProcessUnload
1011//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001012#pragma mark CommandObjectProcessUnload
Greg Clayton0baa3942010-11-04 01:54:29 +00001013
Jim Inghamda26bd22012-06-08 21:56:10 +00001014class CommandObjectProcessUnload : public CommandObjectParsed
Greg Clayton0baa3942010-11-04 01:54:29 +00001015{
1016public:
1017
1018 CommandObjectProcessUnload (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001019 CommandObjectParsed (interpreter,
1020 "process unload",
1021 "Unload a shared library from the current process using the index returned by a previous call to \"process load\".",
1022 "process unload <index>",
1023 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
Greg Clayton0baa3942010-11-04 01:54:29 +00001024 {
1025 }
1026
1027 ~CommandObjectProcessUnload ()
1028 {
1029 }
1030
Jim Inghamda26bd22012-06-08 21:56:10 +00001031protected:
Greg Clayton0baa3942010-11-04 01:54:29 +00001032 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001033 DoExecute (Args& command,
Greg Clayton0baa3942010-11-04 01:54:29 +00001034 CommandReturnObject &result)
1035 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001036 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton0baa3942010-11-04 01:54:29 +00001037 if (process == NULL)
1038 {
1039 result.AppendError ("must have a valid process in order to load a shared library");
1040 result.SetStatus (eReturnStatusFailed);
1041 return false;
1042 }
1043
1044 const uint32_t argc = command.GetArgumentCount();
1045
1046 for (uint32_t i=0; i<argc; ++i)
1047 {
1048 const char *image_token_cstr = command.GetArgumentAtIndex(i);
1049 uint32_t image_token = Args::StringToUInt32(image_token_cstr, LLDB_INVALID_IMAGE_TOKEN, 0);
1050 if (image_token == LLDB_INVALID_IMAGE_TOKEN)
1051 {
1052 result.AppendErrorWithFormat ("invalid image index argument '%s'", image_token_cstr);
1053 result.SetStatus (eReturnStatusFailed);
1054 break;
1055 }
1056 else
1057 {
1058 Error error (process->UnloadImage(image_token));
1059 if (error.Success())
1060 {
1061 result.AppendMessageWithFormat ("Unloading shared library with index %u...ok\n", image_token);
1062 result.SetStatus (eReturnStatusSuccessFinishResult);
1063 }
1064 else
1065 {
1066 result.AppendErrorWithFormat ("failed to unload image: %s", error.AsCString());
1067 result.SetStatus (eReturnStatusFailed);
1068 break;
1069 }
1070 }
1071 }
1072 return result.Succeeded();
1073 }
1074};
1075
1076//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001077// CommandObjectProcessSignal
1078//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001079#pragma mark CommandObjectProcessSignal
Chris Lattner24943d22010-06-08 16:52:24 +00001080
Jim Inghamda26bd22012-06-08 21:56:10 +00001081class CommandObjectProcessSignal : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001082{
1083public:
1084
Greg Clayton238c0a12010-09-18 01:14:36 +00001085 CommandObjectProcessSignal (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001086 CommandObjectParsed (interpreter,
1087 "process signal",
1088 "Send a UNIX signal to the current process being debugged.",
1089 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001090 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001091 CommandArgumentEntry arg;
1092 CommandArgumentData signal_arg;
1093
1094 // Define the first (and only) variant of this arg.
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001095 signal_arg.arg_type = eArgTypeUnixSignal;
Caroline Tice43b014a2010-10-04 22:28:36 +00001096 signal_arg.arg_repetition = eArgRepeatPlain;
1097
1098 // There is only one variant this argument could be; put it into the argument entry.
1099 arg.push_back (signal_arg);
1100
1101 // Push the data for the first argument into the m_arguments vector.
1102 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001103 }
1104
1105 ~CommandObjectProcessSignal ()
1106 {
1107 }
1108
Jim Inghamda26bd22012-06-08 21:56:10 +00001109protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001110 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001111 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001112 CommandReturnObject &result)
1113 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001114 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001115 if (process == NULL)
1116 {
1117 result.AppendError ("no process to signal");
1118 result.SetStatus (eReturnStatusFailed);
1119 return false;
1120 }
1121
1122 if (command.GetArgumentCount() == 1)
1123 {
Greg Clayton8f6be2a2010-10-09 01:40:57 +00001124 int signo = LLDB_INVALID_SIGNAL_NUMBER;
1125
1126 const char *signal_name = command.GetArgumentAtIndex(0);
1127 if (::isxdigit (signal_name[0]))
1128 signo = Args::StringToSInt32(signal_name, LLDB_INVALID_SIGNAL_NUMBER, 0);
1129 else
1130 signo = process->GetUnixSignals().GetSignalNumberFromName (signal_name);
1131
1132 if (signo == LLDB_INVALID_SIGNAL_NUMBER)
Chris Lattner24943d22010-06-08 16:52:24 +00001133 {
1134 result.AppendErrorWithFormat ("Invalid signal argument '%s'.\n", command.GetArgumentAtIndex(0));
1135 result.SetStatus (eReturnStatusFailed);
1136 }
1137 else
1138 {
1139 Error error (process->Signal (signo));
1140 if (error.Success())
1141 {
1142 result.SetStatus (eReturnStatusSuccessFinishResult);
1143 }
1144 else
1145 {
1146 result.AppendErrorWithFormat ("Failed to send signal %i: %s\n", signo, error.AsCString());
1147 result.SetStatus (eReturnStatusFailed);
1148 }
1149 }
1150 }
1151 else
1152 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001153 result.AppendErrorWithFormat("'%s' takes exactly one signal number argument:\nUsage: %s\n", m_cmd_name.c_str(),
Chris Lattner24943d22010-06-08 16:52:24 +00001154 m_cmd_syntax.c_str());
1155 result.SetStatus (eReturnStatusFailed);
1156 }
1157 return result.Succeeded();
1158 }
1159};
1160
1161
1162//-------------------------------------------------------------------------
1163// CommandObjectProcessInterrupt
1164//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001165#pragma mark CommandObjectProcessInterrupt
Chris Lattner24943d22010-06-08 16:52:24 +00001166
Jim Inghamda26bd22012-06-08 21:56:10 +00001167class CommandObjectProcessInterrupt : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001168{
1169public:
1170
1171
Greg Clayton238c0a12010-09-18 01:14:36 +00001172 CommandObjectProcessInterrupt (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001173 CommandObjectParsed (interpreter,
1174 "process interrupt",
1175 "Interrupt the current process being debugged.",
1176 "process interrupt",
1177 eFlagProcessMustBeLaunched)
Chris Lattner24943d22010-06-08 16:52:24 +00001178 {
1179 }
1180
1181 ~CommandObjectProcessInterrupt ()
1182 {
1183 }
1184
Jim Inghamda26bd22012-06-08 21:56:10 +00001185protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001186 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001187 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001188 CommandReturnObject &result)
1189 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001190 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001191 if (process == NULL)
1192 {
1193 result.AppendError ("no process to halt");
1194 result.SetStatus (eReturnStatusFailed);
1195 return false;
1196 }
1197
1198 if (command.GetArgumentCount() == 0)
1199 {
1200 Error error(process->Halt ());
1201 if (error.Success())
1202 {
1203 result.SetStatus (eReturnStatusSuccessFinishResult);
1204
1205 // Maybe we should add a "SuspendThreadPlans so we
1206 // can halt, and keep in place all the current thread plans.
1207 process->GetThreadList().DiscardThreadPlans();
1208 }
1209 else
1210 {
1211 result.AppendErrorWithFormat ("Failed to halt process: %s\n", error.AsCString());
1212 result.SetStatus (eReturnStatusFailed);
1213 }
1214 }
1215 else
1216 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001217 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner24943d22010-06-08 16:52:24 +00001218 m_cmd_name.c_str(),
1219 m_cmd_syntax.c_str());
1220 result.SetStatus (eReturnStatusFailed);
1221 }
1222 return result.Succeeded();
1223 }
1224};
1225
1226//-------------------------------------------------------------------------
1227// CommandObjectProcessKill
1228//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001229#pragma mark CommandObjectProcessKill
Chris Lattner24943d22010-06-08 16:52:24 +00001230
Jim Inghamda26bd22012-06-08 21:56:10 +00001231class CommandObjectProcessKill : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001232{
1233public:
1234
Greg Clayton238c0a12010-09-18 01:14:36 +00001235 CommandObjectProcessKill (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001236 CommandObjectParsed (interpreter,
1237 "process kill",
1238 "Terminate the current process being debugged.",
1239 "process kill",
1240 eFlagProcessMustBeLaunched)
Chris Lattner24943d22010-06-08 16:52:24 +00001241 {
1242 }
1243
1244 ~CommandObjectProcessKill ()
1245 {
1246 }
1247
Jim Inghamda26bd22012-06-08 21:56:10 +00001248protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001249 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001250 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001251 CommandReturnObject &result)
1252 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001253 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001254 if (process == NULL)
1255 {
1256 result.AppendError ("no process to kill");
1257 result.SetStatus (eReturnStatusFailed);
1258 return false;
1259 }
1260
1261 if (command.GetArgumentCount() == 0)
1262 {
1263 Error error (process->Destroy());
1264 if (error.Success())
1265 {
1266 result.SetStatus (eReturnStatusSuccessFinishResult);
1267 }
1268 else
1269 {
1270 result.AppendErrorWithFormat ("Failed to kill process: %s\n", error.AsCString());
1271 result.SetStatus (eReturnStatusFailed);
1272 }
1273 }
1274 else
1275 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001276 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner24943d22010-06-08 16:52:24 +00001277 m_cmd_name.c_str(),
1278 m_cmd_syntax.c_str());
1279 result.SetStatus (eReturnStatusFailed);
1280 }
1281 return result.Succeeded();
1282 }
1283};
1284
1285//-------------------------------------------------------------------------
Jim Ingham41313fc2010-06-18 01:23:09 +00001286// CommandObjectProcessStatus
1287//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001288#pragma mark CommandObjectProcessStatus
1289
Jim Inghamda26bd22012-06-08 21:56:10 +00001290class CommandObjectProcessStatus : public CommandObjectParsed
Jim Ingham41313fc2010-06-18 01:23:09 +00001291{
1292public:
Greg Clayton238c0a12010-09-18 01:14:36 +00001293 CommandObjectProcessStatus (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001294 CommandObjectParsed (interpreter,
1295 "process status",
1296 "Show the current status and location of executing process.",
1297 "process status",
1298 0)
Jim Ingham41313fc2010-06-18 01:23:09 +00001299 {
1300 }
1301
1302 ~CommandObjectProcessStatus()
1303 {
1304 }
1305
1306
1307 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001308 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham41313fc2010-06-18 01:23:09 +00001309 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001310 Stream &strm = result.GetOutputStream();
Jim Ingham41313fc2010-06-18 01:23:09 +00001311 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Claytonb72d0f02011-04-12 05:54:46 +00001312 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
Greg Clayton567e7f32011-09-22 04:58:26 +00001313 Process *process = exe_ctx.GetProcessPtr();
1314 if (process)
Jim Ingham41313fc2010-06-18 01:23:09 +00001315 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001316 const bool only_threads_with_stop_reason = true;
1317 const uint32_t start_frame = 0;
1318 const uint32_t num_frames = 1;
1319 const uint32_t num_frames_with_source = 1;
Greg Clayton567e7f32011-09-22 04:58:26 +00001320 process->GetStatus(strm);
1321 process->GetThreadStatus (strm,
1322 only_threads_with_stop_reason,
1323 start_frame,
1324 num_frames,
1325 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001326
Jim Ingham41313fc2010-06-18 01:23:09 +00001327 }
1328 else
1329 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001330 result.AppendError ("No process.");
Jim Ingham41313fc2010-06-18 01:23:09 +00001331 result.SetStatus (eReturnStatusFailed);
1332 }
1333 return result.Succeeded();
1334 }
1335};
1336
1337//-------------------------------------------------------------------------
Caroline Tice23d6f272010-10-13 20:44:39 +00001338// CommandObjectProcessHandle
1339//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001340#pragma mark CommandObjectProcessHandle
Caroline Tice23d6f272010-10-13 20:44:39 +00001341
Jim Inghamda26bd22012-06-08 21:56:10 +00001342class CommandObjectProcessHandle : public CommandObjectParsed
Caroline Tice23d6f272010-10-13 20:44:39 +00001343{
1344public:
1345
1346 class CommandOptions : public Options
1347 {
1348 public:
1349
Greg Claytonf15996e2011-04-07 22:46:35 +00001350 CommandOptions (CommandInterpreter &interpreter) :
1351 Options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001352 {
Greg Clayton143fcc32011-04-13 00:18:08 +00001353 OptionParsingStarting ();
Caroline Tice23d6f272010-10-13 20:44:39 +00001354 }
1355
1356 ~CommandOptions ()
1357 {
1358 }
1359
1360 Error
Greg Clayton143fcc32011-04-13 00:18:08 +00001361 SetOptionValue (uint32_t option_idx, const char *option_arg)
Caroline Tice23d6f272010-10-13 20:44:39 +00001362 {
1363 Error error;
1364 char short_option = (char) m_getopt_table[option_idx].val;
1365
1366 switch (short_option)
1367 {
1368 case 's':
1369 stop = option_arg;
1370 break;
1371 case 'n':
1372 notify = option_arg;
1373 break;
1374 case 'p':
1375 pass = option_arg;
1376 break;
1377 default:
Greg Clayton9c236732011-10-26 00:56:27 +00001378 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Caroline Tice23d6f272010-10-13 20:44:39 +00001379 break;
1380 }
1381 return error;
1382 }
1383
1384 void
Greg Clayton143fcc32011-04-13 00:18:08 +00001385 OptionParsingStarting ()
Caroline Tice23d6f272010-10-13 20:44:39 +00001386 {
Caroline Tice23d6f272010-10-13 20:44:39 +00001387 stop.clear();
1388 notify.clear();
1389 pass.clear();
1390 }
1391
Greg Claytonb3448432011-03-24 21:19:54 +00001392 const OptionDefinition*
Caroline Tice23d6f272010-10-13 20:44:39 +00001393 GetDefinitions ()
1394 {
1395 return g_option_table;
1396 }
1397
1398 // Options table: Required for subclasses of Options.
1399
Greg Claytonb3448432011-03-24 21:19:54 +00001400 static OptionDefinition g_option_table[];
Caroline Tice23d6f272010-10-13 20:44:39 +00001401
1402 // Instance variables to hold the values for command options.
1403
1404 std::string stop;
1405 std::string notify;
1406 std::string pass;
1407 };
1408
1409
1410 CommandObjectProcessHandle (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001411 CommandObjectParsed (interpreter,
1412 "process handle",
1413 "Show or update what the process and debugger should do with various signals received from the OS.",
1414 NULL),
Greg Claytonf15996e2011-04-07 22:46:35 +00001415 m_options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001416 {
Caroline Ticee7471982010-10-14 21:31:13 +00001417 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 +00001418 CommandArgumentEntry arg;
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001419 CommandArgumentData signal_arg;
Caroline Tice23d6f272010-10-13 20:44:39 +00001420
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001421 signal_arg.arg_type = eArgTypeUnixSignal;
1422 signal_arg.arg_repetition = eArgRepeatStar;
Caroline Tice23d6f272010-10-13 20:44:39 +00001423
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001424 arg.push_back (signal_arg);
Caroline Tice23d6f272010-10-13 20:44:39 +00001425
1426 m_arguments.push_back (arg);
1427 }
1428
1429 ~CommandObjectProcessHandle ()
1430 {
1431 }
1432
1433 Options *
1434 GetOptions ()
1435 {
1436 return &m_options;
1437 }
1438
1439 bool
Caroline Ticee7471982010-10-14 21:31:13 +00001440 VerifyCommandOptionValue (const std::string &option, int &real_value)
Caroline Tice23d6f272010-10-13 20:44:39 +00001441 {
1442 bool okay = true;
1443
Caroline Ticee7471982010-10-14 21:31:13 +00001444 bool success = false;
1445 bool tmp_value = Args::StringToBoolean (option.c_str(), false, &success);
1446
1447 if (success && tmp_value)
1448 real_value = 1;
1449 else if (success && !tmp_value)
1450 real_value = 0;
Caroline Tice23d6f272010-10-13 20:44:39 +00001451 else
1452 {
1453 // If the value isn't 'true' or 'false', it had better be 0 or 1.
Caroline Ticee7471982010-10-14 21:31:13 +00001454 real_value = Args::StringToUInt32 (option.c_str(), 3);
1455 if (real_value != 0 && real_value != 1)
Caroline Tice23d6f272010-10-13 20:44:39 +00001456 okay = false;
1457 }
1458
1459 return okay;
1460 }
1461
Caroline Ticee7471982010-10-14 21:31:13 +00001462 void
1463 PrintSignalHeader (Stream &str)
1464 {
1465 str.Printf ("NAME PASS STOP NOTIFY\n");
1466 str.Printf ("========== ===== ===== ======\n");
1467 }
1468
1469 void
1470 PrintSignal (Stream &str, int32_t signo, const char *sig_name, UnixSignals &signals)
1471 {
1472 bool stop;
1473 bool suppress;
1474 bool notify;
1475
1476 str.Printf ("%-10s ", sig_name);
1477 if (signals.GetSignalInfo (signo, suppress, stop, notify))
1478 {
1479 bool pass = !suppress;
1480 str.Printf ("%s %s %s",
1481 (pass ? "true " : "false"),
1482 (stop ? "true " : "false"),
1483 (notify ? "true " : "false"));
1484 }
1485 str.Printf ("\n");
1486 }
1487
1488 void
1489 PrintSignalInformation (Stream &str, Args &signal_args, int num_valid_signals, UnixSignals &signals)
1490 {
1491 PrintSignalHeader (str);
1492
1493 if (num_valid_signals > 0)
1494 {
1495 size_t num_args = signal_args.GetArgumentCount();
1496 for (size_t i = 0; i < num_args; ++i)
1497 {
1498 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1499 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
1500 PrintSignal (str, signo, signal_args.GetArgumentAtIndex (i), signals);
1501 }
1502 }
1503 else // Print info for ALL signals
1504 {
1505 int32_t signo = signals.GetFirstSignalNumber();
1506 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1507 {
1508 PrintSignal (str, signo, signals.GetSignalAsCString (signo), signals);
1509 signo = signals.GetNextSignalNumber (signo);
1510 }
1511 }
1512 }
1513
Jim Inghamda26bd22012-06-08 21:56:10 +00001514protected:
Caroline Tice23d6f272010-10-13 20:44:39 +00001515 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001516 DoExecute (Args &signal_args, CommandReturnObject &result)
Caroline Tice23d6f272010-10-13 20:44:39 +00001517 {
1518 TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
1519
1520 if (!target_sp)
1521 {
1522 result.AppendError ("No current target;"
1523 " cannot handle signals until you have a valid target and process.\n");
1524 result.SetStatus (eReturnStatusFailed);
1525 return false;
1526 }
1527
1528 ProcessSP process_sp = target_sp->GetProcessSP();
1529
1530 if (!process_sp)
1531 {
1532 result.AppendError ("No current process; cannot handle signals until you have a valid process.\n");
1533 result.SetStatus (eReturnStatusFailed);
1534 return false;
1535 }
1536
Caroline Tice23d6f272010-10-13 20:44:39 +00001537 int stop_action = -1; // -1 means leave the current setting alone
Caroline Ticee7471982010-10-14 21:31:13 +00001538 int pass_action = -1; // -1 means leave the current setting alone
Caroline Tice23d6f272010-10-13 20:44:39 +00001539 int notify_action = -1; // -1 means leave the current setting alone
1540
1541 if (! m_options.stop.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001542 && ! VerifyCommandOptionValue (m_options.stop, stop_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001543 {
1544 result.AppendError ("Invalid argument for command option --stop; must be true or false.\n");
1545 result.SetStatus (eReturnStatusFailed);
1546 return false;
1547 }
1548
1549 if (! m_options.notify.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001550 && ! VerifyCommandOptionValue (m_options.notify, notify_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001551 {
1552 result.AppendError ("Invalid argument for command option --notify; must be true or false.\n");
1553 result.SetStatus (eReturnStatusFailed);
1554 return false;
1555 }
1556
1557 if (! m_options.pass.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001558 && ! VerifyCommandOptionValue (m_options.pass, pass_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001559 {
1560 result.AppendError ("Invalid argument for command option --pass; must be true or false.\n");
1561 result.SetStatus (eReturnStatusFailed);
1562 return false;
1563 }
1564
1565 size_t num_args = signal_args.GetArgumentCount();
1566 UnixSignals &signals = process_sp->GetUnixSignals();
1567 int num_signals_set = 0;
1568
Caroline Ticee7471982010-10-14 21:31:13 +00001569 if (num_args > 0)
Caroline Tice23d6f272010-10-13 20:44:39 +00001570 {
Caroline Ticee7471982010-10-14 21:31:13 +00001571 for (size_t i = 0; i < num_args; ++i)
Caroline Tice23d6f272010-10-13 20:44:39 +00001572 {
Caroline Ticee7471982010-10-14 21:31:13 +00001573 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1574 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
Caroline Tice23d6f272010-10-13 20:44:39 +00001575 {
Caroline Ticee7471982010-10-14 21:31:13 +00001576 // Casting the actions as bools here should be okay, because VerifyCommandOptionValue guarantees
1577 // the value is either 0 or 1.
1578 if (stop_action != -1)
1579 signals.SetShouldStop (signo, (bool) stop_action);
1580 if (pass_action != -1)
1581 {
1582 bool suppress = ! ((bool) pass_action);
1583 signals.SetShouldSuppress (signo, suppress);
1584 }
1585 if (notify_action != -1)
1586 signals.SetShouldNotify (signo, (bool) notify_action);
1587 ++num_signals_set;
Caroline Tice23d6f272010-10-13 20:44:39 +00001588 }
Caroline Ticee7471982010-10-14 21:31:13 +00001589 else
1590 {
1591 result.AppendErrorWithFormat ("Invalid signal name '%s'\n", signal_args.GetArgumentAtIndex (i));
1592 }
Caroline Tice23d6f272010-10-13 20:44:39 +00001593 }
1594 }
Caroline Ticee7471982010-10-14 21:31:13 +00001595 else
1596 {
1597 // No signal specified, if any command options were specified, update ALL signals.
1598 if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1))
1599 {
1600 if (m_interpreter.Confirm ("Do you really want to update all the signals?", false))
1601 {
1602 int32_t signo = signals.GetFirstSignalNumber();
1603 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1604 {
1605 if (notify_action != -1)
1606 signals.SetShouldNotify (signo, (bool) notify_action);
1607 if (stop_action != -1)
1608 signals.SetShouldStop (signo, (bool) stop_action);
1609 if (pass_action != -1)
1610 {
1611 bool suppress = ! ((bool) pass_action);
1612 signals.SetShouldSuppress (signo, suppress);
1613 }
1614 signo = signals.GetNextSignalNumber (signo);
1615 }
1616 }
1617 }
1618 }
1619
1620 PrintSignalInformation (result.GetOutputStream(), signal_args, num_signals_set, signals);
Caroline Tice23d6f272010-10-13 20:44:39 +00001621
1622 if (num_signals_set > 0)
1623 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1624 else
1625 result.SetStatus (eReturnStatusFailed);
1626
1627 return result.Succeeded();
1628 }
1629
Caroline Tice23d6f272010-10-13 20:44:39 +00001630 CommandOptions m_options;
1631};
1632
Greg Claytonb3448432011-03-24 21:19:54 +00001633OptionDefinition
Caroline Tice23d6f272010-10-13 20:44:39 +00001634CommandObjectProcessHandle::CommandOptions::g_option_table[] =
1635{
1636{ 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." },
1637{ 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." },
1638{ LLDB_OPT_SET_1, false, "pass", 'p', required_argument, NULL, 0, eArgTypeBoolean, "Whether or not the signal should be passed to the process." },
1639{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1640};
1641
1642//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001643// CommandObjectMultiwordProcess
1644//-------------------------------------------------------------------------
1645
Greg Clayton63094e02010-06-23 01:19:29 +00001646CommandObjectMultiwordProcess::CommandObjectMultiwordProcess (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00001647 CommandObjectMultiword (interpreter,
1648 "process",
1649 "A set of commands for operating on a process.",
1650 "process <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00001651{
Greg Claytona9eb8272011-07-02 21:07:54 +00001652 LoadSubCommand ("attach", CommandObjectSP (new CommandObjectProcessAttach (interpreter)));
1653 LoadSubCommand ("launch", CommandObjectSP (new CommandObjectProcessLaunch (interpreter)));
1654 LoadSubCommand ("continue", CommandObjectSP (new CommandObjectProcessContinue (interpreter)));
1655 LoadSubCommand ("connect", CommandObjectSP (new CommandObjectProcessConnect (interpreter)));
1656 LoadSubCommand ("detach", CommandObjectSP (new CommandObjectProcessDetach (interpreter)));
1657 LoadSubCommand ("load", CommandObjectSP (new CommandObjectProcessLoad (interpreter)));
1658 LoadSubCommand ("unload", CommandObjectSP (new CommandObjectProcessUnload (interpreter)));
1659 LoadSubCommand ("signal", CommandObjectSP (new CommandObjectProcessSignal (interpreter)));
1660 LoadSubCommand ("handle", CommandObjectSP (new CommandObjectProcessHandle (interpreter)));
1661 LoadSubCommand ("status", CommandObjectSP (new CommandObjectProcessStatus (interpreter)));
Greg Clayton238c0a12010-09-18 01:14:36 +00001662 LoadSubCommand ("interrupt", CommandObjectSP (new CommandObjectProcessInterrupt (interpreter)));
Greg Claytona9eb8272011-07-02 21:07:54 +00001663 LoadSubCommand ("kill", CommandObjectSP (new CommandObjectProcessKill (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00001664}
1665
1666CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess ()
1667{
1668}
1669