blob: 64e0c73b43939d9b325c9ac8ed7fd097e5102c94 [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
Daniel Malead891f9b2012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner24943d22010-06-08 16:52:24 +000012#include "CommandObjectProcess.h"
13
14// C Includes
15// C++ Includes
16// Other libraries and framework includes
17// Project includes
Jim Ingham124e6902012-08-11 01:27:55 +000018#include "lldb/Breakpoint/Breakpoint.h"
19#include "lldb/Breakpoint/BreakpointLocation.h"
20#include "lldb/Breakpoint/BreakpointSite.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Core/State.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000022#include "lldb/Core/Module.h"
Greg Claytonabe0fed2011-04-18 08:33:37 +000023#include "lldb/Host/Host.h"
Jim Ingham124e6902012-08-11 01:27:55 +000024#include "lldb/Interpreter/Args.h"
25#include "lldb/Interpreter/Options.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026#include "lldb/Interpreter/CommandInterpreter.h"
27#include "lldb/Interpreter/CommandReturnObject.h"
Greg Claytone4b9c1f2011-03-08 22:40:15 +000028#include "lldb/Target/Platform.h"
Chris Lattner24943d22010-06-08 16:52:24 +000029#include "lldb/Target/Process.h"
Jim Ingham124e6902012-08-11 01:27:55 +000030#include "lldb/Target/StopInfo.h"
Chris Lattner24943d22010-06-08 16:52:24 +000031#include "lldb/Target/Target.h"
32#include "lldb/Target/Thread.h"
33
34using namespace lldb;
35using namespace lldb_private;
36
37//-------------------------------------------------------------------------
38// CommandObjectProcessLaunch
39//-------------------------------------------------------------------------
Jim Ingham5a15e692012-02-16 06:50:00 +000040#pragma mark CommandObjectProcessLaunch
Jim Inghamda26bd22012-06-08 21:56:10 +000041class CommandObjectProcessLaunch : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +000042{
43public:
44
Greg Clayton238c0a12010-09-18 01:14:36 +000045 CommandObjectProcessLaunch (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +000046 CommandObjectParsed (interpreter,
47 "process launch",
48 "Launch the executable in the debugger.",
Greg Claytonea0bb4d2013-01-09 19:44:40 +000049 NULL,
50 eFlagRequiresTarget),
Greg Claytonf15996e2011-04-07 22:46:35 +000051 m_options (interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +000052 {
Caroline Tice43b014a2010-10-04 22:28:36 +000053 CommandArgumentEntry arg;
54 CommandArgumentData run_args_arg;
55
56 // Define the first (and only) variant of this arg.
57 run_args_arg.arg_type = eArgTypeRunArgs;
58 run_args_arg.arg_repetition = eArgRepeatOptional;
59
60 // There is only one variant this argument could be; put it into the argument entry.
61 arg.push_back (run_args_arg);
62
63 // Push the data for the first argument into the m_arguments vector.
64 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +000065 }
66
67
68 ~CommandObjectProcessLaunch ()
69 {
70 }
71
Jim Ingham392d9e02012-08-10 21:48:41 +000072 int
73 HandleArgumentCompletion (Args &input,
74 int &cursor_index,
75 int &cursor_char_position,
76 OptionElementVector &opt_element_vector,
77 int match_start_point,
78 int max_return_elements,
79 bool &word_complete,
80 StringList &matches)
81 {
82 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
83 completion_str.erase (cursor_char_position);
84
85 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
86 CommandCompletions::eDiskFileCompletion,
87 completion_str.c_str(),
88 match_start_point,
89 max_return_elements,
90 NULL,
91 word_complete,
92 matches);
93 return matches.GetSize();
94 }
95
Chris Lattner24943d22010-06-08 16:52:24 +000096 Options *
97 GetOptions ()
98 {
99 return &m_options;
100 }
101
Jim Inghamda26bd22012-06-08 21:56:10 +0000102 virtual const char *GetRepeatCommand (Args &current_command_args, uint32_t index)
103 {
104 // No repeat for "process launch"...
105 return "";
106 }
107
108protected:
Chris Lattner24943d22010-06-08 16:52:24 +0000109 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000110 DoExecute (Args& launch_args, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +0000111 {
Greg Claytonabb33022011-11-08 02:43:13 +0000112 Debugger &debugger = m_interpreter.GetDebugger();
113 Target *target = debugger.GetSelectedTarget().get();
114 Error error;
Chris Lattner24943d22010-06-08 16:52:24 +0000115 // If our listener is NULL, users aren't allows to launch
Chris Lattner24943d22010-06-08 16:52:24 +0000116 char filename[PATH_MAX];
Greg Clayton5beb99d2011-08-11 02:48:45 +0000117 const Module *exe_module = target->GetExecutableModulePointer();
Greg Claytona2f74232011-02-24 22:24:29 +0000118
119 if (exe_module == NULL)
120 {
Greg Claytone1f50b92011-05-03 22:09:39 +0000121 result.AppendError ("no file in target, create a debug target using the 'target create' command");
Greg Claytona2f74232011-02-24 22:24:29 +0000122 result.SetStatus (eReturnStatusFailed);
123 return false;
124 }
125
Greg Claytona2f74232011-02-24 22:24:29 +0000126 StateType state = eStateInvalid;
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000127 Process *process = m_exe_ctx.GetProcessPtr();
Greg Claytona2f74232011-02-24 22:24:29 +0000128 if (process)
129 {
130 state = process->GetState();
131
132 if (process->IsAlive() && state != eStateConnected)
133 {
134 char message[1024];
135 if (process->GetState() == eStateAttaching)
136 ::strncpy (message, "There is a pending attach, abort it and launch a new process?", sizeof(message));
137 else
138 ::strncpy (message, "There is a running process, kill it and restart?", sizeof(message));
139
140 if (!m_interpreter.Confirm (message, true))
Jim Ingham22dc9722010-12-09 18:58:16 +0000141 {
Greg Claytona2f74232011-02-24 22:24:29 +0000142 result.SetStatus (eReturnStatusFailed);
143 return false;
Jim Ingham22dc9722010-12-09 18:58:16 +0000144 }
145 else
146 {
Greg Claytonabb33022011-11-08 02:43:13 +0000147 Error destroy_error (process->Destroy());
148 if (destroy_error.Success())
Greg Claytona2f74232011-02-24 22:24:29 +0000149 {
150 result.SetStatus (eReturnStatusSuccessFinishResult);
151 }
152 else
153 {
Greg Claytonabb33022011-11-08 02:43:13 +0000154 result.AppendErrorWithFormat ("Failed to kill process: %s\n", destroy_error.AsCString());
Greg Claytona2f74232011-02-24 22:24:29 +0000155 result.SetStatus (eReturnStatusFailed);
156 }
Jim Ingham22dc9722010-12-09 18:58:16 +0000157 }
158 }
Chris Lattner24943d22010-06-08 16:52:24 +0000159 }
Jim Ingham22dc9722010-12-09 18:58:16 +0000160
Greg Clayton0c8446c2012-10-17 22:57:12 +0000161 const char *target_settings_argv0 = target->GetArg0();
162
163 exe_module->GetFileSpec().GetPath (filename, sizeof(filename));
164
165 if (target_settings_argv0)
166 {
167 m_options.launch_info.GetArguments().AppendArgument (target_settings_argv0);
168 m_options.launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), false);
169 }
170 else
171 {
172 m_options.launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
173 }
174
Greg Clayton527154d2011-11-15 03:53:30 +0000175 if (launch_args.GetArgumentCount() == 0)
176 {
Greg Clayton73844aa2012-08-22 17:17:09 +0000177 Args target_setting_args;
Greg Clayton0c8446c2012-10-17 22:57:12 +0000178 if (target->GetRunArguments(target_setting_args))
Greg Clayton73844aa2012-08-22 17:17:09 +0000179 m_options.launch_info.GetArguments().AppendArguments (target_setting_args);
Greg Clayton527154d2011-11-15 03:53:30 +0000180 }
181 else
Greg Claytonabb33022011-11-08 02:43:13 +0000182 {
Greg Clayton0c8446c2012-10-17 22:57:12 +0000183 m_options.launch_info.GetArguments().AppendArguments (launch_args);
184
Greg Clayton3e6f2cc2011-11-21 21:51:18 +0000185 // Save the arguments for subsequent runs in the current target.
186 target->SetRunArguments (launch_args);
Greg Claytonabb33022011-11-08 02:43:13 +0000187 }
Greg Claytonabb33022011-11-08 02:43:13 +0000188
Greg Clayton527154d2011-11-15 03:53:30 +0000189 if (target->GetDisableASLR())
190 m_options.launch_info.GetFlags().Set (eLaunchFlagDisableASLR);
191
192 if (target->GetDisableSTDIO())
193 m_options.launch_info.GetFlags().Set (eLaunchFlagDisableSTDIO);
194
195 m_options.launch_info.GetFlags().Set (eLaunchFlagDebug);
196
197 Args environment;
198 target->GetEnvironmentAsArgs (environment);
199 if (environment.GetArgumentCount() > 0)
200 m_options.launch_info.GetEnvironmentEntries ().AppendArguments (environment);
201
Greg Clayton464c6162011-11-17 22:14:31 +0000202 // Finalize the file actions, and if none were given, default to opening
203 // up a pseudo terminal
204 const bool default_to_use_pty = true;
205 m_options.launch_info.FinalizeFileActions (target, default_to_use_pty);
Greg Clayton527154d2011-11-15 03:53:30 +0000206
Greg Claytonabb33022011-11-08 02:43:13 +0000207 if (state == eStateConnected)
208 {
209 if (m_options.launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
210 {
211 result.AppendWarning("can't launch in tty when launching through a remote connection");
212 m_options.launch_info.GetFlags().Clear (eLaunchFlagLaunchInTTY);
213 }
214 }
215 else
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000216 {
Greg Clayton527154d2011-11-15 03:53:30 +0000217 if (!m_options.launch_info.GetArchitecture().IsValid())
Greg Clayton2d9adb72011-11-12 02:10:56 +0000218 m_options.launch_info.GetArchitecture() = target->GetArchitecture();
219
Greg Clayton75d8c252011-11-28 01:45:00 +0000220 PlatformSP platform_sp (target->GetPlatform());
221
222 if (platform_sp && platform_sp->CanDebugProcess ())
223 {
224 process = target->GetPlatform()->DebugProcess (m_options.launch_info,
225 debugger,
226 target,
227 debugger.GetListener(),
228 error).get();
229 }
230 else
231 {
232 const char *plugin_name = m_options.launch_info.GetProcessPluginName();
Greg Clayton46c9a352012-02-09 06:16:32 +0000233 process = target->CreateProcess (debugger.GetListener(), plugin_name, NULL).get();
Greg Clayton75d8c252011-11-28 01:45:00 +0000234 if (process)
235 error = process->Launch (m_options.launch_info);
236 }
Greg Claytonabb33022011-11-08 02:43:13 +0000237
Greg Claytona2f74232011-02-24 22:24:29 +0000238 if (process == NULL)
239 {
Greg Clayton527154d2011-11-15 03:53:30 +0000240 result.SetError (error, "failed to launch or debug process");
Greg Claytona2f74232011-02-24 22:24:29 +0000241 return false;
242 }
Chris Lattner24943d22010-06-08 16:52:24 +0000243 }
Greg Claytonabb33022011-11-08 02:43:13 +0000244
Greg Clayton238c0a12010-09-18 01:14:36 +0000245 if (error.Success())
246 {
Greg Clayton940b1032011-02-23 00:35:02 +0000247 const char *archname = exe_module->GetArchitecture().GetArchitectureName();
Greg Claytonc1d37752010-10-18 01:45:30 +0000248
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000249 result.AppendMessageWithFormat ("Process %" PRIu64 " launched: '%s' (%s)\n", process->GetID(), filename, archname);
Greg Claytond8c62532010-10-07 04:19:01 +0000250 result.SetDidChangeProcessState (true);
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000251 if (m_options.launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
Greg Clayton238c0a12010-09-18 01:14:36 +0000252 {
Greg Claytond8c62532010-10-07 04:19:01 +0000253 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
Greg Clayton238c0a12010-09-18 01:14:36 +0000254 StateType state = process->WaitForProcessToStop (NULL);
255
256 if (state == eStateStopped)
257 {
Greg Claytond8c62532010-10-07 04:19:01 +0000258 error = process->Resume();
259 if (error.Success())
260 {
261 bool synchronous_execution = m_interpreter.GetSynchronous ();
262 if (synchronous_execution)
263 {
264 state = process->WaitForProcessToStop (NULL);
Greg Clayton20206082011-11-17 01:23:07 +0000265 const bool must_be_alive = true;
266 if (!StateIsStoppedState(state, must_be_alive))
Greg Clayton395fc332011-02-15 21:59:32 +0000267 {
Greg Clayton527154d2011-11-15 03:53:30 +0000268 result.AppendErrorWithFormat ("process isn't stopped: %s", StateAsCString(state));
Greg Clayton395fc332011-02-15 21:59:32 +0000269 }
Greg Claytond8c62532010-10-07 04:19:01 +0000270 result.SetDidChangeProcessState (true);
271 result.SetStatus (eReturnStatusSuccessFinishResult);
272 }
273 else
274 {
275 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
276 }
277 }
Greg Clayton395fc332011-02-15 21:59:32 +0000278 else
279 {
Greg Clayton527154d2011-11-15 03:53:30 +0000280 result.AppendErrorWithFormat ("process resume at entry point failed: %s", error.AsCString());
Greg Clayton395fc332011-02-15 21:59:32 +0000281 result.SetStatus (eReturnStatusFailed);
282 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000283 }
Greg Clayton395fc332011-02-15 21:59:32 +0000284 else
285 {
Greg Clayton527154d2011-11-15 03:53:30 +0000286 result.AppendErrorWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
Greg Clayton395fc332011-02-15 21:59:32 +0000287 result.SetStatus (eReturnStatusFailed);
288 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000289 }
290 }
Greg Clayton395fc332011-02-15 21:59:32 +0000291 else
292 {
Greg Claytona9eb8272011-07-02 21:07:54 +0000293 result.AppendErrorWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton395fc332011-02-15 21:59:32 +0000294 result.SetStatus (eReturnStatusFailed);
295 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000296
Chris Lattner24943d22010-06-08 16:52:24 +0000297 return result.Succeeded();
298 }
299
300protected:
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000301 ProcessLaunchCommandOptions m_options;
Chris Lattner24943d22010-06-08 16:52:24 +0000302};
303
304
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000305//#define SET1 LLDB_OPT_SET_1
306//#define SET2 LLDB_OPT_SET_2
307//#define SET3 LLDB_OPT_SET_3
308//
309//OptionDefinition
310//CommandObjectProcessLaunch::CommandOptions::g_option_table[] =
311//{
312//{ 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."},
Sean Callanan9a91ef62012-10-24 01:12:14 +0000313//{ SET1 , false, "stdin", 'i', required_argument, NULL, 0, eArgTypeDirectoryName, "Redirect stdin for the process to <path>."},
314//{ SET1 , false, "stdout", 'o', required_argument, NULL, 0, eArgTypeDirectoryName, "Redirect stdout for the process to <path>."},
315//{ SET1 , false, "stderr", 'e', required_argument, NULL, 0, eArgTypeDirectoryName, "Redirect stderr for the process to <path>."},
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000316//{ SET1 | SET2 | SET3, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
Sean Callanan9a91ef62012-10-24 01:12:14 +0000317//{ SET2 , false, "tty", 't', optional_argument, NULL, 0, eArgTypeDirectoryName, "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."},
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000318//{ SET3, false, "no-stdio", 'n', no_argument, NULL, 0, eArgTypeNone, "Do not set up for terminal I/O to go to running process."},
Sean Callanan9a91ef62012-10-24 01:12:14 +0000319//{ SET1 | SET2 | SET3, false, "working-dir", 'w', required_argument, NULL, 0, eArgTypeDirectoryName, "Set the current working directory to <path> when running the inferior."},
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000320//{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
321//};
322//
323//#undef SET1
324//#undef SET2
325//#undef SET3
Chris Lattner24943d22010-06-08 16:52:24 +0000326
327//-------------------------------------------------------------------------
328// CommandObjectProcessAttach
329//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000330#pragma mark CommandObjectProcessAttach
Jim Inghamda26bd22012-06-08 21:56:10 +0000331class CommandObjectProcessAttach : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000332{
333public:
334
Chris Lattner24943d22010-06-08 16:52:24 +0000335 class CommandOptions : public Options
336 {
337 public:
338
Greg Claytonf15996e2011-04-07 22:46:35 +0000339 CommandOptions (CommandInterpreter &interpreter) :
340 Options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000341 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000342 // Keep default values of all options in one place: OptionParsingStarting ()
343 OptionParsingStarting ();
Chris Lattner24943d22010-06-08 16:52:24 +0000344 }
345
346 ~CommandOptions ()
347 {
348 }
349
350 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000351 SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +0000352 {
353 Error error;
Greg Clayton6475c422012-12-04 00:32:51 +0000354 const int short_option = m_getopt_table[option_idx].val;
Chris Lattner24943d22010-06-08 16:52:24 +0000355 bool success = false;
356 switch (short_option)
357 {
Johnny Chen7c099972012-05-24 00:43:00 +0000358 case 'c':
359 attach_info.SetContinueOnceAttached(true);
360 break;
361
Chris Lattner24943d22010-06-08 16:52:24 +0000362 case 'p':
Chris Lattner24943d22010-06-08 16:52:24 +0000363 {
Greg Clayton527154d2011-11-15 03:53:30 +0000364 lldb::pid_t pid = Args::StringToUInt32 (option_arg, LLDB_INVALID_PROCESS_ID, 0, &success);
365 if (!success || pid == LLDB_INVALID_PROCESS_ID)
366 {
367 error.SetErrorStringWithFormat("invalid process ID '%s'", option_arg);
368 }
369 else
370 {
371 attach_info.SetProcessID (pid);
372 }
Chris Lattner24943d22010-06-08 16:52:24 +0000373 }
374 break;
375
376 case 'P':
Greg Clayton527154d2011-11-15 03:53:30 +0000377 attach_info.SetProcessPluginName (option_arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000378 break;
379
380 case 'n':
Greg Clayton527154d2011-11-15 03:53:30 +0000381 attach_info.GetExecutableFile().SetFile(option_arg, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000382 break;
383
384 case 'w':
Greg Clayton527154d2011-11-15 03:53:30 +0000385 attach_info.SetWaitForLaunch(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000386 break;
Jim Ingham3a458eb2012-07-20 21:37:13 +0000387
388 case 'i':
389 attach_info.SetIgnoreExisting(false);
390 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000391
392 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000393 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner24943d22010-06-08 16:52:24 +0000394 break;
395 }
396 return error;
397 }
398
399 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000400 OptionParsingStarting ()
Chris Lattner24943d22010-06-08 16:52:24 +0000401 {
Greg Clayton527154d2011-11-15 03:53:30 +0000402 attach_info.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000403 }
404
Greg Claytonb3448432011-03-24 21:19:54 +0000405 const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +0000406 GetDefinitions ()
407 {
408 return g_option_table;
409 }
410
Jim Ingham7508e732010-08-09 23:31:02 +0000411 virtual bool
Greg Claytonf15996e2011-04-07 22:46:35 +0000412 HandleOptionArgumentCompletion (Args &input,
Jim Ingham7508e732010-08-09 23:31:02 +0000413 int cursor_index,
414 int char_pos,
415 OptionElementVector &opt_element_vector,
416 int opt_element_index,
417 int match_start_point,
418 int max_return_elements,
419 bool &word_complete,
420 StringList &matches)
421 {
422 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
423 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
424
425 // We are only completing the name option for now...
426
Greg Claytonb3448432011-03-24 21:19:54 +0000427 const OptionDefinition *opt_defs = GetDefinitions();
Jim Ingham7508e732010-08-09 23:31:02 +0000428 if (opt_defs[opt_defs_index].short_option == 'n')
429 {
430 // Are we in the name?
431
432 // Look to see if there is a -P argument provided, and if so use that plugin, otherwise
433 // use the default plugin.
Jim Ingham7508e732010-08-09 23:31:02 +0000434
435 const char *partial_name = NULL;
436 partial_name = input.GetArgumentAtIndex(opt_arg_pos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000437
Greg Claytonb72d0f02011-04-12 05:54:46 +0000438 PlatformSP platform_sp (m_interpreter.GetPlatform (true));
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000439 if (platform_sp)
Jim Ingham7508e732010-08-09 23:31:02 +0000440 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000441 ProcessInstanceInfoList process_infos;
442 ProcessInstanceInfoMatch match_info;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000443 if (partial_name)
444 {
Greg Clayton527154d2011-11-15 03:53:30 +0000445 match_info.GetProcessInfo().GetExecutableFile().SetFile(partial_name, false);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000446 match_info.SetNameMatchType(eNameMatchStartsWith);
447 }
448 platform_sp->FindProcesses (match_info, process_infos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000449 const uint32_t num_matches = process_infos.GetSize();
450 if (num_matches > 0)
451 {
452 for (uint32_t i=0; i<num_matches; ++i)
453 {
454 matches.AppendString (process_infos.GetProcessNameAtIndex(i),
455 process_infos.GetProcessNameLengthAtIndex(i));
456 }
457 }
Jim Ingham7508e732010-08-09 23:31:02 +0000458 }
459 }
460
461 return false;
462 }
463
Chris Lattner24943d22010-06-08 16:52:24 +0000464 // Options table: Required for subclasses of Options.
465
Greg Claytonb3448432011-03-24 21:19:54 +0000466 static OptionDefinition g_option_table[];
Chris Lattner24943d22010-06-08 16:52:24 +0000467
468 // Instance variables to hold the values for command options.
469
Greg Clayton527154d2011-11-15 03:53:30 +0000470 ProcessAttachInfo attach_info;
Chris Lattner24943d22010-06-08 16:52:24 +0000471 };
472
Greg Clayton238c0a12010-09-18 01:14:36 +0000473 CommandObjectProcessAttach (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000474 CommandObjectParsed (interpreter,
475 "process attach",
476 "Attach to a process.",
477 "process attach <cmd-options>"),
Greg Claytonf15996e2011-04-07 22:46:35 +0000478 m_options (interpreter)
Jim Ingham7508e732010-08-09 23:31:02 +0000479 {
Jim Ingham7508e732010-08-09 23:31:02 +0000480 }
481
482 ~CommandObjectProcessAttach ()
483 {
484 }
485
Jim Inghamda26bd22012-06-08 21:56:10 +0000486 Options *
487 GetOptions ()
488 {
489 return &m_options;
490 }
491
492protected:
Jim Ingham7508e732010-08-09 23:31:02 +0000493 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000494 DoExecute (Args& command,
Jim Ingham7508e732010-08-09 23:31:02 +0000495 CommandReturnObject &result)
496 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000497 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Jim Inghamee940e22011-09-15 01:08:57 +0000498 // N.B. The attach should be synchronous. It doesn't help much to get the prompt back between initiating the attach
499 // and the target actually stopping. So even if the interpreter is set to be asynchronous, we wait for the stop
500 // ourselves here.
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000501
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000502 Process *process = m_exe_ctx.GetProcessPtr();
Greg Claytona2f74232011-02-24 22:24:29 +0000503 StateType state = eStateInvalid;
Jim Ingham7508e732010-08-09 23:31:02 +0000504 if (process)
505 {
Greg Claytona2f74232011-02-24 22:24:29 +0000506 state = process->GetState();
507 if (process->IsAlive() && state != eStateConnected)
Jim Ingham7508e732010-08-09 23:31:02 +0000508 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000509 result.AppendErrorWithFormat ("Process %" PRIu64 " is currently being debugged, kill the process before attaching.\n",
Jim Ingham7508e732010-08-09 23:31:02 +0000510 process->GetID());
511 result.SetStatus (eReturnStatusFailed);
512 return false;
513 }
514 }
515
516 if (target == NULL)
517 {
518 // If there isn't a current target create one.
519 TargetSP new_target_sp;
Jim Ingham7508e732010-08-09 23:31:02 +0000520 Error error;
521
Greg Clayton238c0a12010-09-18 01:14:36 +0000522 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
Greg Claytoned0a0fb2012-10-18 16:33:33 +0000523 NULL,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000524 NULL,
Greg Clayton238c0a12010-09-18 01:14:36 +0000525 false,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000526 NULL, // No platform options
Greg Clayton238c0a12010-09-18 01:14:36 +0000527 new_target_sp);
Jim Ingham7508e732010-08-09 23:31:02 +0000528 target = new_target_sp.get();
529 if (target == NULL || error.Fail())
530 {
Greg Claytone71e2582011-02-04 01:58:07 +0000531 result.AppendError(error.AsCString("Error creating target"));
Jim Ingham7508e732010-08-09 23:31:02 +0000532 return false;
533 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000534 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target);
Jim Ingham7508e732010-08-09 23:31:02 +0000535 }
536
537 // Record the old executable module, we want to issue a warning if the process of attaching changed the
538 // current executable (like somebody said "file foo" then attached to a PID whose executable was bar.)
539
540 ModuleSP old_exec_module_sp = target->GetExecutableModule();
541 ArchSpec old_arch_spec = target->GetArchitecture();
542
543 if (command.GetArgumentCount())
544 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000545 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 +0000546 result.SetStatus (eReturnStatusFailed);
547 }
548 else
549 {
Greg Claytona2f74232011-02-24 22:24:29 +0000550 if (state != eStateConnected)
551 {
Greg Clayton527154d2011-11-15 03:53:30 +0000552 const char *plugin_name = m_options.attach_info.GetProcessPluginName();
Greg Clayton46c9a352012-02-09 06:16:32 +0000553 process = target->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name, NULL).get();
Greg Claytona2f74232011-02-24 22:24:29 +0000554 }
Jim Ingham7508e732010-08-09 23:31:02 +0000555
556 if (process)
557 {
558 Error error;
Greg Clayton527154d2011-11-15 03:53:30 +0000559 // If no process info was specified, then use the target executable
560 // name as the process to attach to by default
561 if (!m_options.attach_info.ProcessInfoSpecified ())
Jim Ingham4805a1c2010-09-15 01:34:14 +0000562 {
563 if (old_exec_module_sp)
Greg Clayton1d1f39e2011-11-29 04:03:30 +0000564 m_options.attach_info.GetExecutableFile().GetFilename() = old_exec_module_sp->GetPlatformFileSpec().GetFilename();
Jim Ingham4805a1c2010-09-15 01:34:14 +0000565
Greg Clayton527154d2011-11-15 03:53:30 +0000566 if (!m_options.attach_info.ProcessInfoSpecified ())
567 {
568 error.SetErrorString ("no process specified, create a target with a file, or specify the --pid or --name command option");
569 }
570 }
571
572 if (error.Success())
573 {
574 error = process->Attach (m_options.attach_info);
575
Jim Ingham4805a1c2010-09-15 01:34:14 +0000576 if (error.Success())
577 {
578 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
579 }
Jim Ingham7508e732010-08-09 23:31:02 +0000580 else
581 {
Greg Clayton527154d2011-11-15 03:53:30 +0000582 result.AppendErrorWithFormat ("attach failed: %s\n", error.AsCString());
Jim Ingham4805a1c2010-09-15 01:34:14 +0000583 result.SetStatus (eReturnStatusFailed);
584 return false;
Jim Ingham7508e732010-08-09 23:31:02 +0000585 }
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000586 // If we're synchronous, wait for the stopped event and report that.
587 // Otherwise just return.
588 // FIXME: in the async case it will now be possible to get to the command
589 // interpreter with a state eStateAttaching. Make sure we handle that correctly.
Jim Inghamee940e22011-09-15 01:08:57 +0000590 StateType state = process->WaitForProcessToStop (NULL);
Greg Clayton527154d2011-11-15 03:53:30 +0000591
Jim Inghamee940e22011-09-15 01:08:57 +0000592 result.SetDidChangeProcessState (true);
Johnny Chen9986a3b2012-05-18 00:51:36 +0000593
594 if (state == eStateStopped)
595 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000596 result.AppendMessageWithFormat ("Process %" PRIu64 " %s\n", process->GetID(), StateAsCString (state));
Johnny Chen9986a3b2012-05-18 00:51:36 +0000597 result.SetStatus (eReturnStatusSuccessFinishNoResult);
598 }
599 else
600 {
601 result.AppendError ("attach failed: process did not stop (no such process or permission problem?)");
Jim Ingham5d90ade2012-07-27 23:57:19 +0000602 process->Destroy();
Johnny Chen9986a3b2012-05-18 00:51:36 +0000603 result.SetStatus (eReturnStatusFailed);
604 return false;
605 }
Jim Ingham7508e732010-08-09 23:31:02 +0000606 }
Jim Ingham7508e732010-08-09 23:31:02 +0000607 }
608 }
609
610 if (result.Succeeded())
611 {
612 // Okay, we're done. Last step is to warn if the executable module has changed:
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000613 char new_path[PATH_MAX];
Greg Clayton5beb99d2011-08-11 02:48:45 +0000614 ModuleSP new_exec_module_sp (target->GetExecutableModule());
Jim Ingham7508e732010-08-09 23:31:02 +0000615 if (!old_exec_module_sp)
616 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000617 // We might not have a module if we attached to a raw pid...
Greg Clayton5beb99d2011-08-11 02:48:45 +0000618 if (new_exec_module_sp)
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000619 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000620 new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000621 result.AppendMessageWithFormat("Executable module set to \"%s\".\n", new_path);
622 }
Jim Ingham7508e732010-08-09 23:31:02 +0000623 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000624 else if (old_exec_module_sp->GetFileSpec() != new_exec_module_sp->GetFileSpec())
Jim Ingham7508e732010-08-09 23:31:02 +0000625 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000626 char old_path[PATH_MAX];
Jim Ingham7508e732010-08-09 23:31:02 +0000627
Greg Clayton5beb99d2011-08-11 02:48:45 +0000628 old_exec_module_sp->GetFileSpec().GetPath (old_path, PATH_MAX);
629 new_exec_module_sp->GetFileSpec().GetPath (new_path, PATH_MAX);
Jim Ingham7508e732010-08-09 23:31:02 +0000630
631 result.AppendWarningWithFormat("Executable module changed from \"%s\" to \"%s\".\n",
632 old_path, new_path);
633 }
634
635 if (!old_arch_spec.IsValid())
636 {
Greg Clayton92cb9a92012-09-14 02:41:36 +0000637 result.AppendMessageWithFormat ("Architecture set to: %s.\n", target->GetArchitecture().GetTriple().getTriple().c_str());
Jim Ingham7508e732010-08-09 23:31:02 +0000638 }
Sean Callanan40e278c2012-12-13 22:07:14 +0000639 else if (!old_arch_spec.IsExactMatch(target->GetArchitecture()))
Jim Ingham7508e732010-08-09 23:31:02 +0000640 {
641 result.AppendWarningWithFormat("Architecture changed from %s to %s.\n",
Greg Clayton92cb9a92012-09-14 02:41:36 +0000642 old_arch_spec.GetTriple().getTriple().c_str(),
643 target->GetArchitecture().GetTriple().getTriple().c_str());
Jim Ingham7508e732010-08-09 23:31:02 +0000644 }
Johnny Chen7c099972012-05-24 00:43:00 +0000645
646 // This supports the use-case scenario of immediately continuing the process once attached.
647 if (m_options.attach_info.GetContinueOnceAttached())
Sean Callanan4336d932012-05-31 01:30:08 +0000648 m_interpreter.HandleCommand("process continue", eLazyBoolNo, result);
Jim Ingham7508e732010-08-09 23:31:02 +0000649 }
650 return result.Succeeded();
651 }
652
Chris Lattner24943d22010-06-08 16:52:24 +0000653 CommandOptions m_options;
654};
655
656
Greg Claytonb3448432011-03-24 21:19:54 +0000657OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000658CommandObjectProcessAttach::CommandOptions::g_option_table[] =
659{
Jim Ingham3a458eb2012-07-20 21:37:13 +0000660{ LLDB_OPT_SET_ALL, false, "continue",'c', no_argument, NULL, 0, eArgTypeNone, "Immediately continue the process once attached."},
661{ LLDB_OPT_SET_ALL, false, "plugin", 'P', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
662{ LLDB_OPT_SET_1, false, "pid", 'p', required_argument, NULL, 0, eArgTypePid, "The process ID of an existing process to attach to."},
663{ LLDB_OPT_SET_2, false, "name", 'n', required_argument, NULL, 0, eArgTypeProcessName, "The name of the process to attach to."},
664{ LLDB_OPT_SET_2, false, "include-existing", 'i', no_argument, NULL, 0, eArgTypeNone, "Include existing processes when doing attach -w."},
665{ 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 +0000666{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000667};
668
669//-------------------------------------------------------------------------
670// CommandObjectProcessContinue
671//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000672#pragma mark CommandObjectProcessContinue
Chris Lattner24943d22010-06-08 16:52:24 +0000673
Jim Inghamda26bd22012-06-08 21:56:10 +0000674class CommandObjectProcessContinue : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000675{
676public:
677
Greg Clayton238c0a12010-09-18 01:14:36 +0000678 CommandObjectProcessContinue (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000679 CommandObjectParsed (interpreter,
680 "process continue",
681 "Continue execution of all threads in the current process.",
682 "process continue",
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000683 eFlagRequiresProcess |
684 eFlagTryTargetAPILock |
685 eFlagProcessMustBeLaunched |
686 eFlagProcessMustBePaused ),
Jim Ingham124e6902012-08-11 01:27:55 +0000687 m_options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000688 {
689 }
690
691
692 ~CommandObjectProcessContinue ()
693 {
694 }
695
Jim Inghamda26bd22012-06-08 21:56:10 +0000696protected:
Jim Ingham124e6902012-08-11 01:27:55 +0000697
698 class CommandOptions : public Options
699 {
700 public:
701
702 CommandOptions (CommandInterpreter &interpreter) :
703 Options(interpreter)
704 {
705 // Keep default values of all options in one place: OptionParsingStarting ()
706 OptionParsingStarting ();
707 }
708
709 ~CommandOptions ()
710 {
711 }
712
713 Error
714 SetOptionValue (uint32_t option_idx, const char *option_arg)
715 {
716 Error error;
Greg Clayton6475c422012-12-04 00:32:51 +0000717 const int short_option = m_getopt_table[option_idx].val;
Jim Ingham124e6902012-08-11 01:27:55 +0000718 bool success = false;
719 switch (short_option)
720 {
721 case 'i':
722 m_ignore = Args::StringToUInt32 (option_arg, 0, 0, &success);
723 if (!success)
724 error.SetErrorStringWithFormat ("invalid value for ignore option: \"%s\", should be a number.", option_arg);
725 break;
726
727 default:
728 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
729 break;
730 }
731 return error;
732 }
733
734 void
735 OptionParsingStarting ()
736 {
737 m_ignore = 0;
738 }
739
740 const OptionDefinition*
741 GetDefinitions ()
742 {
743 return g_option_table;
744 }
745
746 // Options table: Required for subclasses of Options.
747
748 static OptionDefinition g_option_table[];
749
750 uint32_t m_ignore;
751 };
752
Chris Lattner24943d22010-06-08 16:52:24 +0000753 bool
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000754 DoExecute (Args& command, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +0000755 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000756 Process *process = m_exe_ctx.GetProcessPtr();
Greg Clayton238c0a12010-09-18 01:14:36 +0000757 bool synchronous_execution = m_interpreter.GetSynchronous ();
Chris Lattner24943d22010-06-08 16:52:24 +0000758 StateType state = process->GetState();
759 if (state == eStateStopped)
760 {
761 if (command.GetArgumentCount() != 0)
762 {
763 result.AppendErrorWithFormat ("The '%s' command does not take any arguments.\n", m_cmd_name.c_str());
764 result.SetStatus (eReturnStatusFailed);
765 return false;
766 }
767
Jim Ingham124e6902012-08-11 01:27:55 +0000768 if (m_options.m_ignore > 0)
769 {
770 ThreadSP sel_thread_sp(process->GetThreadList().GetSelectedThread());
771 if (sel_thread_sp)
772 {
773 StopInfoSP stop_info_sp = sel_thread_sp->GetStopInfo();
774 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint)
775 {
776 uint64_t bp_site_id = stop_info_sp->GetValue();
777 BreakpointSiteSP bp_site_sp(process->GetBreakpointSiteList().FindByID(bp_site_id));
778 if (bp_site_sp)
779 {
780 uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
781 for (uint32_t i = 0; i < num_owners; i++)
782 {
783 Breakpoint &bp_ref = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
784 if (!bp_ref.IsInternal())
785 {
786 bp_ref.SetIgnoreCount(m_options.m_ignore);
787 }
788 }
789 }
790 }
791 }
792 }
793
Jim Inghamb9950592012-09-10 20:50:15 +0000794 { // Scope for thread list mutex:
795 Mutex::Locker locker (process->GetThreadList().GetMutex());
796 const uint32_t num_threads = process->GetThreadList().GetSize();
Chris Lattner24943d22010-06-08 16:52:24 +0000797
Jim Inghamb9950592012-09-10 20:50:15 +0000798 // Set the actions that the threads should each take when resuming
799 for (uint32_t idx=0; idx<num_threads; ++idx)
800 {
801 process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState (eStateRunning);
802 }
Chris Lattner24943d22010-06-08 16:52:24 +0000803 }
Jim Inghamb9950592012-09-10 20:50:15 +0000804
Chris Lattner24943d22010-06-08 16:52:24 +0000805 Error error(process->Resume());
806 if (error.Success())
807 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000808 result.AppendMessageWithFormat ("Process %" PRIu64 " resuming\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000809 if (synchronous_execution)
810 {
Greg Claytonbef15832010-07-14 00:18:15 +0000811 state = process->WaitForProcessToStop (NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000812
813 result.SetDidChangeProcessState (true);
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000814 result.AppendMessageWithFormat ("Process %" PRIu64 " %s\n", process->GetID(), StateAsCString (state));
Chris Lattner24943d22010-06-08 16:52:24 +0000815 result.SetStatus (eReturnStatusSuccessFinishNoResult);
816 }
817 else
818 {
819 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
820 }
821 }
822 else
823 {
824 result.AppendErrorWithFormat("Failed to resume process: %s.\n", error.AsCString());
825 result.SetStatus (eReturnStatusFailed);
826 }
827 }
828 else
829 {
830 result.AppendErrorWithFormat ("Process cannot be continued from its current state (%s).\n",
831 StateAsCString(state));
832 result.SetStatus (eReturnStatusFailed);
833 }
834 return result.Succeeded();
835 }
Jim Ingham124e6902012-08-11 01:27:55 +0000836
837 Options *
838 GetOptions ()
839 {
840 return &m_options;
841 }
842
843 CommandOptions m_options;
844
845};
846
847OptionDefinition
848CommandObjectProcessContinue::CommandOptions::g_option_table[] =
849{
850{ LLDB_OPT_SET_ALL, false, "ignore-count",'i', required_argument, NULL, 0, eArgTypeUnsignedInteger,
851 "Ignore <N> crossings of the breakpoint (if it exists) for the currently selected thread."},
852{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000853};
854
855//-------------------------------------------------------------------------
856// CommandObjectProcessDetach
857//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000858#pragma mark CommandObjectProcessDetach
Chris Lattner24943d22010-06-08 16:52:24 +0000859
Jim Inghamda26bd22012-06-08 21:56:10 +0000860class CommandObjectProcessDetach : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000861{
862public:
863
Greg Clayton238c0a12010-09-18 01:14:36 +0000864 CommandObjectProcessDetach (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000865 CommandObjectParsed (interpreter,
866 "process detach",
867 "Detach from the current process being debugged.",
868 "process detach",
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000869 eFlagRequiresProcess |
870 eFlagTryTargetAPILock |
Jim Inghamda26bd22012-06-08 21:56:10 +0000871 eFlagProcessMustBeLaunched)
Chris Lattner24943d22010-06-08 16:52:24 +0000872 {
873 }
874
875 ~CommandObjectProcessDetach ()
876 {
877 }
878
Jim Inghamda26bd22012-06-08 21:56:10 +0000879protected:
Chris Lattner24943d22010-06-08 16:52:24 +0000880 bool
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000881 DoExecute (Args& command, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +0000882 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000883 Process *process = m_exe_ctx.GetProcessPtr();
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000884 result.AppendMessageWithFormat ("Detaching from process %" PRIu64 "\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000885 Error error (process->Detach());
886 if (error.Success())
887 {
888 result.SetStatus (eReturnStatusSuccessFinishResult);
889 }
890 else
891 {
892 result.AppendErrorWithFormat ("Detach failed: %s\n", error.AsCString());
893 result.SetStatus (eReturnStatusFailed);
894 return false;
895 }
896 return result.Succeeded();
897 }
898};
899
900//-------------------------------------------------------------------------
Greg Claytone71e2582011-02-04 01:58:07 +0000901// CommandObjectProcessConnect
902//-------------------------------------------------------------------------
903#pragma mark CommandObjectProcessConnect
904
Jim Inghamda26bd22012-06-08 21:56:10 +0000905class CommandObjectProcessConnect : public CommandObjectParsed
Greg Claytone71e2582011-02-04 01:58:07 +0000906{
907public:
908
909 class CommandOptions : public Options
910 {
911 public:
912
Greg Claytonf15996e2011-04-07 22:46:35 +0000913 CommandOptions (CommandInterpreter &interpreter) :
914 Options(interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000915 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000916 // Keep default values of all options in one place: OptionParsingStarting ()
917 OptionParsingStarting ();
Greg Claytone71e2582011-02-04 01:58:07 +0000918 }
919
920 ~CommandOptions ()
921 {
922 }
923
924 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000925 SetOptionValue (uint32_t option_idx, const char *option_arg)
Greg Claytone71e2582011-02-04 01:58:07 +0000926 {
927 Error error;
Greg Clayton6475c422012-12-04 00:32:51 +0000928 const int short_option = m_getopt_table[option_idx].val;
Greg Claytone71e2582011-02-04 01:58:07 +0000929
930 switch (short_option)
931 {
932 case 'p':
933 plugin_name.assign (option_arg);
934 break;
935
936 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000937 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Greg Claytone71e2582011-02-04 01:58:07 +0000938 break;
939 }
940 return error;
941 }
942
943 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000944 OptionParsingStarting ()
Greg Claytone71e2582011-02-04 01:58:07 +0000945 {
Greg Claytone71e2582011-02-04 01:58:07 +0000946 plugin_name.clear();
947 }
948
Greg Claytonb3448432011-03-24 21:19:54 +0000949 const OptionDefinition*
Greg Claytone71e2582011-02-04 01:58:07 +0000950 GetDefinitions ()
951 {
952 return g_option_table;
953 }
954
955 // Options table: Required for subclasses of Options.
956
Greg Claytonb3448432011-03-24 21:19:54 +0000957 static OptionDefinition g_option_table[];
Greg Claytone71e2582011-02-04 01:58:07 +0000958
959 // Instance variables to hold the values for command options.
960
961 std::string plugin_name;
962 };
963
964 CommandObjectProcessConnect (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000965 CommandObjectParsed (interpreter,
966 "process connect",
967 "Connect to a remote debug service.",
968 "process connect <remote-url>",
969 0),
Greg Claytonf15996e2011-04-07 22:46:35 +0000970 m_options (interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000971 {
972 }
973
974 ~CommandObjectProcessConnect ()
975 {
976 }
977
978
Jim Inghamda26bd22012-06-08 21:56:10 +0000979 Options *
980 GetOptions ()
981 {
982 return &m_options;
983 }
984
985protected:
Greg Claytone71e2582011-02-04 01:58:07 +0000986 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000987 DoExecute (Args& command,
Greg Claytone71e2582011-02-04 01:58:07 +0000988 CommandReturnObject &result)
989 {
990
991 TargetSP target_sp (m_interpreter.GetDebugger().GetSelectedTarget());
992 Error error;
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000993 Process *process = m_exe_ctx.GetProcessPtr();
Greg Claytone71e2582011-02-04 01:58:07 +0000994 if (process)
995 {
996 if (process->IsAlive())
997 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000998 result.AppendErrorWithFormat ("Process %" PRIu64 " is currently being debugged, kill the process before connecting.\n",
Greg Claytone71e2582011-02-04 01:58:07 +0000999 process->GetID());
1000 result.SetStatus (eReturnStatusFailed);
1001 return false;
1002 }
1003 }
1004
1005 if (!target_sp)
1006 {
1007 // If there isn't a current target create one.
Greg Claytone71e2582011-02-04 01:58:07 +00001008
1009 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
Greg Claytoned0a0fb2012-10-18 16:33:33 +00001010 NULL,
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001011 NULL,
Greg Claytone71e2582011-02-04 01:58:07 +00001012 false,
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001013 NULL, // No platform options
Greg Claytone71e2582011-02-04 01:58:07 +00001014 target_sp);
1015 if (!target_sp || error.Fail())
1016 {
1017 result.AppendError(error.AsCString("Error creating target"));
1018 result.SetStatus (eReturnStatusFailed);
1019 return false;
1020 }
1021 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target_sp.get());
1022 }
1023
1024 if (command.GetArgumentCount() == 1)
1025 {
1026 const char *plugin_name = NULL;
1027 if (!m_options.plugin_name.empty())
1028 plugin_name = m_options.plugin_name.c_str();
1029
1030 const char *remote_url = command.GetArgumentAtIndex(0);
Greg Clayton46c9a352012-02-09 06:16:32 +00001031 process = target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name, NULL).get();
Greg Claytone71e2582011-02-04 01:58:07 +00001032
1033 if (process)
1034 {
Jason Molendafac2e622012-09-29 04:02:01 +00001035 error = process->ConnectRemote (&process->GetTarget().GetDebugger().GetOutputStream(), remote_url);
Greg Claytone71e2582011-02-04 01:58:07 +00001036
1037 if (error.Fail())
1038 {
1039 result.AppendError(error.AsCString("Remote connect failed"));
1040 result.SetStatus (eReturnStatusFailed);
Greg Clayton0cbb93b2012-03-31 00:10:30 +00001041 target_sp->DeleteCurrentProcess();
Greg Claytone71e2582011-02-04 01:58:07 +00001042 return false;
1043 }
1044 }
1045 else
1046 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001047 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",
Daniel Maleadd068882012-12-18 20:00:40 +00001048 remote_url);
Greg Claytone71e2582011-02-04 01:58:07 +00001049 result.SetStatus (eReturnStatusFailed);
1050 }
1051 }
1052 else
1053 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001054 result.AppendErrorWithFormat ("'%s' takes exactly one argument:\nUsage: %s\n",
Greg Claytone71e2582011-02-04 01:58:07 +00001055 m_cmd_name.c_str(),
1056 m_cmd_syntax.c_str());
1057 result.SetStatus (eReturnStatusFailed);
1058 }
1059 return result.Succeeded();
1060 }
Greg Claytone71e2582011-02-04 01:58:07 +00001061
1062 CommandOptions m_options;
1063};
1064
Greg Claytonb3448432011-03-24 21:19:54 +00001065OptionDefinition
Greg Claytone71e2582011-02-04 01:58:07 +00001066CommandObjectProcessConnect::CommandOptions::g_option_table[] =
1067{
1068 { LLDB_OPT_SET_ALL, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
1069 { 0, false, NULL, 0 , 0, NULL, 0, eArgTypeNone, NULL }
1070};
1071
1072//-------------------------------------------------------------------------
Greg Clayton13193d52012-10-13 02:07:45 +00001073// CommandObjectProcessPlugin
1074//-------------------------------------------------------------------------
1075#pragma mark CommandObjectProcessPlugin
1076
1077class CommandObjectProcessPlugin : public CommandObjectProxy
1078{
1079public:
1080
1081 CommandObjectProcessPlugin (CommandInterpreter &interpreter) :
1082 CommandObjectProxy (interpreter,
1083 "process plugin",
1084 "Send a custom command to the current process plug-in.",
1085 "process plugin <args>",
1086 0)
1087 {
1088 }
1089
1090 ~CommandObjectProcessPlugin ()
1091 {
1092 }
1093
1094 virtual CommandObject *
1095 GetProxyCommandObject()
1096 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00001097 Process *process = m_exe_ctx.GetProcessPtr();
Greg Clayton13193d52012-10-13 02:07:45 +00001098 if (process)
1099 return process->GetPluginCommandObject();
1100 return NULL;
1101 }
1102};
1103
1104
1105//-------------------------------------------------------------------------
Greg Clayton0baa3942010-11-04 01:54:29 +00001106// CommandObjectProcessLoad
1107//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001108#pragma mark CommandObjectProcessLoad
Greg Clayton0baa3942010-11-04 01:54:29 +00001109
Jim Inghamda26bd22012-06-08 21:56:10 +00001110class CommandObjectProcessLoad : public CommandObjectParsed
Greg Clayton0baa3942010-11-04 01:54:29 +00001111{
1112public:
1113
1114 CommandObjectProcessLoad (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001115 CommandObjectParsed (interpreter,
1116 "process load",
1117 "Load a shared library into the current process.",
1118 "process load <filename> [<filename> ...]",
Greg Claytonea0bb4d2013-01-09 19:44:40 +00001119 eFlagRequiresProcess |
1120 eFlagTryTargetAPILock |
1121 eFlagProcessMustBeLaunched |
1122 eFlagProcessMustBePaused )
Greg Clayton0baa3942010-11-04 01:54:29 +00001123 {
1124 }
1125
1126 ~CommandObjectProcessLoad ()
1127 {
1128 }
1129
Jim Inghamda26bd22012-06-08 21:56:10 +00001130protected:
Greg Clayton0baa3942010-11-04 01:54:29 +00001131 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001132 DoExecute (Args& command,
Greg Clayton0baa3942010-11-04 01:54:29 +00001133 CommandReturnObject &result)
1134 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00001135 Process *process = m_exe_ctx.GetProcessPtr();
Greg Clayton0baa3942010-11-04 01:54:29 +00001136
1137 const uint32_t argc = command.GetArgumentCount();
1138
1139 for (uint32_t i=0; i<argc; ++i)
1140 {
1141 Error error;
1142 const char *image_path = command.GetArgumentAtIndex(i);
1143 FileSpec image_spec (image_path, false);
Greg Claytonf2bf8702011-08-11 16:25:18 +00001144 process->GetTarget().GetPlatform()->ResolveRemotePath(image_spec, image_spec);
Greg Clayton0baa3942010-11-04 01:54:29 +00001145 uint32_t image_token = process->LoadImage(image_spec, error);
1146 if (image_token != LLDB_INVALID_IMAGE_TOKEN)
1147 {
1148 result.AppendMessageWithFormat ("Loading \"%s\"...ok\nImage %u loaded.\n", image_path, image_token);
1149 result.SetStatus (eReturnStatusSuccessFinishResult);
1150 }
1151 else
1152 {
1153 result.AppendErrorWithFormat ("failed to load '%s': %s", image_path, error.AsCString());
1154 result.SetStatus (eReturnStatusFailed);
1155 }
1156 }
1157 return result.Succeeded();
1158 }
1159};
1160
1161
1162//-------------------------------------------------------------------------
1163// CommandObjectProcessUnload
1164//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001165#pragma mark CommandObjectProcessUnload
Greg Clayton0baa3942010-11-04 01:54:29 +00001166
Jim Inghamda26bd22012-06-08 21:56:10 +00001167class CommandObjectProcessUnload : public CommandObjectParsed
Greg Clayton0baa3942010-11-04 01:54:29 +00001168{
1169public:
1170
1171 CommandObjectProcessUnload (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001172 CommandObjectParsed (interpreter,
1173 "process unload",
1174 "Unload a shared library from the current process using the index returned by a previous call to \"process load\".",
1175 "process unload <index>",
Greg Claytonea0bb4d2013-01-09 19:44:40 +00001176 eFlagRequiresProcess |
1177 eFlagTryTargetAPILock |
1178 eFlagProcessMustBeLaunched |
1179 eFlagProcessMustBePaused )
Greg Clayton0baa3942010-11-04 01:54:29 +00001180 {
1181 }
1182
1183 ~CommandObjectProcessUnload ()
1184 {
1185 }
1186
Jim Inghamda26bd22012-06-08 21:56:10 +00001187protected:
Greg Clayton0baa3942010-11-04 01:54:29 +00001188 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001189 DoExecute (Args& command,
Greg Clayton0baa3942010-11-04 01:54:29 +00001190 CommandReturnObject &result)
1191 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00001192 Process *process = m_exe_ctx.GetProcessPtr();
Greg Clayton0baa3942010-11-04 01:54:29 +00001193
1194 const uint32_t argc = command.GetArgumentCount();
1195
1196 for (uint32_t i=0; i<argc; ++i)
1197 {
1198 const char *image_token_cstr = command.GetArgumentAtIndex(i);
1199 uint32_t image_token = Args::StringToUInt32(image_token_cstr, LLDB_INVALID_IMAGE_TOKEN, 0);
1200 if (image_token == LLDB_INVALID_IMAGE_TOKEN)
1201 {
1202 result.AppendErrorWithFormat ("invalid image index argument '%s'", image_token_cstr);
1203 result.SetStatus (eReturnStatusFailed);
1204 break;
1205 }
1206 else
1207 {
1208 Error error (process->UnloadImage(image_token));
1209 if (error.Success())
1210 {
1211 result.AppendMessageWithFormat ("Unloading shared library with index %u...ok\n", image_token);
1212 result.SetStatus (eReturnStatusSuccessFinishResult);
1213 }
1214 else
1215 {
1216 result.AppendErrorWithFormat ("failed to unload image: %s", error.AsCString());
1217 result.SetStatus (eReturnStatusFailed);
1218 break;
1219 }
1220 }
1221 }
1222 return result.Succeeded();
1223 }
1224};
1225
1226//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001227// CommandObjectProcessSignal
1228//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001229#pragma mark CommandObjectProcessSignal
Chris Lattner24943d22010-06-08 16:52:24 +00001230
Jim Inghamda26bd22012-06-08 21:56:10 +00001231class CommandObjectProcessSignal : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001232{
1233public:
1234
Greg Clayton238c0a12010-09-18 01:14:36 +00001235 CommandObjectProcessSignal (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001236 CommandObjectParsed (interpreter,
1237 "process signal",
1238 "Send a UNIX signal to the current process being debugged.",
Greg Claytonea0bb4d2013-01-09 19:44:40 +00001239 NULL,
1240 eFlagRequiresProcess | eFlagTryTargetAPILock)
Chris Lattner24943d22010-06-08 16:52:24 +00001241 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001242 CommandArgumentEntry arg;
1243 CommandArgumentData signal_arg;
1244
1245 // Define the first (and only) variant of this arg.
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001246 signal_arg.arg_type = eArgTypeUnixSignal;
Caroline Tice43b014a2010-10-04 22:28:36 +00001247 signal_arg.arg_repetition = eArgRepeatPlain;
1248
1249 // There is only one variant this argument could be; put it into the argument entry.
1250 arg.push_back (signal_arg);
1251
1252 // Push the data for the first argument into the m_arguments vector.
1253 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001254 }
1255
1256 ~CommandObjectProcessSignal ()
1257 {
1258 }
1259
Jim Inghamda26bd22012-06-08 21:56:10 +00001260protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001261 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001262 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001263 CommandReturnObject &result)
1264 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00001265 Process *process = m_exe_ctx.GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001266
1267 if (command.GetArgumentCount() == 1)
1268 {
Greg Clayton8f6be2a2010-10-09 01:40:57 +00001269 int signo = LLDB_INVALID_SIGNAL_NUMBER;
1270
1271 const char *signal_name = command.GetArgumentAtIndex(0);
1272 if (::isxdigit (signal_name[0]))
1273 signo = Args::StringToSInt32(signal_name, LLDB_INVALID_SIGNAL_NUMBER, 0);
1274 else
1275 signo = process->GetUnixSignals().GetSignalNumberFromName (signal_name);
1276
1277 if (signo == LLDB_INVALID_SIGNAL_NUMBER)
Chris Lattner24943d22010-06-08 16:52:24 +00001278 {
1279 result.AppendErrorWithFormat ("Invalid signal argument '%s'.\n", command.GetArgumentAtIndex(0));
1280 result.SetStatus (eReturnStatusFailed);
1281 }
1282 else
1283 {
1284 Error error (process->Signal (signo));
1285 if (error.Success())
1286 {
1287 result.SetStatus (eReturnStatusSuccessFinishResult);
1288 }
1289 else
1290 {
1291 result.AppendErrorWithFormat ("Failed to send signal %i: %s\n", signo, error.AsCString());
1292 result.SetStatus (eReturnStatusFailed);
1293 }
1294 }
1295 }
1296 else
1297 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001298 result.AppendErrorWithFormat("'%s' takes exactly one signal number argument:\nUsage: %s\n", m_cmd_name.c_str(),
Chris Lattner24943d22010-06-08 16:52:24 +00001299 m_cmd_syntax.c_str());
1300 result.SetStatus (eReturnStatusFailed);
1301 }
1302 return result.Succeeded();
1303 }
1304};
1305
1306
1307//-------------------------------------------------------------------------
1308// CommandObjectProcessInterrupt
1309//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001310#pragma mark CommandObjectProcessInterrupt
Chris Lattner24943d22010-06-08 16:52:24 +00001311
Jim Inghamda26bd22012-06-08 21:56:10 +00001312class CommandObjectProcessInterrupt : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001313{
1314public:
1315
1316
Greg Clayton238c0a12010-09-18 01:14:36 +00001317 CommandObjectProcessInterrupt (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001318 CommandObjectParsed (interpreter,
1319 "process interrupt",
1320 "Interrupt the current process being debugged.",
1321 "process interrupt",
Greg Claytonea0bb4d2013-01-09 19:44:40 +00001322 eFlagRequiresProcess |
1323 eFlagTryTargetAPILock |
Jim Inghamda26bd22012-06-08 21:56:10 +00001324 eFlagProcessMustBeLaunched)
Chris Lattner24943d22010-06-08 16:52:24 +00001325 {
1326 }
1327
1328 ~CommandObjectProcessInterrupt ()
1329 {
1330 }
1331
Jim Inghamda26bd22012-06-08 21:56:10 +00001332protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001333 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001334 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001335 CommandReturnObject &result)
1336 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00001337 Process *process = m_exe_ctx.GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001338 if (process == NULL)
1339 {
1340 result.AppendError ("no process to halt");
1341 result.SetStatus (eReturnStatusFailed);
1342 return false;
1343 }
1344
1345 if (command.GetArgumentCount() == 0)
1346 {
1347 Error error(process->Halt ());
1348 if (error.Success())
1349 {
1350 result.SetStatus (eReturnStatusSuccessFinishResult);
1351
1352 // Maybe we should add a "SuspendThreadPlans so we
1353 // can halt, and keep in place all the current thread plans.
1354 process->GetThreadList().DiscardThreadPlans();
1355 }
1356 else
1357 {
1358 result.AppendErrorWithFormat ("Failed to halt process: %s\n", error.AsCString());
1359 result.SetStatus (eReturnStatusFailed);
1360 }
1361 }
1362 else
1363 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001364 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner24943d22010-06-08 16:52:24 +00001365 m_cmd_name.c_str(),
1366 m_cmd_syntax.c_str());
1367 result.SetStatus (eReturnStatusFailed);
1368 }
1369 return result.Succeeded();
1370 }
1371};
1372
1373//-------------------------------------------------------------------------
1374// CommandObjectProcessKill
1375//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001376#pragma mark CommandObjectProcessKill
Chris Lattner24943d22010-06-08 16:52:24 +00001377
Jim Inghamda26bd22012-06-08 21:56:10 +00001378class CommandObjectProcessKill : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001379{
1380public:
1381
Greg Clayton238c0a12010-09-18 01:14:36 +00001382 CommandObjectProcessKill (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001383 CommandObjectParsed (interpreter,
1384 "process kill",
1385 "Terminate the current process being debugged.",
1386 "process kill",
Greg Claytonea0bb4d2013-01-09 19:44:40 +00001387 eFlagRequiresProcess |
1388 eFlagTryTargetAPILock |
Jim Inghamda26bd22012-06-08 21:56:10 +00001389 eFlagProcessMustBeLaunched)
Chris Lattner24943d22010-06-08 16:52:24 +00001390 {
1391 }
1392
1393 ~CommandObjectProcessKill ()
1394 {
1395 }
1396
Jim Inghamda26bd22012-06-08 21:56:10 +00001397protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001398 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001399 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001400 CommandReturnObject &result)
1401 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00001402 Process *process = m_exe_ctx.GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001403 if (process == NULL)
1404 {
1405 result.AppendError ("no process to kill");
1406 result.SetStatus (eReturnStatusFailed);
1407 return false;
1408 }
1409
1410 if (command.GetArgumentCount() == 0)
1411 {
1412 Error error (process->Destroy());
1413 if (error.Success())
1414 {
1415 result.SetStatus (eReturnStatusSuccessFinishResult);
1416 }
1417 else
1418 {
1419 result.AppendErrorWithFormat ("Failed to kill process: %s\n", error.AsCString());
1420 result.SetStatus (eReturnStatusFailed);
1421 }
1422 }
1423 else
1424 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001425 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner24943d22010-06-08 16:52:24 +00001426 m_cmd_name.c_str(),
1427 m_cmd_syntax.c_str());
1428 result.SetStatus (eReturnStatusFailed);
1429 }
1430 return result.Succeeded();
1431 }
1432};
1433
1434//-------------------------------------------------------------------------
Jim Ingham41313fc2010-06-18 01:23:09 +00001435// CommandObjectProcessStatus
1436//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001437#pragma mark CommandObjectProcessStatus
1438
Jim Inghamda26bd22012-06-08 21:56:10 +00001439class CommandObjectProcessStatus : public CommandObjectParsed
Jim Ingham41313fc2010-06-18 01:23:09 +00001440{
1441public:
Greg Clayton238c0a12010-09-18 01:14:36 +00001442 CommandObjectProcessStatus (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001443 CommandObjectParsed (interpreter,
1444 "process status",
1445 "Show the current status and location of executing process.",
1446 "process status",
Greg Claytonea0bb4d2013-01-09 19:44:40 +00001447 eFlagRequiresProcess | eFlagTryTargetAPILock)
Jim Ingham41313fc2010-06-18 01:23:09 +00001448 {
1449 }
1450
1451 ~CommandObjectProcessStatus()
1452 {
1453 }
1454
1455
1456 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001457 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham41313fc2010-06-18 01:23:09 +00001458 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001459 Stream &strm = result.GetOutputStream();
Jim Ingham41313fc2010-06-18 01:23:09 +00001460 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Claytonea0bb4d2013-01-09 19:44:40 +00001461 // No need to check "process" for validity as eFlagRequiresProcess ensures it is valid
1462 Process *process = m_exe_ctx.GetProcessPtr();
1463 const bool only_threads_with_stop_reason = true;
1464 const uint32_t start_frame = 0;
1465 const uint32_t num_frames = 1;
1466 const uint32_t num_frames_with_source = 1;
1467 process->GetStatus(strm);
1468 process->GetThreadStatus (strm,
1469 only_threads_with_stop_reason,
1470 start_frame,
1471 num_frames,
1472 num_frames_with_source);
Jim Ingham41313fc2010-06-18 01:23:09 +00001473 return result.Succeeded();
1474 }
1475};
1476
1477//-------------------------------------------------------------------------
Caroline Tice23d6f272010-10-13 20:44:39 +00001478// CommandObjectProcessHandle
1479//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001480#pragma mark CommandObjectProcessHandle
Caroline Tice23d6f272010-10-13 20:44:39 +00001481
Jim Inghamda26bd22012-06-08 21:56:10 +00001482class CommandObjectProcessHandle : public CommandObjectParsed
Caroline Tice23d6f272010-10-13 20:44:39 +00001483{
1484public:
1485
1486 class CommandOptions : public Options
1487 {
1488 public:
1489
Greg Claytonf15996e2011-04-07 22:46:35 +00001490 CommandOptions (CommandInterpreter &interpreter) :
1491 Options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001492 {
Greg Clayton143fcc32011-04-13 00:18:08 +00001493 OptionParsingStarting ();
Caroline Tice23d6f272010-10-13 20:44:39 +00001494 }
1495
1496 ~CommandOptions ()
1497 {
1498 }
1499
1500 Error
Greg Clayton143fcc32011-04-13 00:18:08 +00001501 SetOptionValue (uint32_t option_idx, const char *option_arg)
Caroline Tice23d6f272010-10-13 20:44:39 +00001502 {
1503 Error error;
Greg Clayton6475c422012-12-04 00:32:51 +00001504 const int short_option = m_getopt_table[option_idx].val;
Caroline Tice23d6f272010-10-13 20:44:39 +00001505
1506 switch (short_option)
1507 {
1508 case 's':
1509 stop = option_arg;
1510 break;
1511 case 'n':
1512 notify = option_arg;
1513 break;
1514 case 'p':
1515 pass = option_arg;
1516 break;
1517 default:
Greg Clayton9c236732011-10-26 00:56:27 +00001518 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Caroline Tice23d6f272010-10-13 20:44:39 +00001519 break;
1520 }
1521 return error;
1522 }
1523
1524 void
Greg Clayton143fcc32011-04-13 00:18:08 +00001525 OptionParsingStarting ()
Caroline Tice23d6f272010-10-13 20:44:39 +00001526 {
Caroline Tice23d6f272010-10-13 20:44:39 +00001527 stop.clear();
1528 notify.clear();
1529 pass.clear();
1530 }
1531
Greg Claytonb3448432011-03-24 21:19:54 +00001532 const OptionDefinition*
Caroline Tice23d6f272010-10-13 20:44:39 +00001533 GetDefinitions ()
1534 {
1535 return g_option_table;
1536 }
1537
1538 // Options table: Required for subclasses of Options.
1539
Greg Claytonb3448432011-03-24 21:19:54 +00001540 static OptionDefinition g_option_table[];
Caroline Tice23d6f272010-10-13 20:44:39 +00001541
1542 // Instance variables to hold the values for command options.
1543
1544 std::string stop;
1545 std::string notify;
1546 std::string pass;
1547 };
1548
1549
1550 CommandObjectProcessHandle (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001551 CommandObjectParsed (interpreter,
1552 "process handle",
1553 "Show or update what the process and debugger should do with various signals received from the OS.",
1554 NULL),
Greg Claytonf15996e2011-04-07 22:46:35 +00001555 m_options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001556 {
Caroline Ticee7471982010-10-14 21:31:13 +00001557 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 +00001558 CommandArgumentEntry arg;
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001559 CommandArgumentData signal_arg;
Caroline Tice23d6f272010-10-13 20:44:39 +00001560
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001561 signal_arg.arg_type = eArgTypeUnixSignal;
1562 signal_arg.arg_repetition = eArgRepeatStar;
Caroline Tice23d6f272010-10-13 20:44:39 +00001563
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001564 arg.push_back (signal_arg);
Caroline Tice23d6f272010-10-13 20:44:39 +00001565
1566 m_arguments.push_back (arg);
1567 }
1568
1569 ~CommandObjectProcessHandle ()
1570 {
1571 }
1572
1573 Options *
1574 GetOptions ()
1575 {
1576 return &m_options;
1577 }
1578
1579 bool
Caroline Ticee7471982010-10-14 21:31:13 +00001580 VerifyCommandOptionValue (const std::string &option, int &real_value)
Caroline Tice23d6f272010-10-13 20:44:39 +00001581 {
1582 bool okay = true;
1583
Caroline Ticee7471982010-10-14 21:31:13 +00001584 bool success = false;
1585 bool tmp_value = Args::StringToBoolean (option.c_str(), false, &success);
1586
1587 if (success && tmp_value)
1588 real_value = 1;
1589 else if (success && !tmp_value)
1590 real_value = 0;
Caroline Tice23d6f272010-10-13 20:44:39 +00001591 else
1592 {
1593 // If the value isn't 'true' or 'false', it had better be 0 or 1.
Caroline Ticee7471982010-10-14 21:31:13 +00001594 real_value = Args::StringToUInt32 (option.c_str(), 3);
1595 if (real_value != 0 && real_value != 1)
Caroline Tice23d6f272010-10-13 20:44:39 +00001596 okay = false;
1597 }
1598
1599 return okay;
1600 }
1601
Caroline Ticee7471982010-10-14 21:31:13 +00001602 void
1603 PrintSignalHeader (Stream &str)
1604 {
1605 str.Printf ("NAME PASS STOP NOTIFY\n");
1606 str.Printf ("========== ===== ===== ======\n");
1607 }
1608
1609 void
1610 PrintSignal (Stream &str, int32_t signo, const char *sig_name, UnixSignals &signals)
1611 {
1612 bool stop;
1613 bool suppress;
1614 bool notify;
1615
1616 str.Printf ("%-10s ", sig_name);
1617 if (signals.GetSignalInfo (signo, suppress, stop, notify))
1618 {
1619 bool pass = !suppress;
1620 str.Printf ("%s %s %s",
1621 (pass ? "true " : "false"),
1622 (stop ? "true " : "false"),
1623 (notify ? "true " : "false"));
1624 }
1625 str.Printf ("\n");
1626 }
1627
1628 void
1629 PrintSignalInformation (Stream &str, Args &signal_args, int num_valid_signals, UnixSignals &signals)
1630 {
1631 PrintSignalHeader (str);
1632
1633 if (num_valid_signals > 0)
1634 {
1635 size_t num_args = signal_args.GetArgumentCount();
1636 for (size_t i = 0; i < num_args; ++i)
1637 {
1638 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1639 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
1640 PrintSignal (str, signo, signal_args.GetArgumentAtIndex (i), signals);
1641 }
1642 }
1643 else // Print info for ALL signals
1644 {
1645 int32_t signo = signals.GetFirstSignalNumber();
1646 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1647 {
1648 PrintSignal (str, signo, signals.GetSignalAsCString (signo), signals);
1649 signo = signals.GetNextSignalNumber (signo);
1650 }
1651 }
1652 }
1653
Jim Inghamda26bd22012-06-08 21:56:10 +00001654protected:
Caroline Tice23d6f272010-10-13 20:44:39 +00001655 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001656 DoExecute (Args &signal_args, CommandReturnObject &result)
Caroline Tice23d6f272010-10-13 20:44:39 +00001657 {
1658 TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
1659
1660 if (!target_sp)
1661 {
1662 result.AppendError ("No current target;"
1663 " cannot handle signals until you have a valid target and process.\n");
1664 result.SetStatus (eReturnStatusFailed);
1665 return false;
1666 }
1667
1668 ProcessSP process_sp = target_sp->GetProcessSP();
1669
1670 if (!process_sp)
1671 {
1672 result.AppendError ("No current process; cannot handle signals until you have a valid process.\n");
1673 result.SetStatus (eReturnStatusFailed);
1674 return false;
1675 }
1676
Caroline Tice23d6f272010-10-13 20:44:39 +00001677 int stop_action = -1; // -1 means leave the current setting alone
Caroline Ticee7471982010-10-14 21:31:13 +00001678 int pass_action = -1; // -1 means leave the current setting alone
Caroline Tice23d6f272010-10-13 20:44:39 +00001679 int notify_action = -1; // -1 means leave the current setting alone
1680
1681 if (! m_options.stop.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001682 && ! VerifyCommandOptionValue (m_options.stop, stop_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001683 {
1684 result.AppendError ("Invalid argument for command option --stop; must be true or false.\n");
1685 result.SetStatus (eReturnStatusFailed);
1686 return false;
1687 }
1688
1689 if (! m_options.notify.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001690 && ! VerifyCommandOptionValue (m_options.notify, notify_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001691 {
1692 result.AppendError ("Invalid argument for command option --notify; must be true or false.\n");
1693 result.SetStatus (eReturnStatusFailed);
1694 return false;
1695 }
1696
1697 if (! m_options.pass.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001698 && ! VerifyCommandOptionValue (m_options.pass, pass_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001699 {
1700 result.AppendError ("Invalid argument for command option --pass; must be true or false.\n");
1701 result.SetStatus (eReturnStatusFailed);
1702 return false;
1703 }
1704
1705 size_t num_args = signal_args.GetArgumentCount();
1706 UnixSignals &signals = process_sp->GetUnixSignals();
1707 int num_signals_set = 0;
1708
Caroline Ticee7471982010-10-14 21:31:13 +00001709 if (num_args > 0)
Caroline Tice23d6f272010-10-13 20:44:39 +00001710 {
Caroline Ticee7471982010-10-14 21:31:13 +00001711 for (size_t i = 0; i < num_args; ++i)
Caroline Tice23d6f272010-10-13 20:44:39 +00001712 {
Caroline Ticee7471982010-10-14 21:31:13 +00001713 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1714 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
Caroline Tice23d6f272010-10-13 20:44:39 +00001715 {
Caroline Ticee7471982010-10-14 21:31:13 +00001716 // Casting the actions as bools here should be okay, because VerifyCommandOptionValue guarantees
1717 // the value is either 0 or 1.
1718 if (stop_action != -1)
1719 signals.SetShouldStop (signo, (bool) stop_action);
1720 if (pass_action != -1)
1721 {
1722 bool suppress = ! ((bool) pass_action);
1723 signals.SetShouldSuppress (signo, suppress);
1724 }
1725 if (notify_action != -1)
1726 signals.SetShouldNotify (signo, (bool) notify_action);
1727 ++num_signals_set;
Caroline Tice23d6f272010-10-13 20:44:39 +00001728 }
Caroline Ticee7471982010-10-14 21:31:13 +00001729 else
1730 {
1731 result.AppendErrorWithFormat ("Invalid signal name '%s'\n", signal_args.GetArgumentAtIndex (i));
1732 }
Caroline Tice23d6f272010-10-13 20:44:39 +00001733 }
1734 }
Caroline Ticee7471982010-10-14 21:31:13 +00001735 else
1736 {
1737 // No signal specified, if any command options were specified, update ALL signals.
1738 if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1))
1739 {
1740 if (m_interpreter.Confirm ("Do you really want to update all the signals?", false))
1741 {
1742 int32_t signo = signals.GetFirstSignalNumber();
1743 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1744 {
1745 if (notify_action != -1)
1746 signals.SetShouldNotify (signo, (bool) notify_action);
1747 if (stop_action != -1)
1748 signals.SetShouldStop (signo, (bool) stop_action);
1749 if (pass_action != -1)
1750 {
1751 bool suppress = ! ((bool) pass_action);
1752 signals.SetShouldSuppress (signo, suppress);
1753 }
1754 signo = signals.GetNextSignalNumber (signo);
1755 }
1756 }
1757 }
1758 }
1759
1760 PrintSignalInformation (result.GetOutputStream(), signal_args, num_signals_set, signals);
Caroline Tice23d6f272010-10-13 20:44:39 +00001761
1762 if (num_signals_set > 0)
1763 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1764 else
1765 result.SetStatus (eReturnStatusFailed);
1766
1767 return result.Succeeded();
1768 }
1769
Caroline Tice23d6f272010-10-13 20:44:39 +00001770 CommandOptions m_options;
1771};
1772
Greg Claytonb3448432011-03-24 21:19:54 +00001773OptionDefinition
Caroline Tice23d6f272010-10-13 20:44:39 +00001774CommandObjectProcessHandle::CommandOptions::g_option_table[] =
1775{
1776{ 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." },
1777{ 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." },
1778{ LLDB_OPT_SET_1, false, "pass", 'p', required_argument, NULL, 0, eArgTypeBoolean, "Whether or not the signal should be passed to the process." },
1779{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1780};
1781
1782//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001783// CommandObjectMultiwordProcess
1784//-------------------------------------------------------------------------
1785
Greg Clayton63094e02010-06-23 01:19:29 +00001786CommandObjectMultiwordProcess::CommandObjectMultiwordProcess (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00001787 CommandObjectMultiword (interpreter,
1788 "process",
1789 "A set of commands for operating on a process.",
1790 "process <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00001791{
Greg Claytona9eb8272011-07-02 21:07:54 +00001792 LoadSubCommand ("attach", CommandObjectSP (new CommandObjectProcessAttach (interpreter)));
1793 LoadSubCommand ("launch", CommandObjectSP (new CommandObjectProcessLaunch (interpreter)));
1794 LoadSubCommand ("continue", CommandObjectSP (new CommandObjectProcessContinue (interpreter)));
1795 LoadSubCommand ("connect", CommandObjectSP (new CommandObjectProcessConnect (interpreter)));
1796 LoadSubCommand ("detach", CommandObjectSP (new CommandObjectProcessDetach (interpreter)));
1797 LoadSubCommand ("load", CommandObjectSP (new CommandObjectProcessLoad (interpreter)));
1798 LoadSubCommand ("unload", CommandObjectSP (new CommandObjectProcessUnload (interpreter)));
1799 LoadSubCommand ("signal", CommandObjectSP (new CommandObjectProcessSignal (interpreter)));
1800 LoadSubCommand ("handle", CommandObjectSP (new CommandObjectProcessHandle (interpreter)));
1801 LoadSubCommand ("status", CommandObjectSP (new CommandObjectProcessStatus (interpreter)));
Greg Clayton238c0a12010-09-18 01:14:36 +00001802 LoadSubCommand ("interrupt", CommandObjectSP (new CommandObjectProcessInterrupt (interpreter)));
Greg Claytona9eb8272011-07-02 21:07:54 +00001803 LoadSubCommand ("kill", CommandObjectSP (new CommandObjectProcessKill (interpreter)));
Greg Clayton13193d52012-10-13 02:07:45 +00001804 LoadSubCommand ("plugin", CommandObjectSP (new CommandObjectProcessPlugin (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00001805}
1806
1807CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess ()
1808{
1809}
1810