blob: 41e216ec6cba8a8b2ca1763363d000dc8b185790 [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 Clayton36bc5ea2011-11-03 21:22:33 +0000130 exe_module->GetFileSpec().GetPath (filename, sizeof(filename));
Chris Lattner24943d22010-06-08 16:52:24 +0000131
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000132 const bool add_exe_file_as_first_arg = true;
Greg Clayton1d1f39e2011-11-29 04:03:30 +0000133 m_options.launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), add_exe_file_as_first_arg);
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000134
Greg Claytona2f74232011-02-24 22:24:29 +0000135 StateType state = eStateInvalid;
Greg Clayton567e7f32011-09-22 04:58:26 +0000136 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytona2f74232011-02-24 22:24:29 +0000137 if (process)
138 {
139 state = process->GetState();
140
141 if (process->IsAlive() && state != eStateConnected)
142 {
143 char message[1024];
144 if (process->GetState() == eStateAttaching)
145 ::strncpy (message, "There is a pending attach, abort it and launch a new process?", sizeof(message));
146 else
147 ::strncpy (message, "There is a running process, kill it and restart?", sizeof(message));
148
149 if (!m_interpreter.Confirm (message, true))
Jim Ingham22dc9722010-12-09 18:58:16 +0000150 {
Greg Claytona2f74232011-02-24 22:24:29 +0000151 result.SetStatus (eReturnStatusFailed);
152 return false;
Jim Ingham22dc9722010-12-09 18:58:16 +0000153 }
154 else
155 {
Greg Claytonabb33022011-11-08 02:43:13 +0000156 Error destroy_error (process->Destroy());
157 if (destroy_error.Success())
Greg Claytona2f74232011-02-24 22:24:29 +0000158 {
159 result.SetStatus (eReturnStatusSuccessFinishResult);
160 }
161 else
162 {
Greg Claytonabb33022011-11-08 02:43:13 +0000163 result.AppendErrorWithFormat ("Failed to kill process: %s\n", destroy_error.AsCString());
Greg Claytona2f74232011-02-24 22:24:29 +0000164 result.SetStatus (eReturnStatusFailed);
165 }
Jim Ingham22dc9722010-12-09 18:58:16 +0000166 }
167 }
Chris Lattner24943d22010-06-08 16:52:24 +0000168 }
Jim Ingham22dc9722010-12-09 18:58:16 +0000169
Greg Clayton527154d2011-11-15 03:53:30 +0000170 if (launch_args.GetArgumentCount() == 0)
171 {
Greg Clayton73844aa2012-08-22 17:17:09 +0000172 Args target_setting_args;
173 if (target->GetRunArguments(target_setting_args) > 0)
174 m_options.launch_info.GetArguments().AppendArguments (target_setting_args);
Greg Clayton527154d2011-11-15 03:53:30 +0000175 }
176 else
Greg Claytonabb33022011-11-08 02:43:13 +0000177 {
Greg Clayton3e6f2cc2011-11-21 21:51:18 +0000178 // Save the arguments for subsequent runs in the current target.
179 target->SetRunArguments (launch_args);
180
Greg Claytonabb33022011-11-08 02:43:13 +0000181 m_options.launch_info.GetArguments().AppendArguments (launch_args);
182 }
Greg Claytonabb33022011-11-08 02:43:13 +0000183
Greg Clayton527154d2011-11-15 03:53:30 +0000184 if (target->GetDisableASLR())
185 m_options.launch_info.GetFlags().Set (eLaunchFlagDisableASLR);
186
187 if (target->GetDisableSTDIO())
188 m_options.launch_info.GetFlags().Set (eLaunchFlagDisableSTDIO);
189
190 m_options.launch_info.GetFlags().Set (eLaunchFlagDebug);
191
192 Args environment;
193 target->GetEnvironmentAsArgs (environment);
194 if (environment.GetArgumentCount() > 0)
195 m_options.launch_info.GetEnvironmentEntries ().AppendArguments (environment);
196
Greg Clayton464c6162011-11-17 22:14:31 +0000197 // Finalize the file actions, and if none were given, default to opening
198 // up a pseudo terminal
199 const bool default_to_use_pty = true;
200 m_options.launch_info.FinalizeFileActions (target, default_to_use_pty);
Greg Clayton527154d2011-11-15 03:53:30 +0000201
Greg Claytonabb33022011-11-08 02:43:13 +0000202 if (state == eStateConnected)
203 {
204 if (m_options.launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
205 {
206 result.AppendWarning("can't launch in tty when launching through a remote connection");
207 m_options.launch_info.GetFlags().Clear (eLaunchFlagLaunchInTTY);
208 }
209 }
210 else
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000211 {
Greg Clayton527154d2011-11-15 03:53:30 +0000212 if (!m_options.launch_info.GetArchitecture().IsValid())
Greg Clayton2d9adb72011-11-12 02:10:56 +0000213 m_options.launch_info.GetArchitecture() = target->GetArchitecture();
214
Greg Clayton75d8c252011-11-28 01:45:00 +0000215 PlatformSP platform_sp (target->GetPlatform());
216
217 if (platform_sp && platform_sp->CanDebugProcess ())
218 {
219 process = target->GetPlatform()->DebugProcess (m_options.launch_info,
220 debugger,
221 target,
222 debugger.GetListener(),
223 error).get();
224 }
225 else
226 {
227 const char *plugin_name = m_options.launch_info.GetProcessPluginName();
Greg Clayton46c9a352012-02-09 06:16:32 +0000228 process = target->CreateProcess (debugger.GetListener(), plugin_name, NULL).get();
Greg Clayton75d8c252011-11-28 01:45:00 +0000229 if (process)
230 error = process->Launch (m_options.launch_info);
231 }
Greg Claytonabb33022011-11-08 02:43:13 +0000232
Greg Claytona2f74232011-02-24 22:24:29 +0000233 if (process == NULL)
234 {
Greg Clayton527154d2011-11-15 03:53:30 +0000235 result.SetError (error, "failed to launch or debug process");
Greg Claytona2f74232011-02-24 22:24:29 +0000236 return false;
237 }
Chris Lattner24943d22010-06-08 16:52:24 +0000238 }
Greg Claytonabb33022011-11-08 02:43:13 +0000239
Greg Clayton238c0a12010-09-18 01:14:36 +0000240 if (error.Success())
241 {
Greg Clayton940b1032011-02-23 00:35:02 +0000242 const char *archname = exe_module->GetArchitecture().GetArchitectureName();
Greg Claytonc1d37752010-10-18 01:45:30 +0000243
Greg Clayton444e35b2011-10-19 18:09:39 +0000244 result.AppendMessageWithFormat ("Process %llu launched: '%s' (%s)\n", process->GetID(), filename, archname);
Greg Claytond8c62532010-10-07 04:19:01 +0000245 result.SetDidChangeProcessState (true);
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000246 if (m_options.launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
Greg Clayton238c0a12010-09-18 01:14:36 +0000247 {
Greg Claytond8c62532010-10-07 04:19:01 +0000248 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
Greg Clayton238c0a12010-09-18 01:14:36 +0000249 StateType state = process->WaitForProcessToStop (NULL);
250
251 if (state == eStateStopped)
252 {
Greg Claytond8c62532010-10-07 04:19:01 +0000253 error = process->Resume();
254 if (error.Success())
255 {
256 bool synchronous_execution = m_interpreter.GetSynchronous ();
257 if (synchronous_execution)
258 {
259 state = process->WaitForProcessToStop (NULL);
Greg Clayton20206082011-11-17 01:23:07 +0000260 const bool must_be_alive = true;
261 if (!StateIsStoppedState(state, must_be_alive))
Greg Clayton395fc332011-02-15 21:59:32 +0000262 {
Greg Clayton527154d2011-11-15 03:53:30 +0000263 result.AppendErrorWithFormat ("process isn't stopped: %s", StateAsCString(state));
Greg Clayton395fc332011-02-15 21:59:32 +0000264 }
Greg Claytond8c62532010-10-07 04:19:01 +0000265 result.SetDidChangeProcessState (true);
266 result.SetStatus (eReturnStatusSuccessFinishResult);
267 }
268 else
269 {
270 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
271 }
272 }
Greg Clayton395fc332011-02-15 21:59:32 +0000273 else
274 {
Greg Clayton527154d2011-11-15 03:53:30 +0000275 result.AppendErrorWithFormat ("process resume at entry point failed: %s", error.AsCString());
Greg Clayton395fc332011-02-15 21:59:32 +0000276 result.SetStatus (eReturnStatusFailed);
277 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000278 }
Greg Clayton395fc332011-02-15 21:59:32 +0000279 else
280 {
Greg Clayton527154d2011-11-15 03:53:30 +0000281 result.AppendErrorWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
Greg Clayton395fc332011-02-15 21:59:32 +0000282 result.SetStatus (eReturnStatusFailed);
283 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000284 }
285 }
Greg Clayton395fc332011-02-15 21:59:32 +0000286 else
287 {
Greg Claytona9eb8272011-07-02 21:07:54 +0000288 result.AppendErrorWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton395fc332011-02-15 21:59:32 +0000289 result.SetStatus (eReturnStatusFailed);
290 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000291
Chris Lattner24943d22010-06-08 16:52:24 +0000292 return result.Succeeded();
293 }
294
295protected:
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000296 ProcessLaunchCommandOptions m_options;
Chris Lattner24943d22010-06-08 16:52:24 +0000297};
298
299
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000300//#define SET1 LLDB_OPT_SET_1
301//#define SET2 LLDB_OPT_SET_2
302//#define SET3 LLDB_OPT_SET_3
303//
304//OptionDefinition
305//CommandObjectProcessLaunch::CommandOptions::g_option_table[] =
306//{
307//{ 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."},
308//{ SET1 , false, "stdin", 'i', required_argument, NULL, 0, eArgTypePath, "Redirect stdin for the process to <path>."},
309//{ SET1 , false, "stdout", 'o', required_argument, NULL, 0, eArgTypePath, "Redirect stdout for the process to <path>."},
310//{ SET1 , false, "stderr", 'e', required_argument, NULL, 0, eArgTypePath, "Redirect stderr for the process to <path>."},
311//{ SET1 | SET2 | SET3, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
312//{ 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."},
313//{ SET3, false, "no-stdio", 'n', no_argument, NULL, 0, eArgTypeNone, "Do not set up for terminal I/O to go to running process."},
314//{ SET1 | SET2 | SET3, false, "working-dir", 'w', required_argument, NULL, 0, eArgTypePath, "Set the current working directory to <path> when running the inferior."},
315//{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
316//};
317//
318//#undef SET1
319//#undef SET2
320//#undef SET3
Chris Lattner24943d22010-06-08 16:52:24 +0000321
322//-------------------------------------------------------------------------
323// CommandObjectProcessAttach
324//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000325#pragma mark CommandObjectProcessAttach
Jim Inghamda26bd22012-06-08 21:56:10 +0000326class CommandObjectProcessAttach : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000327{
328public:
329
Chris Lattner24943d22010-06-08 16:52:24 +0000330 class CommandOptions : public Options
331 {
332 public:
333
Greg Claytonf15996e2011-04-07 22:46:35 +0000334 CommandOptions (CommandInterpreter &interpreter) :
335 Options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000336 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000337 // Keep default values of all options in one place: OptionParsingStarting ()
338 OptionParsingStarting ();
Chris Lattner24943d22010-06-08 16:52:24 +0000339 }
340
341 ~CommandOptions ()
342 {
343 }
344
345 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000346 SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +0000347 {
348 Error error;
349 char short_option = (char) m_getopt_table[option_idx].val;
350 bool success = false;
351 switch (short_option)
352 {
Johnny Chen7c099972012-05-24 00:43:00 +0000353 case 'c':
354 attach_info.SetContinueOnceAttached(true);
355 break;
356
Chris Lattner24943d22010-06-08 16:52:24 +0000357 case 'p':
Chris Lattner24943d22010-06-08 16:52:24 +0000358 {
Greg Clayton527154d2011-11-15 03:53:30 +0000359 lldb::pid_t pid = Args::StringToUInt32 (option_arg, LLDB_INVALID_PROCESS_ID, 0, &success);
360 if (!success || pid == LLDB_INVALID_PROCESS_ID)
361 {
362 error.SetErrorStringWithFormat("invalid process ID '%s'", option_arg);
363 }
364 else
365 {
366 attach_info.SetProcessID (pid);
367 }
Chris Lattner24943d22010-06-08 16:52:24 +0000368 }
369 break;
370
371 case 'P':
Greg Clayton527154d2011-11-15 03:53:30 +0000372 attach_info.SetProcessPluginName (option_arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000373 break;
374
375 case 'n':
Greg Clayton527154d2011-11-15 03:53:30 +0000376 attach_info.GetExecutableFile().SetFile(option_arg, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000377 break;
378
379 case 'w':
Greg Clayton527154d2011-11-15 03:53:30 +0000380 attach_info.SetWaitForLaunch(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000381 break;
Jim Ingham3a458eb2012-07-20 21:37:13 +0000382
383 case 'i':
384 attach_info.SetIgnoreExisting(false);
385 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000386
387 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000388 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner24943d22010-06-08 16:52:24 +0000389 break;
390 }
391 return error;
392 }
393
394 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000395 OptionParsingStarting ()
Chris Lattner24943d22010-06-08 16:52:24 +0000396 {
Greg Clayton527154d2011-11-15 03:53:30 +0000397 attach_info.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000398 }
399
Greg Claytonb3448432011-03-24 21:19:54 +0000400 const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +0000401 GetDefinitions ()
402 {
403 return g_option_table;
404 }
405
Jim Ingham7508e732010-08-09 23:31:02 +0000406 virtual bool
Greg Claytonf15996e2011-04-07 22:46:35 +0000407 HandleOptionArgumentCompletion (Args &input,
Jim Ingham7508e732010-08-09 23:31:02 +0000408 int cursor_index,
409 int char_pos,
410 OptionElementVector &opt_element_vector,
411 int opt_element_index,
412 int match_start_point,
413 int max_return_elements,
414 bool &word_complete,
415 StringList &matches)
416 {
417 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
418 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
419
420 // We are only completing the name option for now...
421
Greg Claytonb3448432011-03-24 21:19:54 +0000422 const OptionDefinition *opt_defs = GetDefinitions();
Jim Ingham7508e732010-08-09 23:31:02 +0000423 if (opt_defs[opt_defs_index].short_option == 'n')
424 {
425 // Are we in the name?
426
427 // Look to see if there is a -P argument provided, and if so use that plugin, otherwise
428 // use the default plugin.
Jim Ingham7508e732010-08-09 23:31:02 +0000429
430 const char *partial_name = NULL;
431 partial_name = input.GetArgumentAtIndex(opt_arg_pos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000432
Greg Claytonb72d0f02011-04-12 05:54:46 +0000433 PlatformSP platform_sp (m_interpreter.GetPlatform (true));
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000434 if (platform_sp)
Jim Ingham7508e732010-08-09 23:31:02 +0000435 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000436 ProcessInstanceInfoList process_infos;
437 ProcessInstanceInfoMatch match_info;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000438 if (partial_name)
439 {
Greg Clayton527154d2011-11-15 03:53:30 +0000440 match_info.GetProcessInfo().GetExecutableFile().SetFile(partial_name, false);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000441 match_info.SetNameMatchType(eNameMatchStartsWith);
442 }
443 platform_sp->FindProcesses (match_info, process_infos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000444 const uint32_t num_matches = process_infos.GetSize();
445 if (num_matches > 0)
446 {
447 for (uint32_t i=0; i<num_matches; ++i)
448 {
449 matches.AppendString (process_infos.GetProcessNameAtIndex(i),
450 process_infos.GetProcessNameLengthAtIndex(i));
451 }
452 }
Jim Ingham7508e732010-08-09 23:31:02 +0000453 }
454 }
455
456 return false;
457 }
458
Chris Lattner24943d22010-06-08 16:52:24 +0000459 // Options table: Required for subclasses of Options.
460
Greg Claytonb3448432011-03-24 21:19:54 +0000461 static OptionDefinition g_option_table[];
Chris Lattner24943d22010-06-08 16:52:24 +0000462
463 // Instance variables to hold the values for command options.
464
Greg Clayton527154d2011-11-15 03:53:30 +0000465 ProcessAttachInfo attach_info;
Chris Lattner24943d22010-06-08 16:52:24 +0000466 };
467
Greg Clayton238c0a12010-09-18 01:14:36 +0000468 CommandObjectProcessAttach (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000469 CommandObjectParsed (interpreter,
470 "process attach",
471 "Attach to a process.",
472 "process attach <cmd-options>"),
Greg Claytonf15996e2011-04-07 22:46:35 +0000473 m_options (interpreter)
Jim Ingham7508e732010-08-09 23:31:02 +0000474 {
Jim Ingham7508e732010-08-09 23:31:02 +0000475 }
476
477 ~CommandObjectProcessAttach ()
478 {
479 }
480
Jim Inghamda26bd22012-06-08 21:56:10 +0000481 Options *
482 GetOptions ()
483 {
484 return &m_options;
485 }
486
487protected:
Jim Ingham7508e732010-08-09 23:31:02 +0000488 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000489 DoExecute (Args& command,
Jim Ingham7508e732010-08-09 23:31:02 +0000490 CommandReturnObject &result)
491 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000492 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Jim Inghamee940e22011-09-15 01:08:57 +0000493 // N.B. The attach should be synchronous. It doesn't help much to get the prompt back between initiating the attach
494 // and the target actually stopping. So even if the interpreter is set to be asynchronous, we wait for the stop
495 // ourselves here.
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000496
Greg Clayton567e7f32011-09-22 04:58:26 +0000497 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytona2f74232011-02-24 22:24:29 +0000498 StateType state = eStateInvalid;
Jim Ingham7508e732010-08-09 23:31:02 +0000499 if (process)
500 {
Greg Claytona2f74232011-02-24 22:24:29 +0000501 state = process->GetState();
502 if (process->IsAlive() && state != eStateConnected)
Jim Ingham7508e732010-08-09 23:31:02 +0000503 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000504 result.AppendErrorWithFormat ("Process %llu is currently being debugged, kill the process before attaching.\n",
Jim Ingham7508e732010-08-09 23:31:02 +0000505 process->GetID());
506 result.SetStatus (eReturnStatusFailed);
507 return false;
508 }
509 }
510
511 if (target == NULL)
512 {
513 // If there isn't a current target create one.
514 TargetSP new_target_sp;
515 FileSpec emptyFileSpec;
Jim Ingham7508e732010-08-09 23:31:02 +0000516 Error error;
517
Greg Clayton238c0a12010-09-18 01:14:36 +0000518 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
519 emptyFileSpec,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000520 NULL,
Greg Clayton238c0a12010-09-18 01:14:36 +0000521 false,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000522 NULL, // No platform options
Greg Clayton238c0a12010-09-18 01:14:36 +0000523 new_target_sp);
Jim Ingham7508e732010-08-09 23:31:02 +0000524 target = new_target_sp.get();
525 if (target == NULL || error.Fail())
526 {
Greg Claytone71e2582011-02-04 01:58:07 +0000527 result.AppendError(error.AsCString("Error creating target"));
Jim Ingham7508e732010-08-09 23:31:02 +0000528 return false;
529 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000530 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target);
Jim Ingham7508e732010-08-09 23:31:02 +0000531 }
532
533 // Record the old executable module, we want to issue a warning if the process of attaching changed the
534 // current executable (like somebody said "file foo" then attached to a PID whose executable was bar.)
535
536 ModuleSP old_exec_module_sp = target->GetExecutableModule();
537 ArchSpec old_arch_spec = target->GetArchitecture();
538
539 if (command.GetArgumentCount())
540 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000541 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 +0000542 result.SetStatus (eReturnStatusFailed);
543 }
544 else
545 {
Greg Claytona2f74232011-02-24 22:24:29 +0000546 if (state != eStateConnected)
547 {
Greg Clayton527154d2011-11-15 03:53:30 +0000548 const char *plugin_name = m_options.attach_info.GetProcessPluginName();
Greg Clayton46c9a352012-02-09 06:16:32 +0000549 process = target->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name, NULL).get();
Greg Claytona2f74232011-02-24 22:24:29 +0000550 }
Jim Ingham7508e732010-08-09 23:31:02 +0000551
552 if (process)
553 {
554 Error error;
Greg Clayton527154d2011-11-15 03:53:30 +0000555 // If no process info was specified, then use the target executable
556 // name as the process to attach to by default
557 if (!m_options.attach_info.ProcessInfoSpecified ())
Jim Ingham4805a1c2010-09-15 01:34:14 +0000558 {
559 if (old_exec_module_sp)
Greg Clayton1d1f39e2011-11-29 04:03:30 +0000560 m_options.attach_info.GetExecutableFile().GetFilename() = old_exec_module_sp->GetPlatformFileSpec().GetFilename();
Jim Ingham4805a1c2010-09-15 01:34:14 +0000561
Greg Clayton527154d2011-11-15 03:53:30 +0000562 if (!m_options.attach_info.ProcessInfoSpecified ())
563 {
564 error.SetErrorString ("no process specified, create a target with a file, or specify the --pid or --name command option");
565 }
566 }
567
568 if (error.Success())
569 {
570 error = process->Attach (m_options.attach_info);
571
Jim Ingham4805a1c2010-09-15 01:34:14 +0000572 if (error.Success())
573 {
574 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
575 }
Jim Ingham7508e732010-08-09 23:31:02 +0000576 else
577 {
Greg Clayton527154d2011-11-15 03:53:30 +0000578 result.AppendErrorWithFormat ("attach failed: %s\n", error.AsCString());
Jim Ingham4805a1c2010-09-15 01:34:14 +0000579 result.SetStatus (eReturnStatusFailed);
580 return false;
Jim Ingham7508e732010-08-09 23:31:02 +0000581 }
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000582 // If we're synchronous, wait for the stopped event and report that.
583 // Otherwise just return.
584 // FIXME: in the async case it will now be possible to get to the command
585 // interpreter with a state eStateAttaching. Make sure we handle that correctly.
Jim Inghamee940e22011-09-15 01:08:57 +0000586 StateType state = process->WaitForProcessToStop (NULL);
Greg Clayton527154d2011-11-15 03:53:30 +0000587
Jim Inghamee940e22011-09-15 01:08:57 +0000588 result.SetDidChangeProcessState (true);
Johnny Chen9986a3b2012-05-18 00:51:36 +0000589
590 if (state == eStateStopped)
591 {
592 result.AppendMessageWithFormat ("Process %llu %s\n", process->GetID(), StateAsCString (state));
593 result.SetStatus (eReturnStatusSuccessFinishNoResult);
594 }
595 else
596 {
597 result.AppendError ("attach failed: process did not stop (no such process or permission problem?)");
Jim Ingham5d90ade2012-07-27 23:57:19 +0000598 process->Destroy();
Johnny Chen9986a3b2012-05-18 00:51:36 +0000599 result.SetStatus (eReturnStatusFailed);
600 return false;
601 }
Jim Ingham7508e732010-08-09 23:31:02 +0000602 }
Jim Ingham7508e732010-08-09 23:31:02 +0000603 }
604 }
605
606 if (result.Succeeded())
607 {
608 // Okay, we're done. Last step is to warn if the executable module has changed:
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000609 char new_path[PATH_MAX];
Greg Clayton5beb99d2011-08-11 02:48:45 +0000610 ModuleSP new_exec_module_sp (target->GetExecutableModule());
Jim Ingham7508e732010-08-09 23:31:02 +0000611 if (!old_exec_module_sp)
612 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000613 // We might not have a module if we attached to a raw pid...
Greg Clayton5beb99d2011-08-11 02:48:45 +0000614 if (new_exec_module_sp)
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000615 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000616 new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000617 result.AppendMessageWithFormat("Executable module set to \"%s\".\n", new_path);
618 }
Jim Ingham7508e732010-08-09 23:31:02 +0000619 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000620 else if (old_exec_module_sp->GetFileSpec() != new_exec_module_sp->GetFileSpec())
Jim Ingham7508e732010-08-09 23:31:02 +0000621 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000622 char old_path[PATH_MAX];
Jim Ingham7508e732010-08-09 23:31:02 +0000623
Greg Clayton5beb99d2011-08-11 02:48:45 +0000624 old_exec_module_sp->GetFileSpec().GetPath (old_path, PATH_MAX);
625 new_exec_module_sp->GetFileSpec().GetPath (new_path, PATH_MAX);
Jim Ingham7508e732010-08-09 23:31:02 +0000626
627 result.AppendWarningWithFormat("Executable module changed from \"%s\" to \"%s\".\n",
628 old_path, new_path);
629 }
630
631 if (!old_arch_spec.IsValid())
632 {
Greg Clayton92cb9a92012-09-14 02:41:36 +0000633 result.AppendMessageWithFormat ("Architecture set to: %s.\n", target->GetArchitecture().GetTriple().getTriple().c_str());
Jim Ingham7508e732010-08-09 23:31:02 +0000634 }
635 else if (old_arch_spec != target->GetArchitecture())
636 {
637 result.AppendWarningWithFormat("Architecture changed from %s to %s.\n",
Greg Clayton92cb9a92012-09-14 02:41:36 +0000638 old_arch_spec.GetTriple().getTriple().c_str(),
639 target->GetArchitecture().GetTriple().getTriple().c_str());
Jim Ingham7508e732010-08-09 23:31:02 +0000640 }
Johnny Chen7c099972012-05-24 00:43:00 +0000641
642 // This supports the use-case scenario of immediately continuing the process once attached.
643 if (m_options.attach_info.GetContinueOnceAttached())
Sean Callanan4336d932012-05-31 01:30:08 +0000644 m_interpreter.HandleCommand("process continue", eLazyBoolNo, result);
Jim Ingham7508e732010-08-09 23:31:02 +0000645 }
646 return result.Succeeded();
647 }
648
Chris Lattner24943d22010-06-08 16:52:24 +0000649 CommandOptions m_options;
650};
651
652
Greg Claytonb3448432011-03-24 21:19:54 +0000653OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000654CommandObjectProcessAttach::CommandOptions::g_option_table[] =
655{
Jim Ingham3a458eb2012-07-20 21:37:13 +0000656{ LLDB_OPT_SET_ALL, false, "continue",'c', no_argument, NULL, 0, eArgTypeNone, "Immediately continue the process once attached."},
657{ LLDB_OPT_SET_ALL, false, "plugin", 'P', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
658{ LLDB_OPT_SET_1, false, "pid", 'p', required_argument, NULL, 0, eArgTypePid, "The process ID of an existing process to attach to."},
659{ LLDB_OPT_SET_2, false, "name", 'n', required_argument, NULL, 0, eArgTypeProcessName, "The name of the process to attach to."},
660{ LLDB_OPT_SET_2, false, "include-existing", 'i', no_argument, NULL, 0, eArgTypeNone, "Include existing processes when doing attach -w."},
661{ 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 +0000662{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000663};
664
665//-------------------------------------------------------------------------
666// CommandObjectProcessContinue
667//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000668#pragma mark CommandObjectProcessContinue
Chris Lattner24943d22010-06-08 16:52:24 +0000669
Jim Inghamda26bd22012-06-08 21:56:10 +0000670class CommandObjectProcessContinue : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000671{
672public:
673
Greg Clayton238c0a12010-09-18 01:14:36 +0000674 CommandObjectProcessContinue (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000675 CommandObjectParsed (interpreter,
676 "process continue",
677 "Continue execution of all threads in the current process.",
678 "process continue",
Jim Ingham124e6902012-08-11 01:27:55 +0000679 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused),
680 m_options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000681 {
682 }
683
684
685 ~CommandObjectProcessContinue ()
686 {
687 }
688
Jim Inghamda26bd22012-06-08 21:56:10 +0000689protected:
Jim Ingham124e6902012-08-11 01:27:55 +0000690
691 class CommandOptions : public Options
692 {
693 public:
694
695 CommandOptions (CommandInterpreter &interpreter) :
696 Options(interpreter)
697 {
698 // Keep default values of all options in one place: OptionParsingStarting ()
699 OptionParsingStarting ();
700 }
701
702 ~CommandOptions ()
703 {
704 }
705
706 Error
707 SetOptionValue (uint32_t option_idx, const char *option_arg)
708 {
709 Error error;
710 char short_option = (char) m_getopt_table[option_idx].val;
711 bool success = false;
712 switch (short_option)
713 {
714 case 'i':
715 m_ignore = Args::StringToUInt32 (option_arg, 0, 0, &success);
716 if (!success)
717 error.SetErrorStringWithFormat ("invalid value for ignore option: \"%s\", should be a number.", option_arg);
718 break;
719
720 default:
721 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
722 break;
723 }
724 return error;
725 }
726
727 void
728 OptionParsingStarting ()
729 {
730 m_ignore = 0;
731 }
732
733 const OptionDefinition*
734 GetDefinitions ()
735 {
736 return g_option_table;
737 }
738
739 // Options table: Required for subclasses of Options.
740
741 static OptionDefinition g_option_table[];
742
743 uint32_t m_ignore;
744 };
745
Chris Lattner24943d22010-06-08 16:52:24 +0000746 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000747 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000748 CommandReturnObject &result)
749 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000750 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton238c0a12010-09-18 01:14:36 +0000751 bool synchronous_execution = m_interpreter.GetSynchronous ();
Chris Lattner24943d22010-06-08 16:52:24 +0000752
753 if (process == NULL)
754 {
755 result.AppendError ("no process to continue");
756 result.SetStatus (eReturnStatusFailed);
757 return false;
758 }
759
760 StateType state = process->GetState();
761 if (state == eStateStopped)
762 {
763 if (command.GetArgumentCount() != 0)
764 {
765 result.AppendErrorWithFormat ("The '%s' command does not take any arguments.\n", m_cmd_name.c_str());
766 result.SetStatus (eReturnStatusFailed);
767 return false;
768 }
769
Jim Ingham124e6902012-08-11 01:27:55 +0000770 if (m_options.m_ignore > 0)
771 {
772 ThreadSP sel_thread_sp(process->GetThreadList().GetSelectedThread());
773 if (sel_thread_sp)
774 {
775 StopInfoSP stop_info_sp = sel_thread_sp->GetStopInfo();
776 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint)
777 {
778 uint64_t bp_site_id = stop_info_sp->GetValue();
779 BreakpointSiteSP bp_site_sp(process->GetBreakpointSiteList().FindByID(bp_site_id));
780 if (bp_site_sp)
781 {
782 uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
783 for (uint32_t i = 0; i < num_owners; i++)
784 {
785 Breakpoint &bp_ref = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
786 if (!bp_ref.IsInternal())
787 {
788 bp_ref.SetIgnoreCount(m_options.m_ignore);
789 }
790 }
791 }
792 }
793 }
794 }
795
Jim Inghamb9950592012-09-10 20:50:15 +0000796 { // Scope for thread list mutex:
797 Mutex::Locker locker (process->GetThreadList().GetMutex());
798 const uint32_t num_threads = process->GetThreadList().GetSize();
Chris Lattner24943d22010-06-08 16:52:24 +0000799
Jim Inghamb9950592012-09-10 20:50:15 +0000800 // Set the actions that the threads should each take when resuming
801 for (uint32_t idx=0; idx<num_threads; ++idx)
802 {
803 process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState (eStateRunning);
804 }
Chris Lattner24943d22010-06-08 16:52:24 +0000805 }
Jim Inghamb9950592012-09-10 20:50:15 +0000806
Chris Lattner24943d22010-06-08 16:52:24 +0000807 Error error(process->Resume());
808 if (error.Success())
809 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000810 result.AppendMessageWithFormat ("Process %llu resuming\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000811 if (synchronous_execution)
812 {
Greg Claytonbef15832010-07-14 00:18:15 +0000813 state = process->WaitForProcessToStop (NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000814
815 result.SetDidChangeProcessState (true);
Greg Clayton444e35b2011-10-19 18:09:39 +0000816 result.AppendMessageWithFormat ("Process %llu %s\n", process->GetID(), StateAsCString (state));
Chris Lattner24943d22010-06-08 16:52:24 +0000817 result.SetStatus (eReturnStatusSuccessFinishNoResult);
818 }
819 else
820 {
821 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
822 }
823 }
824 else
825 {
826 result.AppendErrorWithFormat("Failed to resume process: %s.\n", error.AsCString());
827 result.SetStatus (eReturnStatusFailed);
828 }
829 }
830 else
831 {
832 result.AppendErrorWithFormat ("Process cannot be continued from its current state (%s).\n",
833 StateAsCString(state));
834 result.SetStatus (eReturnStatusFailed);
835 }
836 return result.Succeeded();
837 }
Jim Ingham124e6902012-08-11 01:27:55 +0000838
839 Options *
840 GetOptions ()
841 {
842 return &m_options;
843 }
844
845 CommandOptions m_options;
846
847};
848
849OptionDefinition
850CommandObjectProcessContinue::CommandOptions::g_option_table[] =
851{
852{ LLDB_OPT_SET_ALL, false, "ignore-count",'i', required_argument, NULL, 0, eArgTypeUnsignedInteger,
853 "Ignore <N> crossings of the breakpoint (if it exists) for the currently selected thread."},
854{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000855};
856
857//-------------------------------------------------------------------------
858// CommandObjectProcessDetach
859//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000860#pragma mark CommandObjectProcessDetach
Chris Lattner24943d22010-06-08 16:52:24 +0000861
Jim Inghamda26bd22012-06-08 21:56:10 +0000862class CommandObjectProcessDetach : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000863{
864public:
865
Greg Clayton238c0a12010-09-18 01:14:36 +0000866 CommandObjectProcessDetach (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000867 CommandObjectParsed (interpreter,
868 "process detach",
869 "Detach from the current process being debugged.",
870 "process detach",
871 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
Jim Inghamda26bd22012-06-08 21:56:10 +0000881 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000882 CommandReturnObject &result)
883 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000884 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000885 if (process == NULL)
886 {
887 result.AppendError ("must have a valid process in order to detach");
888 result.SetStatus (eReturnStatusFailed);
889 return false;
890 }
891
Greg Clayton444e35b2011-10-19 18:09:39 +0000892 result.AppendMessageWithFormat ("Detaching from process %llu\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000893 Error error (process->Detach());
894 if (error.Success())
895 {
896 result.SetStatus (eReturnStatusSuccessFinishResult);
897 }
898 else
899 {
900 result.AppendErrorWithFormat ("Detach failed: %s\n", error.AsCString());
901 result.SetStatus (eReturnStatusFailed);
902 return false;
903 }
904 return result.Succeeded();
905 }
906};
907
908//-------------------------------------------------------------------------
Greg Claytone71e2582011-02-04 01:58:07 +0000909// CommandObjectProcessConnect
910//-------------------------------------------------------------------------
911#pragma mark CommandObjectProcessConnect
912
Jim Inghamda26bd22012-06-08 21:56:10 +0000913class CommandObjectProcessConnect : public CommandObjectParsed
Greg Claytone71e2582011-02-04 01:58:07 +0000914{
915public:
916
917 class CommandOptions : public Options
918 {
919 public:
920
Greg Claytonf15996e2011-04-07 22:46:35 +0000921 CommandOptions (CommandInterpreter &interpreter) :
922 Options(interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000923 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000924 // Keep default values of all options in one place: OptionParsingStarting ()
925 OptionParsingStarting ();
Greg Claytone71e2582011-02-04 01:58:07 +0000926 }
927
928 ~CommandOptions ()
929 {
930 }
931
932 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000933 SetOptionValue (uint32_t option_idx, const char *option_arg)
Greg Claytone71e2582011-02-04 01:58:07 +0000934 {
935 Error error;
936 char short_option = (char) m_getopt_table[option_idx].val;
937
938 switch (short_option)
939 {
940 case 'p':
941 plugin_name.assign (option_arg);
942 break;
943
944 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000945 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Greg Claytone71e2582011-02-04 01:58:07 +0000946 break;
947 }
948 return error;
949 }
950
951 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000952 OptionParsingStarting ()
Greg Claytone71e2582011-02-04 01:58:07 +0000953 {
Greg Claytone71e2582011-02-04 01:58:07 +0000954 plugin_name.clear();
955 }
956
Greg Claytonb3448432011-03-24 21:19:54 +0000957 const OptionDefinition*
Greg Claytone71e2582011-02-04 01:58:07 +0000958 GetDefinitions ()
959 {
960 return g_option_table;
961 }
962
963 // Options table: Required for subclasses of Options.
964
Greg Claytonb3448432011-03-24 21:19:54 +0000965 static OptionDefinition g_option_table[];
Greg Claytone71e2582011-02-04 01:58:07 +0000966
967 // Instance variables to hold the values for command options.
968
969 std::string plugin_name;
970 };
971
972 CommandObjectProcessConnect (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000973 CommandObjectParsed (interpreter,
974 "process connect",
975 "Connect to a remote debug service.",
976 "process connect <remote-url>",
977 0),
Greg Claytonf15996e2011-04-07 22:46:35 +0000978 m_options (interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000979 {
980 }
981
982 ~CommandObjectProcessConnect ()
983 {
984 }
985
986
Jim Inghamda26bd22012-06-08 21:56:10 +0000987 Options *
988 GetOptions ()
989 {
990 return &m_options;
991 }
992
993protected:
Greg Claytone71e2582011-02-04 01:58:07 +0000994 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000995 DoExecute (Args& command,
Greg Claytone71e2582011-02-04 01:58:07 +0000996 CommandReturnObject &result)
997 {
998
999 TargetSP target_sp (m_interpreter.GetDebugger().GetSelectedTarget());
1000 Error error;
Greg Clayton567e7f32011-09-22 04:58:26 +00001001 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytone71e2582011-02-04 01:58:07 +00001002 if (process)
1003 {
1004 if (process->IsAlive())
1005 {
Greg Clayton444e35b2011-10-19 18:09:39 +00001006 result.AppendErrorWithFormat ("Process %llu is currently being debugged, kill the process before connecting.\n",
Greg Claytone71e2582011-02-04 01:58:07 +00001007 process->GetID());
1008 result.SetStatus (eReturnStatusFailed);
1009 return false;
1010 }
1011 }
1012
1013 if (!target_sp)
1014 {
1015 // If there isn't a current target create one.
1016 FileSpec emptyFileSpec;
Greg Claytone71e2582011-02-04 01:58:07 +00001017
1018 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
1019 emptyFileSpec,
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001020 NULL,
Greg Claytone71e2582011-02-04 01:58:07 +00001021 false,
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001022 NULL, // No platform options
Greg Claytone71e2582011-02-04 01:58:07 +00001023 target_sp);
1024 if (!target_sp || error.Fail())
1025 {
1026 result.AppendError(error.AsCString("Error creating target"));
1027 result.SetStatus (eReturnStatusFailed);
1028 return false;
1029 }
1030 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target_sp.get());
1031 }
1032
1033 if (command.GetArgumentCount() == 1)
1034 {
1035 const char *plugin_name = NULL;
1036 if (!m_options.plugin_name.empty())
1037 plugin_name = m_options.plugin_name.c_str();
1038
1039 const char *remote_url = command.GetArgumentAtIndex(0);
Greg Clayton46c9a352012-02-09 06:16:32 +00001040 process = target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name, NULL).get();
Greg Claytone71e2582011-02-04 01:58:07 +00001041
1042 if (process)
1043 {
Jason Molendafac2e622012-09-29 04:02:01 +00001044 error = process->ConnectRemote (&process->GetTarget().GetDebugger().GetOutputStream(), remote_url);
Greg Claytone71e2582011-02-04 01:58:07 +00001045
1046 if (error.Fail())
1047 {
1048 result.AppendError(error.AsCString("Remote connect failed"));
1049 result.SetStatus (eReturnStatusFailed);
Greg Clayton0cbb93b2012-03-31 00:10:30 +00001050 target_sp->DeleteCurrentProcess();
Greg Claytone71e2582011-02-04 01:58:07 +00001051 return false;
1052 }
1053 }
1054 else
1055 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001056 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",
1057 m_cmd_name.c_str());
Greg Claytone71e2582011-02-04 01:58:07 +00001058 result.SetStatus (eReturnStatusFailed);
1059 }
1060 }
1061 else
1062 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001063 result.AppendErrorWithFormat ("'%s' takes exactly one argument:\nUsage: %s\n",
Greg Claytone71e2582011-02-04 01:58:07 +00001064 m_cmd_name.c_str(),
1065 m_cmd_syntax.c_str());
1066 result.SetStatus (eReturnStatusFailed);
1067 }
1068 return result.Succeeded();
1069 }
Greg Claytone71e2582011-02-04 01:58:07 +00001070
1071 CommandOptions m_options;
1072};
1073
Greg Claytonb3448432011-03-24 21:19:54 +00001074OptionDefinition
Greg Claytone71e2582011-02-04 01:58:07 +00001075CommandObjectProcessConnect::CommandOptions::g_option_table[] =
1076{
1077 { LLDB_OPT_SET_ALL, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
1078 { 0, false, NULL, 0 , 0, NULL, 0, eArgTypeNone, NULL }
1079};
1080
1081//-------------------------------------------------------------------------
Greg Clayton13193d52012-10-13 02:07:45 +00001082// CommandObjectProcessPlugin
1083//-------------------------------------------------------------------------
1084#pragma mark CommandObjectProcessPlugin
1085
1086class CommandObjectProcessPlugin : public CommandObjectProxy
1087{
1088public:
1089
1090 CommandObjectProcessPlugin (CommandInterpreter &interpreter) :
1091 CommandObjectProxy (interpreter,
1092 "process plugin",
1093 "Send a custom command to the current process plug-in.",
1094 "process plugin <args>",
1095 0)
1096 {
1097 }
1098
1099 ~CommandObjectProcessPlugin ()
1100 {
1101 }
1102
1103 virtual CommandObject *
1104 GetProxyCommandObject()
1105 {
1106 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
1107 if (process)
1108 return process->GetPluginCommandObject();
1109 return NULL;
1110 }
1111};
1112
1113
1114//-------------------------------------------------------------------------
Greg Clayton0baa3942010-11-04 01:54:29 +00001115// CommandObjectProcessLoad
1116//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001117#pragma mark CommandObjectProcessLoad
Greg Clayton0baa3942010-11-04 01:54:29 +00001118
Jim Inghamda26bd22012-06-08 21:56:10 +00001119class CommandObjectProcessLoad : public CommandObjectParsed
Greg Clayton0baa3942010-11-04 01:54:29 +00001120{
1121public:
1122
1123 CommandObjectProcessLoad (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001124 CommandObjectParsed (interpreter,
1125 "process load",
1126 "Load a shared library into the current process.",
1127 "process load <filename> [<filename> ...]",
1128 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
Greg Clayton0baa3942010-11-04 01:54:29 +00001129 {
1130 }
1131
1132 ~CommandObjectProcessLoad ()
1133 {
1134 }
1135
Jim Inghamda26bd22012-06-08 21:56:10 +00001136protected:
Greg Clayton0baa3942010-11-04 01:54:29 +00001137 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001138 DoExecute (Args& command,
Greg Clayton0baa3942010-11-04 01:54:29 +00001139 CommandReturnObject &result)
1140 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001141 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton0baa3942010-11-04 01:54:29 +00001142 if (process == NULL)
1143 {
1144 result.AppendError ("must have a valid process in order to load a shared library");
1145 result.SetStatus (eReturnStatusFailed);
1146 return false;
1147 }
1148
1149 const uint32_t argc = command.GetArgumentCount();
1150
1151 for (uint32_t i=0; i<argc; ++i)
1152 {
1153 Error error;
1154 const char *image_path = command.GetArgumentAtIndex(i);
1155 FileSpec image_spec (image_path, false);
Greg Claytonf2bf8702011-08-11 16:25:18 +00001156 process->GetTarget().GetPlatform()->ResolveRemotePath(image_spec, image_spec);
Greg Clayton0baa3942010-11-04 01:54:29 +00001157 uint32_t image_token = process->LoadImage(image_spec, error);
1158 if (image_token != LLDB_INVALID_IMAGE_TOKEN)
1159 {
1160 result.AppendMessageWithFormat ("Loading \"%s\"...ok\nImage %u loaded.\n", image_path, image_token);
1161 result.SetStatus (eReturnStatusSuccessFinishResult);
1162 }
1163 else
1164 {
1165 result.AppendErrorWithFormat ("failed to load '%s': %s", image_path, error.AsCString());
1166 result.SetStatus (eReturnStatusFailed);
1167 }
1168 }
1169 return result.Succeeded();
1170 }
1171};
1172
1173
1174//-------------------------------------------------------------------------
1175// CommandObjectProcessUnload
1176//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001177#pragma mark CommandObjectProcessUnload
Greg Clayton0baa3942010-11-04 01:54:29 +00001178
Jim Inghamda26bd22012-06-08 21:56:10 +00001179class CommandObjectProcessUnload : public CommandObjectParsed
Greg Clayton0baa3942010-11-04 01:54:29 +00001180{
1181public:
1182
1183 CommandObjectProcessUnload (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001184 CommandObjectParsed (interpreter,
1185 "process unload",
1186 "Unload a shared library from the current process using the index returned by a previous call to \"process load\".",
1187 "process unload <index>",
1188 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
Greg Clayton0baa3942010-11-04 01:54:29 +00001189 {
1190 }
1191
1192 ~CommandObjectProcessUnload ()
1193 {
1194 }
1195
Jim Inghamda26bd22012-06-08 21:56:10 +00001196protected:
Greg Clayton0baa3942010-11-04 01:54:29 +00001197 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001198 DoExecute (Args& command,
Greg Clayton0baa3942010-11-04 01:54:29 +00001199 CommandReturnObject &result)
1200 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001201 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton0baa3942010-11-04 01:54:29 +00001202 if (process == NULL)
1203 {
1204 result.AppendError ("must have a valid process in order to load a shared library");
1205 result.SetStatus (eReturnStatusFailed);
1206 return false;
1207 }
1208
1209 const uint32_t argc = command.GetArgumentCount();
1210
1211 for (uint32_t i=0; i<argc; ++i)
1212 {
1213 const char *image_token_cstr = command.GetArgumentAtIndex(i);
1214 uint32_t image_token = Args::StringToUInt32(image_token_cstr, LLDB_INVALID_IMAGE_TOKEN, 0);
1215 if (image_token == LLDB_INVALID_IMAGE_TOKEN)
1216 {
1217 result.AppendErrorWithFormat ("invalid image index argument '%s'", image_token_cstr);
1218 result.SetStatus (eReturnStatusFailed);
1219 break;
1220 }
1221 else
1222 {
1223 Error error (process->UnloadImage(image_token));
1224 if (error.Success())
1225 {
1226 result.AppendMessageWithFormat ("Unloading shared library with index %u...ok\n", image_token);
1227 result.SetStatus (eReturnStatusSuccessFinishResult);
1228 }
1229 else
1230 {
1231 result.AppendErrorWithFormat ("failed to unload image: %s", error.AsCString());
1232 result.SetStatus (eReturnStatusFailed);
1233 break;
1234 }
1235 }
1236 }
1237 return result.Succeeded();
1238 }
1239};
1240
1241//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001242// CommandObjectProcessSignal
1243//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001244#pragma mark CommandObjectProcessSignal
Chris Lattner24943d22010-06-08 16:52:24 +00001245
Jim Inghamda26bd22012-06-08 21:56:10 +00001246class CommandObjectProcessSignal : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001247{
1248public:
1249
Greg Clayton238c0a12010-09-18 01:14:36 +00001250 CommandObjectProcessSignal (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001251 CommandObjectParsed (interpreter,
1252 "process signal",
1253 "Send a UNIX signal to the current process being debugged.",
1254 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001255 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001256 CommandArgumentEntry arg;
1257 CommandArgumentData signal_arg;
1258
1259 // Define the first (and only) variant of this arg.
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001260 signal_arg.arg_type = eArgTypeUnixSignal;
Caroline Tice43b014a2010-10-04 22:28:36 +00001261 signal_arg.arg_repetition = eArgRepeatPlain;
1262
1263 // There is only one variant this argument could be; put it into the argument entry.
1264 arg.push_back (signal_arg);
1265
1266 // Push the data for the first argument into the m_arguments vector.
1267 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001268 }
1269
1270 ~CommandObjectProcessSignal ()
1271 {
1272 }
1273
Jim Inghamda26bd22012-06-08 21:56:10 +00001274protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001275 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001276 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001277 CommandReturnObject &result)
1278 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001279 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001280 if (process == NULL)
1281 {
1282 result.AppendError ("no process to signal");
1283 result.SetStatus (eReturnStatusFailed);
1284 return false;
1285 }
1286
1287 if (command.GetArgumentCount() == 1)
1288 {
Greg Clayton8f6be2a2010-10-09 01:40:57 +00001289 int signo = LLDB_INVALID_SIGNAL_NUMBER;
1290
1291 const char *signal_name = command.GetArgumentAtIndex(0);
1292 if (::isxdigit (signal_name[0]))
1293 signo = Args::StringToSInt32(signal_name, LLDB_INVALID_SIGNAL_NUMBER, 0);
1294 else
1295 signo = process->GetUnixSignals().GetSignalNumberFromName (signal_name);
1296
1297 if (signo == LLDB_INVALID_SIGNAL_NUMBER)
Chris Lattner24943d22010-06-08 16:52:24 +00001298 {
1299 result.AppendErrorWithFormat ("Invalid signal argument '%s'.\n", command.GetArgumentAtIndex(0));
1300 result.SetStatus (eReturnStatusFailed);
1301 }
1302 else
1303 {
1304 Error error (process->Signal (signo));
1305 if (error.Success())
1306 {
1307 result.SetStatus (eReturnStatusSuccessFinishResult);
1308 }
1309 else
1310 {
1311 result.AppendErrorWithFormat ("Failed to send signal %i: %s\n", signo, error.AsCString());
1312 result.SetStatus (eReturnStatusFailed);
1313 }
1314 }
1315 }
1316 else
1317 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001318 result.AppendErrorWithFormat("'%s' takes exactly one signal number argument:\nUsage: %s\n", m_cmd_name.c_str(),
Chris Lattner24943d22010-06-08 16:52:24 +00001319 m_cmd_syntax.c_str());
1320 result.SetStatus (eReturnStatusFailed);
1321 }
1322 return result.Succeeded();
1323 }
1324};
1325
1326
1327//-------------------------------------------------------------------------
1328// CommandObjectProcessInterrupt
1329//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001330#pragma mark CommandObjectProcessInterrupt
Chris Lattner24943d22010-06-08 16:52:24 +00001331
Jim Inghamda26bd22012-06-08 21:56:10 +00001332class CommandObjectProcessInterrupt : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001333{
1334public:
1335
1336
Greg Clayton238c0a12010-09-18 01:14:36 +00001337 CommandObjectProcessInterrupt (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001338 CommandObjectParsed (interpreter,
1339 "process interrupt",
1340 "Interrupt the current process being debugged.",
1341 "process interrupt",
1342 eFlagProcessMustBeLaunched)
Chris Lattner24943d22010-06-08 16:52:24 +00001343 {
1344 }
1345
1346 ~CommandObjectProcessInterrupt ()
1347 {
1348 }
1349
Jim Inghamda26bd22012-06-08 21:56:10 +00001350protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001351 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001352 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001353 CommandReturnObject &result)
1354 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001355 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001356 if (process == NULL)
1357 {
1358 result.AppendError ("no process to halt");
1359 result.SetStatus (eReturnStatusFailed);
1360 return false;
1361 }
1362
1363 if (command.GetArgumentCount() == 0)
1364 {
1365 Error error(process->Halt ());
1366 if (error.Success())
1367 {
1368 result.SetStatus (eReturnStatusSuccessFinishResult);
1369
1370 // Maybe we should add a "SuspendThreadPlans so we
1371 // can halt, and keep in place all the current thread plans.
1372 process->GetThreadList().DiscardThreadPlans();
1373 }
1374 else
1375 {
1376 result.AppendErrorWithFormat ("Failed to halt process: %s\n", error.AsCString());
1377 result.SetStatus (eReturnStatusFailed);
1378 }
1379 }
1380 else
1381 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001382 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner24943d22010-06-08 16:52:24 +00001383 m_cmd_name.c_str(),
1384 m_cmd_syntax.c_str());
1385 result.SetStatus (eReturnStatusFailed);
1386 }
1387 return result.Succeeded();
1388 }
1389};
1390
1391//-------------------------------------------------------------------------
1392// CommandObjectProcessKill
1393//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001394#pragma mark CommandObjectProcessKill
Chris Lattner24943d22010-06-08 16:52:24 +00001395
Jim Inghamda26bd22012-06-08 21:56:10 +00001396class CommandObjectProcessKill : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001397{
1398public:
1399
Greg Clayton238c0a12010-09-18 01:14:36 +00001400 CommandObjectProcessKill (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001401 CommandObjectParsed (interpreter,
1402 "process kill",
1403 "Terminate the current process being debugged.",
1404 "process kill",
1405 eFlagProcessMustBeLaunched)
Chris Lattner24943d22010-06-08 16:52:24 +00001406 {
1407 }
1408
1409 ~CommandObjectProcessKill ()
1410 {
1411 }
1412
Jim Inghamda26bd22012-06-08 21:56:10 +00001413protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001414 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001415 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001416 CommandReturnObject &result)
1417 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001418 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001419 if (process == NULL)
1420 {
1421 result.AppendError ("no process to kill");
1422 result.SetStatus (eReturnStatusFailed);
1423 return false;
1424 }
1425
1426 if (command.GetArgumentCount() == 0)
1427 {
1428 Error error (process->Destroy());
1429 if (error.Success())
1430 {
1431 result.SetStatus (eReturnStatusSuccessFinishResult);
1432 }
1433 else
1434 {
1435 result.AppendErrorWithFormat ("Failed to kill process: %s\n", error.AsCString());
1436 result.SetStatus (eReturnStatusFailed);
1437 }
1438 }
1439 else
1440 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001441 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner24943d22010-06-08 16:52:24 +00001442 m_cmd_name.c_str(),
1443 m_cmd_syntax.c_str());
1444 result.SetStatus (eReturnStatusFailed);
1445 }
1446 return result.Succeeded();
1447 }
1448};
1449
1450//-------------------------------------------------------------------------
Jim Ingham41313fc2010-06-18 01:23:09 +00001451// CommandObjectProcessStatus
1452//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001453#pragma mark CommandObjectProcessStatus
1454
Jim Inghamda26bd22012-06-08 21:56:10 +00001455class CommandObjectProcessStatus : public CommandObjectParsed
Jim Ingham41313fc2010-06-18 01:23:09 +00001456{
1457public:
Greg Clayton238c0a12010-09-18 01:14:36 +00001458 CommandObjectProcessStatus (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001459 CommandObjectParsed (interpreter,
1460 "process status",
1461 "Show the current status and location of executing process.",
1462 "process status",
1463 0)
Jim Ingham41313fc2010-06-18 01:23:09 +00001464 {
1465 }
1466
1467 ~CommandObjectProcessStatus()
1468 {
1469 }
1470
1471
1472 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001473 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham41313fc2010-06-18 01:23:09 +00001474 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001475 Stream &strm = result.GetOutputStream();
Jim Ingham41313fc2010-06-18 01:23:09 +00001476 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Claytonb72d0f02011-04-12 05:54:46 +00001477 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
Greg Clayton567e7f32011-09-22 04:58:26 +00001478 Process *process = exe_ctx.GetProcessPtr();
1479 if (process)
Jim Ingham41313fc2010-06-18 01:23:09 +00001480 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001481 const bool only_threads_with_stop_reason = true;
1482 const uint32_t start_frame = 0;
1483 const uint32_t num_frames = 1;
1484 const uint32_t num_frames_with_source = 1;
Greg Clayton567e7f32011-09-22 04:58:26 +00001485 process->GetStatus(strm);
1486 process->GetThreadStatus (strm,
1487 only_threads_with_stop_reason,
1488 start_frame,
1489 num_frames,
1490 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001491
Jim Ingham41313fc2010-06-18 01:23:09 +00001492 }
1493 else
1494 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001495 result.AppendError ("No process.");
Jim Ingham41313fc2010-06-18 01:23:09 +00001496 result.SetStatus (eReturnStatusFailed);
1497 }
1498 return result.Succeeded();
1499 }
1500};
1501
1502//-------------------------------------------------------------------------
Caroline Tice23d6f272010-10-13 20:44:39 +00001503// CommandObjectProcessHandle
1504//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001505#pragma mark CommandObjectProcessHandle
Caroline Tice23d6f272010-10-13 20:44:39 +00001506
Jim Inghamda26bd22012-06-08 21:56:10 +00001507class CommandObjectProcessHandle : public CommandObjectParsed
Caroline Tice23d6f272010-10-13 20:44:39 +00001508{
1509public:
1510
1511 class CommandOptions : public Options
1512 {
1513 public:
1514
Greg Claytonf15996e2011-04-07 22:46:35 +00001515 CommandOptions (CommandInterpreter &interpreter) :
1516 Options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001517 {
Greg Clayton143fcc32011-04-13 00:18:08 +00001518 OptionParsingStarting ();
Caroline Tice23d6f272010-10-13 20:44:39 +00001519 }
1520
1521 ~CommandOptions ()
1522 {
1523 }
1524
1525 Error
Greg Clayton143fcc32011-04-13 00:18:08 +00001526 SetOptionValue (uint32_t option_idx, const char *option_arg)
Caroline Tice23d6f272010-10-13 20:44:39 +00001527 {
1528 Error error;
1529 char short_option = (char) m_getopt_table[option_idx].val;
1530
1531 switch (short_option)
1532 {
1533 case 's':
1534 stop = option_arg;
1535 break;
1536 case 'n':
1537 notify = option_arg;
1538 break;
1539 case 'p':
1540 pass = option_arg;
1541 break;
1542 default:
Greg Clayton9c236732011-10-26 00:56:27 +00001543 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Caroline Tice23d6f272010-10-13 20:44:39 +00001544 break;
1545 }
1546 return error;
1547 }
1548
1549 void
Greg Clayton143fcc32011-04-13 00:18:08 +00001550 OptionParsingStarting ()
Caroline Tice23d6f272010-10-13 20:44:39 +00001551 {
Caroline Tice23d6f272010-10-13 20:44:39 +00001552 stop.clear();
1553 notify.clear();
1554 pass.clear();
1555 }
1556
Greg Claytonb3448432011-03-24 21:19:54 +00001557 const OptionDefinition*
Caroline Tice23d6f272010-10-13 20:44:39 +00001558 GetDefinitions ()
1559 {
1560 return g_option_table;
1561 }
1562
1563 // Options table: Required for subclasses of Options.
1564
Greg Claytonb3448432011-03-24 21:19:54 +00001565 static OptionDefinition g_option_table[];
Caroline Tice23d6f272010-10-13 20:44:39 +00001566
1567 // Instance variables to hold the values for command options.
1568
1569 std::string stop;
1570 std::string notify;
1571 std::string pass;
1572 };
1573
1574
1575 CommandObjectProcessHandle (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001576 CommandObjectParsed (interpreter,
1577 "process handle",
1578 "Show or update what the process and debugger should do with various signals received from the OS.",
1579 NULL),
Greg Claytonf15996e2011-04-07 22:46:35 +00001580 m_options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001581 {
Caroline Ticee7471982010-10-14 21:31:13 +00001582 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 +00001583 CommandArgumentEntry arg;
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001584 CommandArgumentData signal_arg;
Caroline Tice23d6f272010-10-13 20:44:39 +00001585
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001586 signal_arg.arg_type = eArgTypeUnixSignal;
1587 signal_arg.arg_repetition = eArgRepeatStar;
Caroline Tice23d6f272010-10-13 20:44:39 +00001588
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001589 arg.push_back (signal_arg);
Caroline Tice23d6f272010-10-13 20:44:39 +00001590
1591 m_arguments.push_back (arg);
1592 }
1593
1594 ~CommandObjectProcessHandle ()
1595 {
1596 }
1597
1598 Options *
1599 GetOptions ()
1600 {
1601 return &m_options;
1602 }
1603
1604 bool
Caroline Ticee7471982010-10-14 21:31:13 +00001605 VerifyCommandOptionValue (const std::string &option, int &real_value)
Caroline Tice23d6f272010-10-13 20:44:39 +00001606 {
1607 bool okay = true;
1608
Caroline Ticee7471982010-10-14 21:31:13 +00001609 bool success = false;
1610 bool tmp_value = Args::StringToBoolean (option.c_str(), false, &success);
1611
1612 if (success && tmp_value)
1613 real_value = 1;
1614 else if (success && !tmp_value)
1615 real_value = 0;
Caroline Tice23d6f272010-10-13 20:44:39 +00001616 else
1617 {
1618 // If the value isn't 'true' or 'false', it had better be 0 or 1.
Caroline Ticee7471982010-10-14 21:31:13 +00001619 real_value = Args::StringToUInt32 (option.c_str(), 3);
1620 if (real_value != 0 && real_value != 1)
Caroline Tice23d6f272010-10-13 20:44:39 +00001621 okay = false;
1622 }
1623
1624 return okay;
1625 }
1626
Caroline Ticee7471982010-10-14 21:31:13 +00001627 void
1628 PrintSignalHeader (Stream &str)
1629 {
1630 str.Printf ("NAME PASS STOP NOTIFY\n");
1631 str.Printf ("========== ===== ===== ======\n");
1632 }
1633
1634 void
1635 PrintSignal (Stream &str, int32_t signo, const char *sig_name, UnixSignals &signals)
1636 {
1637 bool stop;
1638 bool suppress;
1639 bool notify;
1640
1641 str.Printf ("%-10s ", sig_name);
1642 if (signals.GetSignalInfo (signo, suppress, stop, notify))
1643 {
1644 bool pass = !suppress;
1645 str.Printf ("%s %s %s",
1646 (pass ? "true " : "false"),
1647 (stop ? "true " : "false"),
1648 (notify ? "true " : "false"));
1649 }
1650 str.Printf ("\n");
1651 }
1652
1653 void
1654 PrintSignalInformation (Stream &str, Args &signal_args, int num_valid_signals, UnixSignals &signals)
1655 {
1656 PrintSignalHeader (str);
1657
1658 if (num_valid_signals > 0)
1659 {
1660 size_t num_args = signal_args.GetArgumentCount();
1661 for (size_t i = 0; i < num_args; ++i)
1662 {
1663 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1664 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
1665 PrintSignal (str, signo, signal_args.GetArgumentAtIndex (i), signals);
1666 }
1667 }
1668 else // Print info for ALL signals
1669 {
1670 int32_t signo = signals.GetFirstSignalNumber();
1671 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1672 {
1673 PrintSignal (str, signo, signals.GetSignalAsCString (signo), signals);
1674 signo = signals.GetNextSignalNumber (signo);
1675 }
1676 }
1677 }
1678
Jim Inghamda26bd22012-06-08 21:56:10 +00001679protected:
Caroline Tice23d6f272010-10-13 20:44:39 +00001680 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001681 DoExecute (Args &signal_args, CommandReturnObject &result)
Caroline Tice23d6f272010-10-13 20:44:39 +00001682 {
1683 TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
1684
1685 if (!target_sp)
1686 {
1687 result.AppendError ("No current target;"
1688 " cannot handle signals until you have a valid target and process.\n");
1689 result.SetStatus (eReturnStatusFailed);
1690 return false;
1691 }
1692
1693 ProcessSP process_sp = target_sp->GetProcessSP();
1694
1695 if (!process_sp)
1696 {
1697 result.AppendError ("No current process; cannot handle signals until you have a valid process.\n");
1698 result.SetStatus (eReturnStatusFailed);
1699 return false;
1700 }
1701
Caroline Tice23d6f272010-10-13 20:44:39 +00001702 int stop_action = -1; // -1 means leave the current setting alone
Caroline Ticee7471982010-10-14 21:31:13 +00001703 int pass_action = -1; // -1 means leave the current setting alone
Caroline Tice23d6f272010-10-13 20:44:39 +00001704 int notify_action = -1; // -1 means leave the current setting alone
1705
1706 if (! m_options.stop.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001707 && ! VerifyCommandOptionValue (m_options.stop, stop_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001708 {
1709 result.AppendError ("Invalid argument for command option --stop; must be true or false.\n");
1710 result.SetStatus (eReturnStatusFailed);
1711 return false;
1712 }
1713
1714 if (! m_options.notify.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001715 && ! VerifyCommandOptionValue (m_options.notify, notify_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001716 {
1717 result.AppendError ("Invalid argument for command option --notify; must be true or false.\n");
1718 result.SetStatus (eReturnStatusFailed);
1719 return false;
1720 }
1721
1722 if (! m_options.pass.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001723 && ! VerifyCommandOptionValue (m_options.pass, pass_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001724 {
1725 result.AppendError ("Invalid argument for command option --pass; must be true or false.\n");
1726 result.SetStatus (eReturnStatusFailed);
1727 return false;
1728 }
1729
1730 size_t num_args = signal_args.GetArgumentCount();
1731 UnixSignals &signals = process_sp->GetUnixSignals();
1732 int num_signals_set = 0;
1733
Caroline Ticee7471982010-10-14 21:31:13 +00001734 if (num_args > 0)
Caroline Tice23d6f272010-10-13 20:44:39 +00001735 {
Caroline Ticee7471982010-10-14 21:31:13 +00001736 for (size_t i = 0; i < num_args; ++i)
Caroline Tice23d6f272010-10-13 20:44:39 +00001737 {
Caroline Ticee7471982010-10-14 21:31:13 +00001738 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1739 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
Caroline Tice23d6f272010-10-13 20:44:39 +00001740 {
Caroline Ticee7471982010-10-14 21:31:13 +00001741 // Casting the actions as bools here should be okay, because VerifyCommandOptionValue guarantees
1742 // the value is either 0 or 1.
1743 if (stop_action != -1)
1744 signals.SetShouldStop (signo, (bool) stop_action);
1745 if (pass_action != -1)
1746 {
1747 bool suppress = ! ((bool) pass_action);
1748 signals.SetShouldSuppress (signo, suppress);
1749 }
1750 if (notify_action != -1)
1751 signals.SetShouldNotify (signo, (bool) notify_action);
1752 ++num_signals_set;
Caroline Tice23d6f272010-10-13 20:44:39 +00001753 }
Caroline Ticee7471982010-10-14 21:31:13 +00001754 else
1755 {
1756 result.AppendErrorWithFormat ("Invalid signal name '%s'\n", signal_args.GetArgumentAtIndex (i));
1757 }
Caroline Tice23d6f272010-10-13 20:44:39 +00001758 }
1759 }
Caroline Ticee7471982010-10-14 21:31:13 +00001760 else
1761 {
1762 // No signal specified, if any command options were specified, update ALL signals.
1763 if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1))
1764 {
1765 if (m_interpreter.Confirm ("Do you really want to update all the signals?", false))
1766 {
1767 int32_t signo = signals.GetFirstSignalNumber();
1768 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1769 {
1770 if (notify_action != -1)
1771 signals.SetShouldNotify (signo, (bool) notify_action);
1772 if (stop_action != -1)
1773 signals.SetShouldStop (signo, (bool) stop_action);
1774 if (pass_action != -1)
1775 {
1776 bool suppress = ! ((bool) pass_action);
1777 signals.SetShouldSuppress (signo, suppress);
1778 }
1779 signo = signals.GetNextSignalNumber (signo);
1780 }
1781 }
1782 }
1783 }
1784
1785 PrintSignalInformation (result.GetOutputStream(), signal_args, num_signals_set, signals);
Caroline Tice23d6f272010-10-13 20:44:39 +00001786
1787 if (num_signals_set > 0)
1788 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1789 else
1790 result.SetStatus (eReturnStatusFailed);
1791
1792 return result.Succeeded();
1793 }
1794
Caroline Tice23d6f272010-10-13 20:44:39 +00001795 CommandOptions m_options;
1796};
1797
Greg Claytonb3448432011-03-24 21:19:54 +00001798OptionDefinition
Caroline Tice23d6f272010-10-13 20:44:39 +00001799CommandObjectProcessHandle::CommandOptions::g_option_table[] =
1800{
1801{ 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." },
1802{ 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." },
1803{ LLDB_OPT_SET_1, false, "pass", 'p', required_argument, NULL, 0, eArgTypeBoolean, "Whether or not the signal should be passed to the process." },
1804{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1805};
1806
1807//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001808// CommandObjectMultiwordProcess
1809//-------------------------------------------------------------------------
1810
Greg Clayton63094e02010-06-23 01:19:29 +00001811CommandObjectMultiwordProcess::CommandObjectMultiwordProcess (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00001812 CommandObjectMultiword (interpreter,
1813 "process",
1814 "A set of commands for operating on a process.",
1815 "process <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00001816{
Greg Claytona9eb8272011-07-02 21:07:54 +00001817 LoadSubCommand ("attach", CommandObjectSP (new CommandObjectProcessAttach (interpreter)));
1818 LoadSubCommand ("launch", CommandObjectSP (new CommandObjectProcessLaunch (interpreter)));
1819 LoadSubCommand ("continue", CommandObjectSP (new CommandObjectProcessContinue (interpreter)));
1820 LoadSubCommand ("connect", CommandObjectSP (new CommandObjectProcessConnect (interpreter)));
1821 LoadSubCommand ("detach", CommandObjectSP (new CommandObjectProcessDetach (interpreter)));
1822 LoadSubCommand ("load", CommandObjectSP (new CommandObjectProcessLoad (interpreter)));
1823 LoadSubCommand ("unload", CommandObjectSP (new CommandObjectProcessUnload (interpreter)));
1824 LoadSubCommand ("signal", CommandObjectSP (new CommandObjectProcessSignal (interpreter)));
1825 LoadSubCommand ("handle", CommandObjectSP (new CommandObjectProcessHandle (interpreter)));
1826 LoadSubCommand ("status", CommandObjectSP (new CommandObjectProcessStatus (interpreter)));
Greg Clayton238c0a12010-09-18 01:14:36 +00001827 LoadSubCommand ("interrupt", CommandObjectSP (new CommandObjectProcessInterrupt (interpreter)));
Greg Claytona9eb8272011-07-02 21:07:54 +00001828 LoadSubCommand ("kill", CommandObjectSP (new CommandObjectProcessKill (interpreter)));
Greg Clayton13193d52012-10-13 02:07:45 +00001829 LoadSubCommand ("plugin", CommandObjectSP (new CommandObjectProcessPlugin (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00001830}
1831
1832CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess ()
1833{
1834}
1835