blob: 441aee9758f758f6f1e7bc57623d5bfbdb6b1336 [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 Ingham124e6902012-08-11 01:27:55 +000016#include "lldb/Breakpoint/Breakpoint.h"
17#include "lldb/Breakpoint/BreakpointLocation.h"
18#include "lldb/Breakpoint/BreakpointSite.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "lldb/Core/State.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000020#include "lldb/Core/Module.h"
Greg Claytonabe0fed2011-04-18 08:33:37 +000021#include "lldb/Host/Host.h"
Jim Ingham124e6902012-08-11 01:27:55 +000022#include "lldb/Interpreter/Args.h"
23#include "lldb/Interpreter/Options.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Interpreter/CommandInterpreter.h"
25#include "lldb/Interpreter/CommandReturnObject.h"
Greg Claytone4b9c1f2011-03-08 22:40:15 +000026#include "lldb/Target/Platform.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027#include "lldb/Target/Process.h"
Jim Ingham124e6902012-08-11 01:27:55 +000028#include "lldb/Target/StopInfo.h"
Chris Lattner24943d22010-06-08 16:52:24 +000029#include "lldb/Target/Target.h"
30#include "lldb/Target/Thread.h"
31
32using namespace lldb;
33using namespace lldb_private;
34
35//-------------------------------------------------------------------------
36// CommandObjectProcessLaunch
37//-------------------------------------------------------------------------
Jim Ingham5a15e692012-02-16 06:50:00 +000038#pragma mark CommandObjectProcessLaunch
Jim Inghamda26bd22012-06-08 21:56:10 +000039class CommandObjectProcessLaunch : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +000040{
41public:
42
Greg Clayton238c0a12010-09-18 01:14:36 +000043 CommandObjectProcessLaunch (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +000044 CommandObjectParsed (interpreter,
45 "process launch",
46 "Launch the executable in the debugger.",
47 NULL),
Greg Claytonf15996e2011-04-07 22:46:35 +000048 m_options (interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +000049 {
Caroline Tice43b014a2010-10-04 22:28:36 +000050 CommandArgumentEntry arg;
51 CommandArgumentData run_args_arg;
52
53 // Define the first (and only) variant of this arg.
54 run_args_arg.arg_type = eArgTypeRunArgs;
55 run_args_arg.arg_repetition = eArgRepeatOptional;
56
57 // There is only one variant this argument could be; put it into the argument entry.
58 arg.push_back (run_args_arg);
59
60 // Push the data for the first argument into the m_arguments vector.
61 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +000062 }
63
64
65 ~CommandObjectProcessLaunch ()
66 {
67 }
68
Jim Ingham392d9e02012-08-10 21:48:41 +000069 int
70 HandleArgumentCompletion (Args &input,
71 int &cursor_index,
72 int &cursor_char_position,
73 OptionElementVector &opt_element_vector,
74 int match_start_point,
75 int max_return_elements,
76 bool &word_complete,
77 StringList &matches)
78 {
79 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
80 completion_str.erase (cursor_char_position);
81
82 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
83 CommandCompletions::eDiskFileCompletion,
84 completion_str.c_str(),
85 match_start_point,
86 max_return_elements,
87 NULL,
88 word_complete,
89 matches);
90 return matches.GetSize();
91 }
92
Chris Lattner24943d22010-06-08 16:52:24 +000093 Options *
94 GetOptions ()
95 {
96 return &m_options;
97 }
98
Jim Inghamda26bd22012-06-08 21:56:10 +000099 virtual const char *GetRepeatCommand (Args &current_command_args, uint32_t index)
100 {
101 // No repeat for "process launch"...
102 return "";
103 }
104
105protected:
Chris Lattner24943d22010-06-08 16:52:24 +0000106 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000107 DoExecute (Args& launch_args, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +0000108 {
Greg Claytonabb33022011-11-08 02:43:13 +0000109 Debugger &debugger = m_interpreter.GetDebugger();
110 Target *target = debugger.GetSelectedTarget().get();
111 Error error;
Chris Lattner24943d22010-06-08 16:52:24 +0000112
113 if (target == NULL)
114 {
Greg Claytone1f50b92011-05-03 22:09:39 +0000115 result.AppendError ("invalid target, create a debug target using the 'target create' command");
Chris Lattner24943d22010-06-08 16:52:24 +0000116 result.SetStatus (eReturnStatusFailed);
117 return false;
118 }
Chris Lattner24943d22010-06-08 16:52:24 +0000119 // If our listener is NULL, users aren't allows to launch
Chris Lattner24943d22010-06-08 16:52:24 +0000120 char filename[PATH_MAX];
Greg Clayton5beb99d2011-08-11 02:48:45 +0000121 const Module *exe_module = target->GetExecutableModulePointer();
Greg Claytona2f74232011-02-24 22:24:29 +0000122
123 if (exe_module == NULL)
124 {
Greg Claytone1f50b92011-05-03 22:09:39 +0000125 result.AppendError ("no file in target, create a debug target using the 'target create' command");
Greg Claytona2f74232011-02-24 22:24:29 +0000126 result.SetStatus (eReturnStatusFailed);
127 return false;
128 }
129
Greg Claytona2f74232011-02-24 22:24:29 +0000130 StateType state = eStateInvalid;
Greg Clayton567e7f32011-09-22 04:58:26 +0000131 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytona2f74232011-02-24 22:24:29 +0000132 if (process)
133 {
134 state = process->GetState();
135
136 if (process->IsAlive() && state != eStateConnected)
137 {
138 char message[1024];
139 if (process->GetState() == eStateAttaching)
140 ::strncpy (message, "There is a pending attach, abort it and launch a new process?", sizeof(message));
141 else
142 ::strncpy (message, "There is a running process, kill it and restart?", sizeof(message));
143
144 if (!m_interpreter.Confirm (message, true))
Jim Ingham22dc9722010-12-09 18:58:16 +0000145 {
Greg Claytona2f74232011-02-24 22:24:29 +0000146 result.SetStatus (eReturnStatusFailed);
147 return false;
Jim Ingham22dc9722010-12-09 18:58:16 +0000148 }
149 else
150 {
Greg Claytonabb33022011-11-08 02:43:13 +0000151 Error destroy_error (process->Destroy());
152 if (destroy_error.Success())
Greg Claytona2f74232011-02-24 22:24:29 +0000153 {
154 result.SetStatus (eReturnStatusSuccessFinishResult);
155 }
156 else
157 {
Greg Claytonabb33022011-11-08 02:43:13 +0000158 result.AppendErrorWithFormat ("Failed to kill process: %s\n", destroy_error.AsCString());
Greg Claytona2f74232011-02-24 22:24:29 +0000159 result.SetStatus (eReturnStatusFailed);
160 }
Jim Ingham22dc9722010-12-09 18:58:16 +0000161 }
162 }
Chris Lattner24943d22010-06-08 16:52:24 +0000163 }
Jim Ingham22dc9722010-12-09 18:58:16 +0000164
Greg Clayton0c8446c2012-10-17 22:57:12 +0000165 const char *target_settings_argv0 = target->GetArg0();
166
167 exe_module->GetFileSpec().GetPath (filename, sizeof(filename));
168
169 if (target_settings_argv0)
170 {
171 m_options.launch_info.GetArguments().AppendArgument (target_settings_argv0);
172 m_options.launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), false);
173 }
174 else
175 {
176 m_options.launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
177 }
178
Greg Clayton527154d2011-11-15 03:53:30 +0000179 if (launch_args.GetArgumentCount() == 0)
180 {
Greg Clayton73844aa2012-08-22 17:17:09 +0000181 Args target_setting_args;
Greg Clayton0c8446c2012-10-17 22:57:12 +0000182 if (target->GetRunArguments(target_setting_args))
Greg Clayton73844aa2012-08-22 17:17:09 +0000183 m_options.launch_info.GetArguments().AppendArguments (target_setting_args);
Greg Clayton527154d2011-11-15 03:53:30 +0000184 }
185 else
Greg Claytonabb33022011-11-08 02:43:13 +0000186 {
Greg Clayton0c8446c2012-10-17 22:57:12 +0000187 m_options.launch_info.GetArguments().AppendArguments (launch_args);
188
Greg Clayton3e6f2cc2011-11-21 21:51:18 +0000189 // Save the arguments for subsequent runs in the current target.
190 target->SetRunArguments (launch_args);
Greg Claytonabb33022011-11-08 02:43:13 +0000191 }
Greg Claytonabb33022011-11-08 02:43:13 +0000192
Greg Clayton527154d2011-11-15 03:53:30 +0000193 if (target->GetDisableASLR())
194 m_options.launch_info.GetFlags().Set (eLaunchFlagDisableASLR);
195
196 if (target->GetDisableSTDIO())
197 m_options.launch_info.GetFlags().Set (eLaunchFlagDisableSTDIO);
198
199 m_options.launch_info.GetFlags().Set (eLaunchFlagDebug);
200
201 Args environment;
202 target->GetEnvironmentAsArgs (environment);
203 if (environment.GetArgumentCount() > 0)
204 m_options.launch_info.GetEnvironmentEntries ().AppendArguments (environment);
205
Greg Clayton464c6162011-11-17 22:14:31 +0000206 // Finalize the file actions, and if none were given, default to opening
207 // up a pseudo terminal
208 const bool default_to_use_pty = true;
209 m_options.launch_info.FinalizeFileActions (target, default_to_use_pty);
Greg Clayton527154d2011-11-15 03:53:30 +0000210
Greg Claytonabb33022011-11-08 02:43:13 +0000211 if (state == eStateConnected)
212 {
213 if (m_options.launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
214 {
215 result.AppendWarning("can't launch in tty when launching through a remote connection");
216 m_options.launch_info.GetFlags().Clear (eLaunchFlagLaunchInTTY);
217 }
218 }
219 else
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000220 {
Greg Clayton527154d2011-11-15 03:53:30 +0000221 if (!m_options.launch_info.GetArchitecture().IsValid())
Greg Clayton2d9adb72011-11-12 02:10:56 +0000222 m_options.launch_info.GetArchitecture() = target->GetArchitecture();
223
Greg Clayton75d8c252011-11-28 01:45:00 +0000224 PlatformSP platform_sp (target->GetPlatform());
225
226 if (platform_sp && platform_sp->CanDebugProcess ())
227 {
228 process = target->GetPlatform()->DebugProcess (m_options.launch_info,
229 debugger,
230 target,
231 debugger.GetListener(),
232 error).get();
233 }
234 else
235 {
236 const char *plugin_name = m_options.launch_info.GetProcessPluginName();
Greg Clayton46c9a352012-02-09 06:16:32 +0000237 process = target->CreateProcess (debugger.GetListener(), plugin_name, NULL).get();
Greg Clayton75d8c252011-11-28 01:45:00 +0000238 if (process)
239 error = process->Launch (m_options.launch_info);
240 }
Greg Claytonabb33022011-11-08 02:43:13 +0000241
Greg Claytona2f74232011-02-24 22:24:29 +0000242 if (process == NULL)
243 {
Greg Clayton527154d2011-11-15 03:53:30 +0000244 result.SetError (error, "failed to launch or debug process");
Greg Claytona2f74232011-02-24 22:24:29 +0000245 return false;
246 }
Chris Lattner24943d22010-06-08 16:52:24 +0000247 }
Greg Claytonabb33022011-11-08 02:43:13 +0000248
Greg Clayton238c0a12010-09-18 01:14:36 +0000249 if (error.Success())
250 {
Greg Clayton940b1032011-02-23 00:35:02 +0000251 const char *archname = exe_module->GetArchitecture().GetArchitectureName();
Greg Claytonc1d37752010-10-18 01:45:30 +0000252
Greg Clayton444e35b2011-10-19 18:09:39 +0000253 result.AppendMessageWithFormat ("Process %llu launched: '%s' (%s)\n", process->GetID(), filename, archname);
Greg Claytond8c62532010-10-07 04:19:01 +0000254 result.SetDidChangeProcessState (true);
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000255 if (m_options.launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
Greg Clayton238c0a12010-09-18 01:14:36 +0000256 {
Greg Claytond8c62532010-10-07 04:19:01 +0000257 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
Greg Clayton238c0a12010-09-18 01:14:36 +0000258 StateType state = process->WaitForProcessToStop (NULL);
259
260 if (state == eStateStopped)
261 {
Greg Claytond8c62532010-10-07 04:19:01 +0000262 error = process->Resume();
263 if (error.Success())
264 {
265 bool synchronous_execution = m_interpreter.GetSynchronous ();
266 if (synchronous_execution)
267 {
268 state = process->WaitForProcessToStop (NULL);
Greg Clayton20206082011-11-17 01:23:07 +0000269 const bool must_be_alive = true;
270 if (!StateIsStoppedState(state, must_be_alive))
Greg Clayton395fc332011-02-15 21:59:32 +0000271 {
Greg Clayton527154d2011-11-15 03:53:30 +0000272 result.AppendErrorWithFormat ("process isn't stopped: %s", StateAsCString(state));
Greg Clayton395fc332011-02-15 21:59:32 +0000273 }
Greg Claytond8c62532010-10-07 04:19:01 +0000274 result.SetDidChangeProcessState (true);
275 result.SetStatus (eReturnStatusSuccessFinishResult);
276 }
277 else
278 {
279 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
280 }
281 }
Greg Clayton395fc332011-02-15 21:59:32 +0000282 else
283 {
Greg Clayton527154d2011-11-15 03:53:30 +0000284 result.AppendErrorWithFormat ("process resume at entry point failed: %s", error.AsCString());
Greg Clayton395fc332011-02-15 21:59:32 +0000285 result.SetStatus (eReturnStatusFailed);
286 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000287 }
Greg Clayton395fc332011-02-15 21:59:32 +0000288 else
289 {
Greg Clayton527154d2011-11-15 03:53:30 +0000290 result.AppendErrorWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
Greg Clayton395fc332011-02-15 21:59:32 +0000291 result.SetStatus (eReturnStatusFailed);
292 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000293 }
294 }
Greg Clayton395fc332011-02-15 21:59:32 +0000295 else
296 {
Greg Claytona9eb8272011-07-02 21:07:54 +0000297 result.AppendErrorWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton395fc332011-02-15 21:59:32 +0000298 result.SetStatus (eReturnStatusFailed);
299 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000300
Chris Lattner24943d22010-06-08 16:52:24 +0000301 return result.Succeeded();
302 }
303
304protected:
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000305 ProcessLaunchCommandOptions m_options;
Chris Lattner24943d22010-06-08 16:52:24 +0000306};
307
308
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000309//#define SET1 LLDB_OPT_SET_1
310//#define SET2 LLDB_OPT_SET_2
311//#define SET3 LLDB_OPT_SET_3
312//
313//OptionDefinition
314//CommandObjectProcessLaunch::CommandOptions::g_option_table[] =
315//{
316//{ 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."},
317//{ SET1 , false, "stdin", 'i', required_argument, NULL, 0, eArgTypePath, "Redirect stdin for the process to <path>."},
318//{ SET1 , false, "stdout", 'o', required_argument, NULL, 0, eArgTypePath, "Redirect stdout for the process to <path>."},
319//{ SET1 , false, "stderr", 'e', required_argument, NULL, 0, eArgTypePath, "Redirect stderr for the process to <path>."},
320//{ SET1 | SET2 | SET3, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
321//{ 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."},
322//{ SET3, false, "no-stdio", 'n', no_argument, NULL, 0, eArgTypeNone, "Do not set up for terminal I/O to go to running process."},
323//{ SET1 | SET2 | SET3, false, "working-dir", 'w', required_argument, NULL, 0, eArgTypePath, "Set the current working directory to <path> when running the inferior."},
324//{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
325//};
326//
327//#undef SET1
328//#undef SET2
329//#undef SET3
Chris Lattner24943d22010-06-08 16:52:24 +0000330
331//-------------------------------------------------------------------------
332// CommandObjectProcessAttach
333//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000334#pragma mark CommandObjectProcessAttach
Jim Inghamda26bd22012-06-08 21:56:10 +0000335class CommandObjectProcessAttach : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000336{
337public:
338
Chris Lattner24943d22010-06-08 16:52:24 +0000339 class CommandOptions : public Options
340 {
341 public:
342
Greg Claytonf15996e2011-04-07 22:46:35 +0000343 CommandOptions (CommandInterpreter &interpreter) :
344 Options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000345 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000346 // Keep default values of all options in one place: OptionParsingStarting ()
347 OptionParsingStarting ();
Chris Lattner24943d22010-06-08 16:52:24 +0000348 }
349
350 ~CommandOptions ()
351 {
352 }
353
354 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000355 SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +0000356 {
357 Error error;
358 char short_option = (char) m_getopt_table[option_idx].val;
359 bool success = false;
360 switch (short_option)
361 {
Johnny Chen7c099972012-05-24 00:43:00 +0000362 case 'c':
363 attach_info.SetContinueOnceAttached(true);
364 break;
365
Chris Lattner24943d22010-06-08 16:52:24 +0000366 case 'p':
Chris Lattner24943d22010-06-08 16:52:24 +0000367 {
Greg Clayton527154d2011-11-15 03:53:30 +0000368 lldb::pid_t pid = Args::StringToUInt32 (option_arg, LLDB_INVALID_PROCESS_ID, 0, &success);
369 if (!success || pid == LLDB_INVALID_PROCESS_ID)
370 {
371 error.SetErrorStringWithFormat("invalid process ID '%s'", option_arg);
372 }
373 else
374 {
375 attach_info.SetProcessID (pid);
376 }
Chris Lattner24943d22010-06-08 16:52:24 +0000377 }
378 break;
379
380 case 'P':
Greg Clayton527154d2011-11-15 03:53:30 +0000381 attach_info.SetProcessPluginName (option_arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000382 break;
383
384 case 'n':
Greg Clayton527154d2011-11-15 03:53:30 +0000385 attach_info.GetExecutableFile().SetFile(option_arg, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000386 break;
387
388 case 'w':
Greg Clayton527154d2011-11-15 03:53:30 +0000389 attach_info.SetWaitForLaunch(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000390 break;
Jim Ingham3a458eb2012-07-20 21:37:13 +0000391
392 case 'i':
393 attach_info.SetIgnoreExisting(false);
394 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000395
396 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000397 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner24943d22010-06-08 16:52:24 +0000398 break;
399 }
400 return error;
401 }
402
403 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000404 OptionParsingStarting ()
Chris Lattner24943d22010-06-08 16:52:24 +0000405 {
Greg Clayton527154d2011-11-15 03:53:30 +0000406 attach_info.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000407 }
408
Greg Claytonb3448432011-03-24 21:19:54 +0000409 const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +0000410 GetDefinitions ()
411 {
412 return g_option_table;
413 }
414
Jim Ingham7508e732010-08-09 23:31:02 +0000415 virtual bool
Greg Claytonf15996e2011-04-07 22:46:35 +0000416 HandleOptionArgumentCompletion (Args &input,
Jim Ingham7508e732010-08-09 23:31:02 +0000417 int cursor_index,
418 int char_pos,
419 OptionElementVector &opt_element_vector,
420 int opt_element_index,
421 int match_start_point,
422 int max_return_elements,
423 bool &word_complete,
424 StringList &matches)
425 {
426 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
427 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
428
429 // We are only completing the name option for now...
430
Greg Claytonb3448432011-03-24 21:19:54 +0000431 const OptionDefinition *opt_defs = GetDefinitions();
Jim Ingham7508e732010-08-09 23:31:02 +0000432 if (opt_defs[opt_defs_index].short_option == 'n')
433 {
434 // Are we in the name?
435
436 // Look to see if there is a -P argument provided, and if so use that plugin, otherwise
437 // use the default plugin.
Jim Ingham7508e732010-08-09 23:31:02 +0000438
439 const char *partial_name = NULL;
440 partial_name = input.GetArgumentAtIndex(opt_arg_pos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000441
Greg Claytonb72d0f02011-04-12 05:54:46 +0000442 PlatformSP platform_sp (m_interpreter.GetPlatform (true));
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000443 if (platform_sp)
Jim Ingham7508e732010-08-09 23:31:02 +0000444 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000445 ProcessInstanceInfoList process_infos;
446 ProcessInstanceInfoMatch match_info;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000447 if (partial_name)
448 {
Greg Clayton527154d2011-11-15 03:53:30 +0000449 match_info.GetProcessInfo().GetExecutableFile().SetFile(partial_name, false);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000450 match_info.SetNameMatchType(eNameMatchStartsWith);
451 }
452 platform_sp->FindProcesses (match_info, process_infos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000453 const uint32_t num_matches = process_infos.GetSize();
454 if (num_matches > 0)
455 {
456 for (uint32_t i=0; i<num_matches; ++i)
457 {
458 matches.AppendString (process_infos.GetProcessNameAtIndex(i),
459 process_infos.GetProcessNameLengthAtIndex(i));
460 }
461 }
Jim Ingham7508e732010-08-09 23:31:02 +0000462 }
463 }
464
465 return false;
466 }
467
Chris Lattner24943d22010-06-08 16:52:24 +0000468 // Options table: Required for subclasses of Options.
469
Greg Claytonb3448432011-03-24 21:19:54 +0000470 static OptionDefinition g_option_table[];
Chris Lattner24943d22010-06-08 16:52:24 +0000471
472 // Instance variables to hold the values for command options.
473
Greg Clayton527154d2011-11-15 03:53:30 +0000474 ProcessAttachInfo attach_info;
Chris Lattner24943d22010-06-08 16:52:24 +0000475 };
476
Greg Clayton238c0a12010-09-18 01:14:36 +0000477 CommandObjectProcessAttach (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000478 CommandObjectParsed (interpreter,
479 "process attach",
480 "Attach to a process.",
481 "process attach <cmd-options>"),
Greg Claytonf15996e2011-04-07 22:46:35 +0000482 m_options (interpreter)
Jim Ingham7508e732010-08-09 23:31:02 +0000483 {
Jim Ingham7508e732010-08-09 23:31:02 +0000484 }
485
486 ~CommandObjectProcessAttach ()
487 {
488 }
489
Jim Inghamda26bd22012-06-08 21:56:10 +0000490 Options *
491 GetOptions ()
492 {
493 return &m_options;
494 }
495
496protected:
Jim Ingham7508e732010-08-09 23:31:02 +0000497 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000498 DoExecute (Args& command,
Jim Ingham7508e732010-08-09 23:31:02 +0000499 CommandReturnObject &result)
500 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000501 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Jim Inghamee940e22011-09-15 01:08:57 +0000502 // N.B. The attach should be synchronous. It doesn't help much to get the prompt back between initiating the attach
503 // and the target actually stopping. So even if the interpreter is set to be asynchronous, we wait for the stop
504 // ourselves here.
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000505
Greg Clayton567e7f32011-09-22 04:58:26 +0000506 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytona2f74232011-02-24 22:24:29 +0000507 StateType state = eStateInvalid;
Jim Ingham7508e732010-08-09 23:31:02 +0000508 if (process)
509 {
Greg Claytona2f74232011-02-24 22:24:29 +0000510 state = process->GetState();
511 if (process->IsAlive() && state != eStateConnected)
Jim Ingham7508e732010-08-09 23:31:02 +0000512 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000513 result.AppendErrorWithFormat ("Process %llu is currently being debugged, kill the process before attaching.\n",
Jim Ingham7508e732010-08-09 23:31:02 +0000514 process->GetID());
515 result.SetStatus (eReturnStatusFailed);
516 return false;
517 }
518 }
519
520 if (target == NULL)
521 {
522 // If there isn't a current target create one.
523 TargetSP new_target_sp;
524 FileSpec emptyFileSpec;
Jim Ingham7508e732010-08-09 23:31:02 +0000525 Error error;
526
Greg Clayton238c0a12010-09-18 01:14:36 +0000527 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
528 emptyFileSpec,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000529 NULL,
Greg Clayton238c0a12010-09-18 01:14:36 +0000530 false,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000531 NULL, // No platform options
Greg Clayton238c0a12010-09-18 01:14:36 +0000532 new_target_sp);
Jim Ingham7508e732010-08-09 23:31:02 +0000533 target = new_target_sp.get();
534 if (target == NULL || error.Fail())
535 {
Greg Claytone71e2582011-02-04 01:58:07 +0000536 result.AppendError(error.AsCString("Error creating target"));
Jim Ingham7508e732010-08-09 23:31:02 +0000537 return false;
538 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000539 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target);
Jim Ingham7508e732010-08-09 23:31:02 +0000540 }
541
542 // Record the old executable module, we want to issue a warning if the process of attaching changed the
543 // current executable (like somebody said "file foo" then attached to a PID whose executable was bar.)
544
545 ModuleSP old_exec_module_sp = target->GetExecutableModule();
546 ArchSpec old_arch_spec = target->GetArchitecture();
547
548 if (command.GetArgumentCount())
549 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000550 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 +0000551 result.SetStatus (eReturnStatusFailed);
552 }
553 else
554 {
Greg Claytona2f74232011-02-24 22:24:29 +0000555 if (state != eStateConnected)
556 {
Greg Clayton527154d2011-11-15 03:53:30 +0000557 const char *plugin_name = m_options.attach_info.GetProcessPluginName();
Greg Clayton46c9a352012-02-09 06:16:32 +0000558 process = target->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name, NULL).get();
Greg Claytona2f74232011-02-24 22:24:29 +0000559 }
Jim Ingham7508e732010-08-09 23:31:02 +0000560
561 if (process)
562 {
563 Error error;
Greg Clayton527154d2011-11-15 03:53:30 +0000564 // If no process info was specified, then use the target executable
565 // name as the process to attach to by default
566 if (!m_options.attach_info.ProcessInfoSpecified ())
Jim Ingham4805a1c2010-09-15 01:34:14 +0000567 {
568 if (old_exec_module_sp)
Greg Clayton1d1f39e2011-11-29 04:03:30 +0000569 m_options.attach_info.GetExecutableFile().GetFilename() = old_exec_module_sp->GetPlatformFileSpec().GetFilename();
Jim Ingham4805a1c2010-09-15 01:34:14 +0000570
Greg Clayton527154d2011-11-15 03:53:30 +0000571 if (!m_options.attach_info.ProcessInfoSpecified ())
572 {
573 error.SetErrorString ("no process specified, create a target with a file, or specify the --pid or --name command option");
574 }
575 }
576
577 if (error.Success())
578 {
579 error = process->Attach (m_options.attach_info);
580
Jim Ingham4805a1c2010-09-15 01:34:14 +0000581 if (error.Success())
582 {
583 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
584 }
Jim Ingham7508e732010-08-09 23:31:02 +0000585 else
586 {
Greg Clayton527154d2011-11-15 03:53:30 +0000587 result.AppendErrorWithFormat ("attach failed: %s\n", error.AsCString());
Jim Ingham4805a1c2010-09-15 01:34:14 +0000588 result.SetStatus (eReturnStatusFailed);
589 return false;
Jim Ingham7508e732010-08-09 23:31:02 +0000590 }
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000591 // If we're synchronous, wait for the stopped event and report that.
592 // Otherwise just return.
593 // FIXME: in the async case it will now be possible to get to the command
594 // interpreter with a state eStateAttaching. Make sure we handle that correctly.
Jim Inghamee940e22011-09-15 01:08:57 +0000595 StateType state = process->WaitForProcessToStop (NULL);
Greg Clayton527154d2011-11-15 03:53:30 +0000596
Jim Inghamee940e22011-09-15 01:08:57 +0000597 result.SetDidChangeProcessState (true);
Johnny Chen9986a3b2012-05-18 00:51:36 +0000598
599 if (state == eStateStopped)
600 {
601 result.AppendMessageWithFormat ("Process %llu %s\n", process->GetID(), StateAsCString (state));
602 result.SetStatus (eReturnStatusSuccessFinishNoResult);
603 }
604 else
605 {
606 result.AppendError ("attach failed: process did not stop (no such process or permission problem?)");
Jim Ingham5d90ade2012-07-27 23:57:19 +0000607 process->Destroy();
Johnny Chen9986a3b2012-05-18 00:51:36 +0000608 result.SetStatus (eReturnStatusFailed);
609 return false;
610 }
Jim Ingham7508e732010-08-09 23:31:02 +0000611 }
Jim Ingham7508e732010-08-09 23:31:02 +0000612 }
613 }
614
615 if (result.Succeeded())
616 {
617 // Okay, we're done. Last step is to warn if the executable module has changed:
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000618 char new_path[PATH_MAX];
Greg Clayton5beb99d2011-08-11 02:48:45 +0000619 ModuleSP new_exec_module_sp (target->GetExecutableModule());
Jim Ingham7508e732010-08-09 23:31:02 +0000620 if (!old_exec_module_sp)
621 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000622 // We might not have a module if we attached to a raw pid...
Greg Clayton5beb99d2011-08-11 02:48:45 +0000623 if (new_exec_module_sp)
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000624 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000625 new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000626 result.AppendMessageWithFormat("Executable module set to \"%s\".\n", new_path);
627 }
Jim Ingham7508e732010-08-09 23:31:02 +0000628 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000629 else if (old_exec_module_sp->GetFileSpec() != new_exec_module_sp->GetFileSpec())
Jim Ingham7508e732010-08-09 23:31:02 +0000630 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000631 char old_path[PATH_MAX];
Jim Ingham7508e732010-08-09 23:31:02 +0000632
Greg Clayton5beb99d2011-08-11 02:48:45 +0000633 old_exec_module_sp->GetFileSpec().GetPath (old_path, PATH_MAX);
634 new_exec_module_sp->GetFileSpec().GetPath (new_path, PATH_MAX);
Jim Ingham7508e732010-08-09 23:31:02 +0000635
636 result.AppendWarningWithFormat("Executable module changed from \"%s\" to \"%s\".\n",
637 old_path, new_path);
638 }
639
640 if (!old_arch_spec.IsValid())
641 {
Greg Clayton92cb9a92012-09-14 02:41:36 +0000642 result.AppendMessageWithFormat ("Architecture set to: %s.\n", target->GetArchitecture().GetTriple().getTriple().c_str());
Jim Ingham7508e732010-08-09 23:31:02 +0000643 }
644 else if (old_arch_spec != target->GetArchitecture())
645 {
646 result.AppendWarningWithFormat("Architecture changed from %s to %s.\n",
Greg Clayton92cb9a92012-09-14 02:41:36 +0000647 old_arch_spec.GetTriple().getTriple().c_str(),
648 target->GetArchitecture().GetTriple().getTriple().c_str());
Jim Ingham7508e732010-08-09 23:31:02 +0000649 }
Johnny Chen7c099972012-05-24 00:43:00 +0000650
651 // This supports the use-case scenario of immediately continuing the process once attached.
652 if (m_options.attach_info.GetContinueOnceAttached())
Sean Callanan4336d932012-05-31 01:30:08 +0000653 m_interpreter.HandleCommand("process continue", eLazyBoolNo, result);
Jim Ingham7508e732010-08-09 23:31:02 +0000654 }
655 return result.Succeeded();
656 }
657
Chris Lattner24943d22010-06-08 16:52:24 +0000658 CommandOptions m_options;
659};
660
661
Greg Claytonb3448432011-03-24 21:19:54 +0000662OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000663CommandObjectProcessAttach::CommandOptions::g_option_table[] =
664{
Jim Ingham3a458eb2012-07-20 21:37:13 +0000665{ LLDB_OPT_SET_ALL, false, "continue",'c', no_argument, NULL, 0, eArgTypeNone, "Immediately continue the process once attached."},
666{ LLDB_OPT_SET_ALL, false, "plugin", 'P', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
667{ LLDB_OPT_SET_1, false, "pid", 'p', required_argument, NULL, 0, eArgTypePid, "The process ID of an existing process to attach to."},
668{ LLDB_OPT_SET_2, false, "name", 'n', required_argument, NULL, 0, eArgTypeProcessName, "The name of the process to attach to."},
669{ LLDB_OPT_SET_2, false, "include-existing", 'i', no_argument, NULL, 0, eArgTypeNone, "Include existing processes when doing attach -w."},
670{ 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 +0000671{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000672};
673
674//-------------------------------------------------------------------------
675// CommandObjectProcessContinue
676//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000677#pragma mark CommandObjectProcessContinue
Chris Lattner24943d22010-06-08 16:52:24 +0000678
Jim Inghamda26bd22012-06-08 21:56:10 +0000679class CommandObjectProcessContinue : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000680{
681public:
682
Greg Clayton238c0a12010-09-18 01:14:36 +0000683 CommandObjectProcessContinue (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000684 CommandObjectParsed (interpreter,
685 "process continue",
686 "Continue execution of all threads in the current process.",
687 "process continue",
Jim Ingham124e6902012-08-11 01:27:55 +0000688 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused),
689 m_options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000690 {
691 }
692
693
694 ~CommandObjectProcessContinue ()
695 {
696 }
697
Jim Inghamda26bd22012-06-08 21:56:10 +0000698protected:
Jim Ingham124e6902012-08-11 01:27:55 +0000699
700 class CommandOptions : public Options
701 {
702 public:
703
704 CommandOptions (CommandInterpreter &interpreter) :
705 Options(interpreter)
706 {
707 // Keep default values of all options in one place: OptionParsingStarting ()
708 OptionParsingStarting ();
709 }
710
711 ~CommandOptions ()
712 {
713 }
714
715 Error
716 SetOptionValue (uint32_t option_idx, const char *option_arg)
717 {
718 Error error;
719 char short_option = (char) m_getopt_table[option_idx].val;
720 bool success = false;
721 switch (short_option)
722 {
723 case 'i':
724 m_ignore = Args::StringToUInt32 (option_arg, 0, 0, &success);
725 if (!success)
726 error.SetErrorStringWithFormat ("invalid value for ignore option: \"%s\", should be a number.", option_arg);
727 break;
728
729 default:
730 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
731 break;
732 }
733 return error;
734 }
735
736 void
737 OptionParsingStarting ()
738 {
739 m_ignore = 0;
740 }
741
742 const OptionDefinition*
743 GetDefinitions ()
744 {
745 return g_option_table;
746 }
747
748 // Options table: Required for subclasses of Options.
749
750 static OptionDefinition g_option_table[];
751
752 uint32_t m_ignore;
753 };
754
Chris Lattner24943d22010-06-08 16:52:24 +0000755 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000756 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000757 CommandReturnObject &result)
758 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000759 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton238c0a12010-09-18 01:14:36 +0000760 bool synchronous_execution = m_interpreter.GetSynchronous ();
Chris Lattner24943d22010-06-08 16:52:24 +0000761
762 if (process == NULL)
763 {
764 result.AppendError ("no process to continue");
765 result.SetStatus (eReturnStatusFailed);
766 return false;
767 }
768
769 StateType state = process->GetState();
770 if (state == eStateStopped)
771 {
772 if (command.GetArgumentCount() != 0)
773 {
774 result.AppendErrorWithFormat ("The '%s' command does not take any arguments.\n", m_cmd_name.c_str());
775 result.SetStatus (eReturnStatusFailed);
776 return false;
777 }
778
Jim Ingham124e6902012-08-11 01:27:55 +0000779 if (m_options.m_ignore > 0)
780 {
781 ThreadSP sel_thread_sp(process->GetThreadList().GetSelectedThread());
782 if (sel_thread_sp)
783 {
784 StopInfoSP stop_info_sp = sel_thread_sp->GetStopInfo();
785 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint)
786 {
787 uint64_t bp_site_id = stop_info_sp->GetValue();
788 BreakpointSiteSP bp_site_sp(process->GetBreakpointSiteList().FindByID(bp_site_id));
789 if (bp_site_sp)
790 {
791 uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
792 for (uint32_t i = 0; i < num_owners; i++)
793 {
794 Breakpoint &bp_ref = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
795 if (!bp_ref.IsInternal())
796 {
797 bp_ref.SetIgnoreCount(m_options.m_ignore);
798 }
799 }
800 }
801 }
802 }
803 }
804
Jim Inghamb9950592012-09-10 20:50:15 +0000805 { // Scope for thread list mutex:
806 Mutex::Locker locker (process->GetThreadList().GetMutex());
807 const uint32_t num_threads = process->GetThreadList().GetSize();
Chris Lattner24943d22010-06-08 16:52:24 +0000808
Jim Inghamb9950592012-09-10 20:50:15 +0000809 // Set the actions that the threads should each take when resuming
810 for (uint32_t idx=0; idx<num_threads; ++idx)
811 {
812 process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState (eStateRunning);
813 }
Chris Lattner24943d22010-06-08 16:52:24 +0000814 }
Jim Inghamb9950592012-09-10 20:50:15 +0000815
Chris Lattner24943d22010-06-08 16:52:24 +0000816 Error error(process->Resume());
817 if (error.Success())
818 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000819 result.AppendMessageWithFormat ("Process %llu resuming\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000820 if (synchronous_execution)
821 {
Greg Claytonbef15832010-07-14 00:18:15 +0000822 state = process->WaitForProcessToStop (NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000823
824 result.SetDidChangeProcessState (true);
Greg Clayton444e35b2011-10-19 18:09:39 +0000825 result.AppendMessageWithFormat ("Process %llu %s\n", process->GetID(), StateAsCString (state));
Chris Lattner24943d22010-06-08 16:52:24 +0000826 result.SetStatus (eReturnStatusSuccessFinishNoResult);
827 }
828 else
829 {
830 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
831 }
832 }
833 else
834 {
835 result.AppendErrorWithFormat("Failed to resume process: %s.\n", error.AsCString());
836 result.SetStatus (eReturnStatusFailed);
837 }
838 }
839 else
840 {
841 result.AppendErrorWithFormat ("Process cannot be continued from its current state (%s).\n",
842 StateAsCString(state));
843 result.SetStatus (eReturnStatusFailed);
844 }
845 return result.Succeeded();
846 }
Jim Ingham124e6902012-08-11 01:27:55 +0000847
848 Options *
849 GetOptions ()
850 {
851 return &m_options;
852 }
853
854 CommandOptions m_options;
855
856};
857
858OptionDefinition
859CommandObjectProcessContinue::CommandOptions::g_option_table[] =
860{
861{ LLDB_OPT_SET_ALL, false, "ignore-count",'i', required_argument, NULL, 0, eArgTypeUnsignedInteger,
862 "Ignore <N> crossings of the breakpoint (if it exists) for the currently selected thread."},
863{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000864};
865
866//-------------------------------------------------------------------------
867// CommandObjectProcessDetach
868//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000869#pragma mark CommandObjectProcessDetach
Chris Lattner24943d22010-06-08 16:52:24 +0000870
Jim Inghamda26bd22012-06-08 21:56:10 +0000871class CommandObjectProcessDetach : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000872{
873public:
874
Greg Clayton238c0a12010-09-18 01:14:36 +0000875 CommandObjectProcessDetach (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000876 CommandObjectParsed (interpreter,
877 "process detach",
878 "Detach from the current process being debugged.",
879 "process detach",
880 eFlagProcessMustBeLaunched)
Chris Lattner24943d22010-06-08 16:52:24 +0000881 {
882 }
883
884 ~CommandObjectProcessDetach ()
885 {
886 }
887
Jim Inghamda26bd22012-06-08 21:56:10 +0000888protected:
Chris Lattner24943d22010-06-08 16:52:24 +0000889 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000890 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000891 CommandReturnObject &result)
892 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000893 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000894 if (process == NULL)
895 {
896 result.AppendError ("must have a valid process in order to detach");
897 result.SetStatus (eReturnStatusFailed);
898 return false;
899 }
900
Greg Clayton444e35b2011-10-19 18:09:39 +0000901 result.AppendMessageWithFormat ("Detaching from process %llu\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000902 Error error (process->Detach());
903 if (error.Success())
904 {
905 result.SetStatus (eReturnStatusSuccessFinishResult);
906 }
907 else
908 {
909 result.AppendErrorWithFormat ("Detach failed: %s\n", error.AsCString());
910 result.SetStatus (eReturnStatusFailed);
911 return false;
912 }
913 return result.Succeeded();
914 }
915};
916
917//-------------------------------------------------------------------------
Greg Claytone71e2582011-02-04 01:58:07 +0000918// CommandObjectProcessConnect
919//-------------------------------------------------------------------------
920#pragma mark CommandObjectProcessConnect
921
Jim Inghamda26bd22012-06-08 21:56:10 +0000922class CommandObjectProcessConnect : public CommandObjectParsed
Greg Claytone71e2582011-02-04 01:58:07 +0000923{
924public:
925
926 class CommandOptions : public Options
927 {
928 public:
929
Greg Claytonf15996e2011-04-07 22:46:35 +0000930 CommandOptions (CommandInterpreter &interpreter) :
931 Options(interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000932 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000933 // Keep default values of all options in one place: OptionParsingStarting ()
934 OptionParsingStarting ();
Greg Claytone71e2582011-02-04 01:58:07 +0000935 }
936
937 ~CommandOptions ()
938 {
939 }
940
941 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000942 SetOptionValue (uint32_t option_idx, const char *option_arg)
Greg Claytone71e2582011-02-04 01:58:07 +0000943 {
944 Error error;
945 char short_option = (char) m_getopt_table[option_idx].val;
946
947 switch (short_option)
948 {
949 case 'p':
950 plugin_name.assign (option_arg);
951 break;
952
953 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000954 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Greg Claytone71e2582011-02-04 01:58:07 +0000955 break;
956 }
957 return error;
958 }
959
960 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000961 OptionParsingStarting ()
Greg Claytone71e2582011-02-04 01:58:07 +0000962 {
Greg Claytone71e2582011-02-04 01:58:07 +0000963 plugin_name.clear();
964 }
965
Greg Claytonb3448432011-03-24 21:19:54 +0000966 const OptionDefinition*
Greg Claytone71e2582011-02-04 01:58:07 +0000967 GetDefinitions ()
968 {
969 return g_option_table;
970 }
971
972 // Options table: Required for subclasses of Options.
973
Greg Claytonb3448432011-03-24 21:19:54 +0000974 static OptionDefinition g_option_table[];
Greg Claytone71e2582011-02-04 01:58:07 +0000975
976 // Instance variables to hold the values for command options.
977
978 std::string plugin_name;
979 };
980
981 CommandObjectProcessConnect (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000982 CommandObjectParsed (interpreter,
983 "process connect",
984 "Connect to a remote debug service.",
985 "process connect <remote-url>",
986 0),
Greg Claytonf15996e2011-04-07 22:46:35 +0000987 m_options (interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000988 {
989 }
990
991 ~CommandObjectProcessConnect ()
992 {
993 }
994
995
Jim Inghamda26bd22012-06-08 21:56:10 +0000996 Options *
997 GetOptions ()
998 {
999 return &m_options;
1000 }
1001
1002protected:
Greg Claytone71e2582011-02-04 01:58:07 +00001003 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001004 DoExecute (Args& command,
Greg Claytone71e2582011-02-04 01:58:07 +00001005 CommandReturnObject &result)
1006 {
1007
1008 TargetSP target_sp (m_interpreter.GetDebugger().GetSelectedTarget());
1009 Error error;
Greg Clayton567e7f32011-09-22 04:58:26 +00001010 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytone71e2582011-02-04 01:58:07 +00001011 if (process)
1012 {
1013 if (process->IsAlive())
1014 {
Greg Clayton444e35b2011-10-19 18:09:39 +00001015 result.AppendErrorWithFormat ("Process %llu is currently being debugged, kill the process before connecting.\n",
Greg Claytone71e2582011-02-04 01:58:07 +00001016 process->GetID());
1017 result.SetStatus (eReturnStatusFailed);
1018 return false;
1019 }
1020 }
1021
1022 if (!target_sp)
1023 {
1024 // If there isn't a current target create one.
1025 FileSpec emptyFileSpec;
Greg Claytone71e2582011-02-04 01:58:07 +00001026
1027 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
1028 emptyFileSpec,
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001029 NULL,
Greg Claytone71e2582011-02-04 01:58:07 +00001030 false,
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001031 NULL, // No platform options
Greg Claytone71e2582011-02-04 01:58:07 +00001032 target_sp);
1033 if (!target_sp || error.Fail())
1034 {
1035 result.AppendError(error.AsCString("Error creating target"));
1036 result.SetStatus (eReturnStatusFailed);
1037 return false;
1038 }
1039 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target_sp.get());
1040 }
1041
1042 if (command.GetArgumentCount() == 1)
1043 {
1044 const char *plugin_name = NULL;
1045 if (!m_options.plugin_name.empty())
1046 plugin_name = m_options.plugin_name.c_str();
1047
1048 const char *remote_url = command.GetArgumentAtIndex(0);
Greg Clayton46c9a352012-02-09 06:16:32 +00001049 process = target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name, NULL).get();
Greg Claytone71e2582011-02-04 01:58:07 +00001050
1051 if (process)
1052 {
Jason Molendafac2e622012-09-29 04:02:01 +00001053 error = process->ConnectRemote (&process->GetTarget().GetDebugger().GetOutputStream(), remote_url);
Greg Claytone71e2582011-02-04 01:58:07 +00001054
1055 if (error.Fail())
1056 {
1057 result.AppendError(error.AsCString("Remote connect failed"));
1058 result.SetStatus (eReturnStatusFailed);
Greg Clayton0cbb93b2012-03-31 00:10:30 +00001059 target_sp->DeleteCurrentProcess();
Greg Claytone71e2582011-02-04 01:58:07 +00001060 return false;
1061 }
1062 }
1063 else
1064 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001065 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",
1066 m_cmd_name.c_str());
Greg Claytone71e2582011-02-04 01:58:07 +00001067 result.SetStatus (eReturnStatusFailed);
1068 }
1069 }
1070 else
1071 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001072 result.AppendErrorWithFormat ("'%s' takes exactly one argument:\nUsage: %s\n",
Greg Claytone71e2582011-02-04 01:58:07 +00001073 m_cmd_name.c_str(),
1074 m_cmd_syntax.c_str());
1075 result.SetStatus (eReturnStatusFailed);
1076 }
1077 return result.Succeeded();
1078 }
Greg Claytone71e2582011-02-04 01:58:07 +00001079
1080 CommandOptions m_options;
1081};
1082
Greg Claytonb3448432011-03-24 21:19:54 +00001083OptionDefinition
Greg Claytone71e2582011-02-04 01:58:07 +00001084CommandObjectProcessConnect::CommandOptions::g_option_table[] =
1085{
1086 { LLDB_OPT_SET_ALL, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
1087 { 0, false, NULL, 0 , 0, NULL, 0, eArgTypeNone, NULL }
1088};
1089
1090//-------------------------------------------------------------------------
Greg Clayton13193d52012-10-13 02:07:45 +00001091// CommandObjectProcessPlugin
1092//-------------------------------------------------------------------------
1093#pragma mark CommandObjectProcessPlugin
1094
1095class CommandObjectProcessPlugin : public CommandObjectProxy
1096{
1097public:
1098
1099 CommandObjectProcessPlugin (CommandInterpreter &interpreter) :
1100 CommandObjectProxy (interpreter,
1101 "process plugin",
1102 "Send a custom command to the current process plug-in.",
1103 "process plugin <args>",
1104 0)
1105 {
1106 }
1107
1108 ~CommandObjectProcessPlugin ()
1109 {
1110 }
1111
1112 virtual CommandObject *
1113 GetProxyCommandObject()
1114 {
1115 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
1116 if (process)
1117 return process->GetPluginCommandObject();
1118 return NULL;
1119 }
1120};
1121
1122
1123//-------------------------------------------------------------------------
Greg Clayton0baa3942010-11-04 01:54:29 +00001124// CommandObjectProcessLoad
1125//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001126#pragma mark CommandObjectProcessLoad
Greg Clayton0baa3942010-11-04 01:54:29 +00001127
Jim Inghamda26bd22012-06-08 21:56:10 +00001128class CommandObjectProcessLoad : public CommandObjectParsed
Greg Clayton0baa3942010-11-04 01:54:29 +00001129{
1130public:
1131
1132 CommandObjectProcessLoad (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001133 CommandObjectParsed (interpreter,
1134 "process load",
1135 "Load a shared library into the current process.",
1136 "process load <filename> [<filename> ...]",
1137 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
Greg Clayton0baa3942010-11-04 01:54:29 +00001138 {
1139 }
1140
1141 ~CommandObjectProcessLoad ()
1142 {
1143 }
1144
Jim Inghamda26bd22012-06-08 21:56:10 +00001145protected:
Greg Clayton0baa3942010-11-04 01:54:29 +00001146 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001147 DoExecute (Args& command,
Greg Clayton0baa3942010-11-04 01:54:29 +00001148 CommandReturnObject &result)
1149 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001150 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton0baa3942010-11-04 01:54:29 +00001151 if (process == NULL)
1152 {
1153 result.AppendError ("must have a valid process in order to load a shared library");
1154 result.SetStatus (eReturnStatusFailed);
1155 return false;
1156 }
1157
1158 const uint32_t argc = command.GetArgumentCount();
1159
1160 for (uint32_t i=0; i<argc; ++i)
1161 {
1162 Error error;
1163 const char *image_path = command.GetArgumentAtIndex(i);
1164 FileSpec image_spec (image_path, false);
Greg Claytonf2bf8702011-08-11 16:25:18 +00001165 process->GetTarget().GetPlatform()->ResolveRemotePath(image_spec, image_spec);
Greg Clayton0baa3942010-11-04 01:54:29 +00001166 uint32_t image_token = process->LoadImage(image_spec, error);
1167 if (image_token != LLDB_INVALID_IMAGE_TOKEN)
1168 {
1169 result.AppendMessageWithFormat ("Loading \"%s\"...ok\nImage %u loaded.\n", image_path, image_token);
1170 result.SetStatus (eReturnStatusSuccessFinishResult);
1171 }
1172 else
1173 {
1174 result.AppendErrorWithFormat ("failed to load '%s': %s", image_path, error.AsCString());
1175 result.SetStatus (eReturnStatusFailed);
1176 }
1177 }
1178 return result.Succeeded();
1179 }
1180};
1181
1182
1183//-------------------------------------------------------------------------
1184// CommandObjectProcessUnload
1185//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001186#pragma mark CommandObjectProcessUnload
Greg Clayton0baa3942010-11-04 01:54:29 +00001187
Jim Inghamda26bd22012-06-08 21:56:10 +00001188class CommandObjectProcessUnload : public CommandObjectParsed
Greg Clayton0baa3942010-11-04 01:54:29 +00001189{
1190public:
1191
1192 CommandObjectProcessUnload (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001193 CommandObjectParsed (interpreter,
1194 "process unload",
1195 "Unload a shared library from the current process using the index returned by a previous call to \"process load\".",
1196 "process unload <index>",
1197 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
Greg Clayton0baa3942010-11-04 01:54:29 +00001198 {
1199 }
1200
1201 ~CommandObjectProcessUnload ()
1202 {
1203 }
1204
Jim Inghamda26bd22012-06-08 21:56:10 +00001205protected:
Greg Clayton0baa3942010-11-04 01:54:29 +00001206 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001207 DoExecute (Args& command,
Greg Clayton0baa3942010-11-04 01:54:29 +00001208 CommandReturnObject &result)
1209 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001210 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton0baa3942010-11-04 01:54:29 +00001211 if (process == NULL)
1212 {
1213 result.AppendError ("must have a valid process in order to load a shared library");
1214 result.SetStatus (eReturnStatusFailed);
1215 return false;
1216 }
1217
1218 const uint32_t argc = command.GetArgumentCount();
1219
1220 for (uint32_t i=0; i<argc; ++i)
1221 {
1222 const char *image_token_cstr = command.GetArgumentAtIndex(i);
1223 uint32_t image_token = Args::StringToUInt32(image_token_cstr, LLDB_INVALID_IMAGE_TOKEN, 0);
1224 if (image_token == LLDB_INVALID_IMAGE_TOKEN)
1225 {
1226 result.AppendErrorWithFormat ("invalid image index argument '%s'", image_token_cstr);
1227 result.SetStatus (eReturnStatusFailed);
1228 break;
1229 }
1230 else
1231 {
1232 Error error (process->UnloadImage(image_token));
1233 if (error.Success())
1234 {
1235 result.AppendMessageWithFormat ("Unloading shared library with index %u...ok\n", image_token);
1236 result.SetStatus (eReturnStatusSuccessFinishResult);
1237 }
1238 else
1239 {
1240 result.AppendErrorWithFormat ("failed to unload image: %s", error.AsCString());
1241 result.SetStatus (eReturnStatusFailed);
1242 break;
1243 }
1244 }
1245 }
1246 return result.Succeeded();
1247 }
1248};
1249
1250//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001251// CommandObjectProcessSignal
1252//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001253#pragma mark CommandObjectProcessSignal
Chris Lattner24943d22010-06-08 16:52:24 +00001254
Jim Inghamda26bd22012-06-08 21:56:10 +00001255class CommandObjectProcessSignal : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001256{
1257public:
1258
Greg Clayton238c0a12010-09-18 01:14:36 +00001259 CommandObjectProcessSignal (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001260 CommandObjectParsed (interpreter,
1261 "process signal",
1262 "Send a UNIX signal to the current process being debugged.",
1263 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001264 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001265 CommandArgumentEntry arg;
1266 CommandArgumentData signal_arg;
1267
1268 // Define the first (and only) variant of this arg.
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001269 signal_arg.arg_type = eArgTypeUnixSignal;
Caroline Tice43b014a2010-10-04 22:28:36 +00001270 signal_arg.arg_repetition = eArgRepeatPlain;
1271
1272 // There is only one variant this argument could be; put it into the argument entry.
1273 arg.push_back (signal_arg);
1274
1275 // Push the data for the first argument into the m_arguments vector.
1276 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001277 }
1278
1279 ~CommandObjectProcessSignal ()
1280 {
1281 }
1282
Jim Inghamda26bd22012-06-08 21:56:10 +00001283protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001284 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001285 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001286 CommandReturnObject &result)
1287 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001288 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001289 if (process == NULL)
1290 {
1291 result.AppendError ("no process to signal");
1292 result.SetStatus (eReturnStatusFailed);
1293 return false;
1294 }
1295
1296 if (command.GetArgumentCount() == 1)
1297 {
Greg Clayton8f6be2a2010-10-09 01:40:57 +00001298 int signo = LLDB_INVALID_SIGNAL_NUMBER;
1299
1300 const char *signal_name = command.GetArgumentAtIndex(0);
1301 if (::isxdigit (signal_name[0]))
1302 signo = Args::StringToSInt32(signal_name, LLDB_INVALID_SIGNAL_NUMBER, 0);
1303 else
1304 signo = process->GetUnixSignals().GetSignalNumberFromName (signal_name);
1305
1306 if (signo == LLDB_INVALID_SIGNAL_NUMBER)
Chris Lattner24943d22010-06-08 16:52:24 +00001307 {
1308 result.AppendErrorWithFormat ("Invalid signal argument '%s'.\n", command.GetArgumentAtIndex(0));
1309 result.SetStatus (eReturnStatusFailed);
1310 }
1311 else
1312 {
1313 Error error (process->Signal (signo));
1314 if (error.Success())
1315 {
1316 result.SetStatus (eReturnStatusSuccessFinishResult);
1317 }
1318 else
1319 {
1320 result.AppendErrorWithFormat ("Failed to send signal %i: %s\n", signo, error.AsCString());
1321 result.SetStatus (eReturnStatusFailed);
1322 }
1323 }
1324 }
1325 else
1326 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001327 result.AppendErrorWithFormat("'%s' takes exactly one signal number argument:\nUsage: %s\n", m_cmd_name.c_str(),
Chris Lattner24943d22010-06-08 16:52:24 +00001328 m_cmd_syntax.c_str());
1329 result.SetStatus (eReturnStatusFailed);
1330 }
1331 return result.Succeeded();
1332 }
1333};
1334
1335
1336//-------------------------------------------------------------------------
1337// CommandObjectProcessInterrupt
1338//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001339#pragma mark CommandObjectProcessInterrupt
Chris Lattner24943d22010-06-08 16:52:24 +00001340
Jim Inghamda26bd22012-06-08 21:56:10 +00001341class CommandObjectProcessInterrupt : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001342{
1343public:
1344
1345
Greg Clayton238c0a12010-09-18 01:14:36 +00001346 CommandObjectProcessInterrupt (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001347 CommandObjectParsed (interpreter,
1348 "process interrupt",
1349 "Interrupt the current process being debugged.",
1350 "process interrupt",
1351 eFlagProcessMustBeLaunched)
Chris Lattner24943d22010-06-08 16:52:24 +00001352 {
1353 }
1354
1355 ~CommandObjectProcessInterrupt ()
1356 {
1357 }
1358
Jim Inghamda26bd22012-06-08 21:56:10 +00001359protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001360 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001361 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001362 CommandReturnObject &result)
1363 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001364 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001365 if (process == NULL)
1366 {
1367 result.AppendError ("no process to halt");
1368 result.SetStatus (eReturnStatusFailed);
1369 return false;
1370 }
1371
1372 if (command.GetArgumentCount() == 0)
1373 {
1374 Error error(process->Halt ());
1375 if (error.Success())
1376 {
1377 result.SetStatus (eReturnStatusSuccessFinishResult);
1378
1379 // Maybe we should add a "SuspendThreadPlans so we
1380 // can halt, and keep in place all the current thread plans.
1381 process->GetThreadList().DiscardThreadPlans();
1382 }
1383 else
1384 {
1385 result.AppendErrorWithFormat ("Failed to halt process: %s\n", error.AsCString());
1386 result.SetStatus (eReturnStatusFailed);
1387 }
1388 }
1389 else
1390 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001391 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner24943d22010-06-08 16:52:24 +00001392 m_cmd_name.c_str(),
1393 m_cmd_syntax.c_str());
1394 result.SetStatus (eReturnStatusFailed);
1395 }
1396 return result.Succeeded();
1397 }
1398};
1399
1400//-------------------------------------------------------------------------
1401// CommandObjectProcessKill
1402//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001403#pragma mark CommandObjectProcessKill
Chris Lattner24943d22010-06-08 16:52:24 +00001404
Jim Inghamda26bd22012-06-08 21:56:10 +00001405class CommandObjectProcessKill : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001406{
1407public:
1408
Greg Clayton238c0a12010-09-18 01:14:36 +00001409 CommandObjectProcessKill (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001410 CommandObjectParsed (interpreter,
1411 "process kill",
1412 "Terminate the current process being debugged.",
1413 "process kill",
1414 eFlagProcessMustBeLaunched)
Chris Lattner24943d22010-06-08 16:52:24 +00001415 {
1416 }
1417
1418 ~CommandObjectProcessKill ()
1419 {
1420 }
1421
Jim Inghamda26bd22012-06-08 21:56:10 +00001422protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001423 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001424 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001425 CommandReturnObject &result)
1426 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001427 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001428 if (process == NULL)
1429 {
1430 result.AppendError ("no process to kill");
1431 result.SetStatus (eReturnStatusFailed);
1432 return false;
1433 }
1434
1435 if (command.GetArgumentCount() == 0)
1436 {
1437 Error error (process->Destroy());
1438 if (error.Success())
1439 {
1440 result.SetStatus (eReturnStatusSuccessFinishResult);
1441 }
1442 else
1443 {
1444 result.AppendErrorWithFormat ("Failed to kill process: %s\n", error.AsCString());
1445 result.SetStatus (eReturnStatusFailed);
1446 }
1447 }
1448 else
1449 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001450 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner24943d22010-06-08 16:52:24 +00001451 m_cmd_name.c_str(),
1452 m_cmd_syntax.c_str());
1453 result.SetStatus (eReturnStatusFailed);
1454 }
1455 return result.Succeeded();
1456 }
1457};
1458
1459//-------------------------------------------------------------------------
Jim Ingham41313fc2010-06-18 01:23:09 +00001460// CommandObjectProcessStatus
1461//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001462#pragma mark CommandObjectProcessStatus
1463
Jim Inghamda26bd22012-06-08 21:56:10 +00001464class CommandObjectProcessStatus : public CommandObjectParsed
Jim Ingham41313fc2010-06-18 01:23:09 +00001465{
1466public:
Greg Clayton238c0a12010-09-18 01:14:36 +00001467 CommandObjectProcessStatus (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001468 CommandObjectParsed (interpreter,
1469 "process status",
1470 "Show the current status and location of executing process.",
1471 "process status",
1472 0)
Jim Ingham41313fc2010-06-18 01:23:09 +00001473 {
1474 }
1475
1476 ~CommandObjectProcessStatus()
1477 {
1478 }
1479
1480
1481 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001482 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham41313fc2010-06-18 01:23:09 +00001483 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001484 Stream &strm = result.GetOutputStream();
Jim Ingham41313fc2010-06-18 01:23:09 +00001485 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Claytonb72d0f02011-04-12 05:54:46 +00001486 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
Greg Clayton567e7f32011-09-22 04:58:26 +00001487 Process *process = exe_ctx.GetProcessPtr();
1488 if (process)
Jim Ingham41313fc2010-06-18 01:23:09 +00001489 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001490 const bool only_threads_with_stop_reason = true;
1491 const uint32_t start_frame = 0;
1492 const uint32_t num_frames = 1;
1493 const uint32_t num_frames_with_source = 1;
Greg Clayton567e7f32011-09-22 04:58:26 +00001494 process->GetStatus(strm);
1495 process->GetThreadStatus (strm,
1496 only_threads_with_stop_reason,
1497 start_frame,
1498 num_frames,
1499 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001500
Jim Ingham41313fc2010-06-18 01:23:09 +00001501 }
1502 else
1503 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001504 result.AppendError ("No process.");
Jim Ingham41313fc2010-06-18 01:23:09 +00001505 result.SetStatus (eReturnStatusFailed);
1506 }
1507 return result.Succeeded();
1508 }
1509};
1510
1511//-------------------------------------------------------------------------
Caroline Tice23d6f272010-10-13 20:44:39 +00001512// CommandObjectProcessHandle
1513//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001514#pragma mark CommandObjectProcessHandle
Caroline Tice23d6f272010-10-13 20:44:39 +00001515
Jim Inghamda26bd22012-06-08 21:56:10 +00001516class CommandObjectProcessHandle : public CommandObjectParsed
Caroline Tice23d6f272010-10-13 20:44:39 +00001517{
1518public:
1519
1520 class CommandOptions : public Options
1521 {
1522 public:
1523
Greg Claytonf15996e2011-04-07 22:46:35 +00001524 CommandOptions (CommandInterpreter &interpreter) :
1525 Options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001526 {
Greg Clayton143fcc32011-04-13 00:18:08 +00001527 OptionParsingStarting ();
Caroline Tice23d6f272010-10-13 20:44:39 +00001528 }
1529
1530 ~CommandOptions ()
1531 {
1532 }
1533
1534 Error
Greg Clayton143fcc32011-04-13 00:18:08 +00001535 SetOptionValue (uint32_t option_idx, const char *option_arg)
Caroline Tice23d6f272010-10-13 20:44:39 +00001536 {
1537 Error error;
1538 char short_option = (char) m_getopt_table[option_idx].val;
1539
1540 switch (short_option)
1541 {
1542 case 's':
1543 stop = option_arg;
1544 break;
1545 case 'n':
1546 notify = option_arg;
1547 break;
1548 case 'p':
1549 pass = option_arg;
1550 break;
1551 default:
Greg Clayton9c236732011-10-26 00:56:27 +00001552 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Caroline Tice23d6f272010-10-13 20:44:39 +00001553 break;
1554 }
1555 return error;
1556 }
1557
1558 void
Greg Clayton143fcc32011-04-13 00:18:08 +00001559 OptionParsingStarting ()
Caroline Tice23d6f272010-10-13 20:44:39 +00001560 {
Caroline Tice23d6f272010-10-13 20:44:39 +00001561 stop.clear();
1562 notify.clear();
1563 pass.clear();
1564 }
1565
Greg Claytonb3448432011-03-24 21:19:54 +00001566 const OptionDefinition*
Caroline Tice23d6f272010-10-13 20:44:39 +00001567 GetDefinitions ()
1568 {
1569 return g_option_table;
1570 }
1571
1572 // Options table: Required for subclasses of Options.
1573
Greg Claytonb3448432011-03-24 21:19:54 +00001574 static OptionDefinition g_option_table[];
Caroline Tice23d6f272010-10-13 20:44:39 +00001575
1576 // Instance variables to hold the values for command options.
1577
1578 std::string stop;
1579 std::string notify;
1580 std::string pass;
1581 };
1582
1583
1584 CommandObjectProcessHandle (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001585 CommandObjectParsed (interpreter,
1586 "process handle",
1587 "Show or update what the process and debugger should do with various signals received from the OS.",
1588 NULL),
Greg Claytonf15996e2011-04-07 22:46:35 +00001589 m_options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001590 {
Caroline Ticee7471982010-10-14 21:31:13 +00001591 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 +00001592 CommandArgumentEntry arg;
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001593 CommandArgumentData signal_arg;
Caroline Tice23d6f272010-10-13 20:44:39 +00001594
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001595 signal_arg.arg_type = eArgTypeUnixSignal;
1596 signal_arg.arg_repetition = eArgRepeatStar;
Caroline Tice23d6f272010-10-13 20:44:39 +00001597
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001598 arg.push_back (signal_arg);
Caroline Tice23d6f272010-10-13 20:44:39 +00001599
1600 m_arguments.push_back (arg);
1601 }
1602
1603 ~CommandObjectProcessHandle ()
1604 {
1605 }
1606
1607 Options *
1608 GetOptions ()
1609 {
1610 return &m_options;
1611 }
1612
1613 bool
Caroline Ticee7471982010-10-14 21:31:13 +00001614 VerifyCommandOptionValue (const std::string &option, int &real_value)
Caroline Tice23d6f272010-10-13 20:44:39 +00001615 {
1616 bool okay = true;
1617
Caroline Ticee7471982010-10-14 21:31:13 +00001618 bool success = false;
1619 bool tmp_value = Args::StringToBoolean (option.c_str(), false, &success);
1620
1621 if (success && tmp_value)
1622 real_value = 1;
1623 else if (success && !tmp_value)
1624 real_value = 0;
Caroline Tice23d6f272010-10-13 20:44:39 +00001625 else
1626 {
1627 // If the value isn't 'true' or 'false', it had better be 0 or 1.
Caroline Ticee7471982010-10-14 21:31:13 +00001628 real_value = Args::StringToUInt32 (option.c_str(), 3);
1629 if (real_value != 0 && real_value != 1)
Caroline Tice23d6f272010-10-13 20:44:39 +00001630 okay = false;
1631 }
1632
1633 return okay;
1634 }
1635
Caroline Ticee7471982010-10-14 21:31:13 +00001636 void
1637 PrintSignalHeader (Stream &str)
1638 {
1639 str.Printf ("NAME PASS STOP NOTIFY\n");
1640 str.Printf ("========== ===== ===== ======\n");
1641 }
1642
1643 void
1644 PrintSignal (Stream &str, int32_t signo, const char *sig_name, UnixSignals &signals)
1645 {
1646 bool stop;
1647 bool suppress;
1648 bool notify;
1649
1650 str.Printf ("%-10s ", sig_name);
1651 if (signals.GetSignalInfo (signo, suppress, stop, notify))
1652 {
1653 bool pass = !suppress;
1654 str.Printf ("%s %s %s",
1655 (pass ? "true " : "false"),
1656 (stop ? "true " : "false"),
1657 (notify ? "true " : "false"));
1658 }
1659 str.Printf ("\n");
1660 }
1661
1662 void
1663 PrintSignalInformation (Stream &str, Args &signal_args, int num_valid_signals, UnixSignals &signals)
1664 {
1665 PrintSignalHeader (str);
1666
1667 if (num_valid_signals > 0)
1668 {
1669 size_t num_args = signal_args.GetArgumentCount();
1670 for (size_t i = 0; i < num_args; ++i)
1671 {
1672 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1673 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
1674 PrintSignal (str, signo, signal_args.GetArgumentAtIndex (i), signals);
1675 }
1676 }
1677 else // Print info for ALL signals
1678 {
1679 int32_t signo = signals.GetFirstSignalNumber();
1680 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1681 {
1682 PrintSignal (str, signo, signals.GetSignalAsCString (signo), signals);
1683 signo = signals.GetNextSignalNumber (signo);
1684 }
1685 }
1686 }
1687
Jim Inghamda26bd22012-06-08 21:56:10 +00001688protected:
Caroline Tice23d6f272010-10-13 20:44:39 +00001689 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001690 DoExecute (Args &signal_args, CommandReturnObject &result)
Caroline Tice23d6f272010-10-13 20:44:39 +00001691 {
1692 TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
1693
1694 if (!target_sp)
1695 {
1696 result.AppendError ("No current target;"
1697 " cannot handle signals until you have a valid target and process.\n");
1698 result.SetStatus (eReturnStatusFailed);
1699 return false;
1700 }
1701
1702 ProcessSP process_sp = target_sp->GetProcessSP();
1703
1704 if (!process_sp)
1705 {
1706 result.AppendError ("No current process; cannot handle signals until you have a valid process.\n");
1707 result.SetStatus (eReturnStatusFailed);
1708 return false;
1709 }
1710
Caroline Tice23d6f272010-10-13 20:44:39 +00001711 int stop_action = -1; // -1 means leave the current setting alone
Caroline Ticee7471982010-10-14 21:31:13 +00001712 int pass_action = -1; // -1 means leave the current setting alone
Caroline Tice23d6f272010-10-13 20:44:39 +00001713 int notify_action = -1; // -1 means leave the current setting alone
1714
1715 if (! m_options.stop.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001716 && ! VerifyCommandOptionValue (m_options.stop, stop_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001717 {
1718 result.AppendError ("Invalid argument for command option --stop; must be true or false.\n");
1719 result.SetStatus (eReturnStatusFailed);
1720 return false;
1721 }
1722
1723 if (! m_options.notify.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001724 && ! VerifyCommandOptionValue (m_options.notify, notify_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001725 {
1726 result.AppendError ("Invalid argument for command option --notify; must be true or false.\n");
1727 result.SetStatus (eReturnStatusFailed);
1728 return false;
1729 }
1730
1731 if (! m_options.pass.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001732 && ! VerifyCommandOptionValue (m_options.pass, pass_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001733 {
1734 result.AppendError ("Invalid argument for command option --pass; must be true or false.\n");
1735 result.SetStatus (eReturnStatusFailed);
1736 return false;
1737 }
1738
1739 size_t num_args = signal_args.GetArgumentCount();
1740 UnixSignals &signals = process_sp->GetUnixSignals();
1741 int num_signals_set = 0;
1742
Caroline Ticee7471982010-10-14 21:31:13 +00001743 if (num_args > 0)
Caroline Tice23d6f272010-10-13 20:44:39 +00001744 {
Caroline Ticee7471982010-10-14 21:31:13 +00001745 for (size_t i = 0; i < num_args; ++i)
Caroline Tice23d6f272010-10-13 20:44:39 +00001746 {
Caroline Ticee7471982010-10-14 21:31:13 +00001747 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1748 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
Caroline Tice23d6f272010-10-13 20:44:39 +00001749 {
Caroline Ticee7471982010-10-14 21:31:13 +00001750 // Casting the actions as bools here should be okay, because VerifyCommandOptionValue guarantees
1751 // the value is either 0 or 1.
1752 if (stop_action != -1)
1753 signals.SetShouldStop (signo, (bool) stop_action);
1754 if (pass_action != -1)
1755 {
1756 bool suppress = ! ((bool) pass_action);
1757 signals.SetShouldSuppress (signo, suppress);
1758 }
1759 if (notify_action != -1)
1760 signals.SetShouldNotify (signo, (bool) notify_action);
1761 ++num_signals_set;
Caroline Tice23d6f272010-10-13 20:44:39 +00001762 }
Caroline Ticee7471982010-10-14 21:31:13 +00001763 else
1764 {
1765 result.AppendErrorWithFormat ("Invalid signal name '%s'\n", signal_args.GetArgumentAtIndex (i));
1766 }
Caroline Tice23d6f272010-10-13 20:44:39 +00001767 }
1768 }
Caroline Ticee7471982010-10-14 21:31:13 +00001769 else
1770 {
1771 // No signal specified, if any command options were specified, update ALL signals.
1772 if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1))
1773 {
1774 if (m_interpreter.Confirm ("Do you really want to update all the signals?", false))
1775 {
1776 int32_t signo = signals.GetFirstSignalNumber();
1777 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1778 {
1779 if (notify_action != -1)
1780 signals.SetShouldNotify (signo, (bool) notify_action);
1781 if (stop_action != -1)
1782 signals.SetShouldStop (signo, (bool) stop_action);
1783 if (pass_action != -1)
1784 {
1785 bool suppress = ! ((bool) pass_action);
1786 signals.SetShouldSuppress (signo, suppress);
1787 }
1788 signo = signals.GetNextSignalNumber (signo);
1789 }
1790 }
1791 }
1792 }
1793
1794 PrintSignalInformation (result.GetOutputStream(), signal_args, num_signals_set, signals);
Caroline Tice23d6f272010-10-13 20:44:39 +00001795
1796 if (num_signals_set > 0)
1797 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1798 else
1799 result.SetStatus (eReturnStatusFailed);
1800
1801 return result.Succeeded();
1802 }
1803
Caroline Tice23d6f272010-10-13 20:44:39 +00001804 CommandOptions m_options;
1805};
1806
Greg Claytonb3448432011-03-24 21:19:54 +00001807OptionDefinition
Caroline Tice23d6f272010-10-13 20:44:39 +00001808CommandObjectProcessHandle::CommandOptions::g_option_table[] =
1809{
1810{ 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." },
1811{ 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." },
1812{ LLDB_OPT_SET_1, false, "pass", 'p', required_argument, NULL, 0, eArgTypeBoolean, "Whether or not the signal should be passed to the process." },
1813{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1814};
1815
1816//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001817// CommandObjectMultiwordProcess
1818//-------------------------------------------------------------------------
1819
Greg Clayton63094e02010-06-23 01:19:29 +00001820CommandObjectMultiwordProcess::CommandObjectMultiwordProcess (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00001821 CommandObjectMultiword (interpreter,
1822 "process",
1823 "A set of commands for operating on a process.",
1824 "process <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00001825{
Greg Claytona9eb8272011-07-02 21:07:54 +00001826 LoadSubCommand ("attach", CommandObjectSP (new CommandObjectProcessAttach (interpreter)));
1827 LoadSubCommand ("launch", CommandObjectSP (new CommandObjectProcessLaunch (interpreter)));
1828 LoadSubCommand ("continue", CommandObjectSP (new CommandObjectProcessContinue (interpreter)));
1829 LoadSubCommand ("connect", CommandObjectSP (new CommandObjectProcessConnect (interpreter)));
1830 LoadSubCommand ("detach", CommandObjectSP (new CommandObjectProcessDetach (interpreter)));
1831 LoadSubCommand ("load", CommandObjectSP (new CommandObjectProcessLoad (interpreter)));
1832 LoadSubCommand ("unload", CommandObjectSP (new CommandObjectProcessUnload (interpreter)));
1833 LoadSubCommand ("signal", CommandObjectSP (new CommandObjectProcessSignal (interpreter)));
1834 LoadSubCommand ("handle", CommandObjectSP (new CommandObjectProcessHandle (interpreter)));
1835 LoadSubCommand ("status", CommandObjectSP (new CommandObjectProcessStatus (interpreter)));
Greg Clayton238c0a12010-09-18 01:14:36 +00001836 LoadSubCommand ("interrupt", CommandObjectSP (new CommandObjectProcessInterrupt (interpreter)));
Greg Claytona9eb8272011-07-02 21:07:54 +00001837 LoadSubCommand ("kill", CommandObjectSP (new CommandObjectProcessKill (interpreter)));
Greg Clayton13193d52012-10-13 02:07:45 +00001838 LoadSubCommand ("plugin", CommandObjectSP (new CommandObjectProcessPlugin (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00001839}
1840
1841CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess ()
1842{
1843}
1844