blob: 54f2a54beba3f02ea485a7db1fd5b000f8cff76c [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- CommandObjectProcess.cpp --------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "CommandObjectProcess.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
Jim Ingham84cdc152010-06-15 19:49:27 +000016#include "lldb/Interpreter/Args.h"
17#include "lldb/Interpreter/Options.h"
Chris Lattner24943d22010-06-08 16:52:24 +000018#include "lldb/Core/State.h"
Greg Claytonabe0fed2011-04-18 08:33:37 +000019#include "lldb/Host/Host.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Interpreter/CommandInterpreter.h"
21#include "lldb/Interpreter/CommandReturnObject.h"
Greg Claytone4b9c1f2011-03-08 22:40:15 +000022#include "lldb/Target/Platform.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Target/Process.h"
24#include "lldb/Target/Target.h"
25#include "lldb/Target/Thread.h"
26
27using namespace lldb;
28using namespace lldb_private;
29
30//-------------------------------------------------------------------------
31// CommandObjectProcessLaunch
32//-------------------------------------------------------------------------
Jim Ingham5a15e692012-02-16 06:50:00 +000033#pragma mark CommandObjectProcessLaunch
Jim Inghamda26bd22012-06-08 21:56:10 +000034class CommandObjectProcessLaunch : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +000035{
36public:
37
Greg Clayton238c0a12010-09-18 01:14:36 +000038 CommandObjectProcessLaunch (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +000039 CommandObjectParsed (interpreter,
40 "process launch",
41 "Launch the executable in the debugger.",
42 NULL),
Greg Claytonf15996e2011-04-07 22:46:35 +000043 m_options (interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +000044 {
Caroline Tice43b014a2010-10-04 22:28:36 +000045 CommandArgumentEntry arg;
46 CommandArgumentData run_args_arg;
47
48 // Define the first (and only) variant of this arg.
49 run_args_arg.arg_type = eArgTypeRunArgs;
50 run_args_arg.arg_repetition = eArgRepeatOptional;
51
52 // There is only one variant this argument could be; put it into the argument entry.
53 arg.push_back (run_args_arg);
54
55 // Push the data for the first argument into the m_arguments vector.
56 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +000057 }
58
59
60 ~CommandObjectProcessLaunch ()
61 {
62 }
63
64 Options *
65 GetOptions ()
66 {
67 return &m_options;
68 }
69
Jim Inghamda26bd22012-06-08 21:56:10 +000070 virtual const char *GetRepeatCommand (Args &current_command_args, uint32_t index)
71 {
72 // No repeat for "process launch"...
73 return "";
74 }
75
76protected:
Chris Lattner24943d22010-06-08 16:52:24 +000077 bool
Jim Inghamda26bd22012-06-08 21:56:10 +000078 DoExecute (Args& launch_args, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +000079 {
Greg Claytonabb33022011-11-08 02:43:13 +000080 Debugger &debugger = m_interpreter.GetDebugger();
81 Target *target = debugger.GetSelectedTarget().get();
82 Error error;
Chris Lattner24943d22010-06-08 16:52:24 +000083
84 if (target == NULL)
85 {
Greg Claytone1f50b92011-05-03 22:09:39 +000086 result.AppendError ("invalid target, create a debug target using the 'target create' command");
Chris Lattner24943d22010-06-08 16:52:24 +000087 result.SetStatus (eReturnStatusFailed);
88 return false;
89 }
Chris Lattner24943d22010-06-08 16:52:24 +000090 // If our listener is NULL, users aren't allows to launch
Chris Lattner24943d22010-06-08 16:52:24 +000091 char filename[PATH_MAX];
Greg Clayton5beb99d2011-08-11 02:48:45 +000092 const Module *exe_module = target->GetExecutableModulePointer();
Greg Claytona2f74232011-02-24 22:24:29 +000093
94 if (exe_module == NULL)
95 {
Greg Claytone1f50b92011-05-03 22:09:39 +000096 result.AppendError ("no file in target, create a debug target using the 'target create' command");
Greg Claytona2f74232011-02-24 22:24:29 +000097 result.SetStatus (eReturnStatusFailed);
98 return false;
99 }
100
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000101 exe_module->GetFileSpec().GetPath (filename, sizeof(filename));
Chris Lattner24943d22010-06-08 16:52:24 +0000102
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000103 const bool add_exe_file_as_first_arg = true;
Greg Clayton1d1f39e2011-11-29 04:03:30 +0000104 m_options.launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), add_exe_file_as_first_arg);
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000105
Greg Claytona2f74232011-02-24 22:24:29 +0000106 StateType state = eStateInvalid;
Greg Clayton567e7f32011-09-22 04:58:26 +0000107 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytona2f74232011-02-24 22:24:29 +0000108 if (process)
109 {
110 state = process->GetState();
111
112 if (process->IsAlive() && state != eStateConnected)
113 {
114 char message[1024];
115 if (process->GetState() == eStateAttaching)
116 ::strncpy (message, "There is a pending attach, abort it and launch a new process?", sizeof(message));
117 else
118 ::strncpy (message, "There is a running process, kill it and restart?", sizeof(message));
119
120 if (!m_interpreter.Confirm (message, true))
Jim Ingham22dc9722010-12-09 18:58:16 +0000121 {
Greg Claytona2f74232011-02-24 22:24:29 +0000122 result.SetStatus (eReturnStatusFailed);
123 return false;
Jim Ingham22dc9722010-12-09 18:58:16 +0000124 }
125 else
126 {
Greg Claytonabb33022011-11-08 02:43:13 +0000127 Error destroy_error (process->Destroy());
128 if (destroy_error.Success())
Greg Claytona2f74232011-02-24 22:24:29 +0000129 {
130 result.SetStatus (eReturnStatusSuccessFinishResult);
131 }
132 else
133 {
Greg Claytonabb33022011-11-08 02:43:13 +0000134 result.AppendErrorWithFormat ("Failed to kill process: %s\n", destroy_error.AsCString());
Greg Claytona2f74232011-02-24 22:24:29 +0000135 result.SetStatus (eReturnStatusFailed);
136 }
Jim Ingham22dc9722010-12-09 18:58:16 +0000137 }
138 }
Chris Lattner24943d22010-06-08 16:52:24 +0000139 }
Jim Ingham22dc9722010-12-09 18:58:16 +0000140
Greg Clayton527154d2011-11-15 03:53:30 +0000141 if (launch_args.GetArgumentCount() == 0)
142 {
143 const Args &process_args = target->GetRunArguments();
144 if (process_args.GetArgumentCount() > 0)
145 m_options.launch_info.GetArguments().AppendArguments (process_args);
146 }
147 else
Greg Claytonabb33022011-11-08 02:43:13 +0000148 {
Greg Clayton3e6f2cc2011-11-21 21:51:18 +0000149 // Save the arguments for subsequent runs in the current target.
150 target->SetRunArguments (launch_args);
151
Greg Claytonabb33022011-11-08 02:43:13 +0000152 m_options.launch_info.GetArguments().AppendArguments (launch_args);
153 }
Greg Claytonabb33022011-11-08 02:43:13 +0000154
Greg Clayton527154d2011-11-15 03:53:30 +0000155 if (target->GetDisableASLR())
156 m_options.launch_info.GetFlags().Set (eLaunchFlagDisableASLR);
157
158 if (target->GetDisableSTDIO())
159 m_options.launch_info.GetFlags().Set (eLaunchFlagDisableSTDIO);
160
161 m_options.launch_info.GetFlags().Set (eLaunchFlagDebug);
162
163 Args environment;
164 target->GetEnvironmentAsArgs (environment);
165 if (environment.GetArgumentCount() > 0)
166 m_options.launch_info.GetEnvironmentEntries ().AppendArguments (environment);
167
Greg Clayton464c6162011-11-17 22:14:31 +0000168 // Finalize the file actions, and if none were given, default to opening
169 // up a pseudo terminal
170 const bool default_to_use_pty = true;
171 m_options.launch_info.FinalizeFileActions (target, default_to_use_pty);
Greg Clayton527154d2011-11-15 03:53:30 +0000172
Greg Claytonabb33022011-11-08 02:43:13 +0000173 if (state == eStateConnected)
174 {
175 if (m_options.launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
176 {
177 result.AppendWarning("can't launch in tty when launching through a remote connection");
178 m_options.launch_info.GetFlags().Clear (eLaunchFlagLaunchInTTY);
179 }
180 }
181 else
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000182 {
Greg Clayton527154d2011-11-15 03:53:30 +0000183 if (!m_options.launch_info.GetArchitecture().IsValid())
Greg Clayton2d9adb72011-11-12 02:10:56 +0000184 m_options.launch_info.GetArchitecture() = target->GetArchitecture();
185
Greg Clayton75d8c252011-11-28 01:45:00 +0000186 PlatformSP platform_sp (target->GetPlatform());
187
188 if (platform_sp && platform_sp->CanDebugProcess ())
189 {
190 process = target->GetPlatform()->DebugProcess (m_options.launch_info,
191 debugger,
192 target,
193 debugger.GetListener(),
194 error).get();
195 }
196 else
197 {
198 const char *plugin_name = m_options.launch_info.GetProcessPluginName();
Greg Clayton46c9a352012-02-09 06:16:32 +0000199 process = target->CreateProcess (debugger.GetListener(), plugin_name, NULL).get();
Greg Clayton75d8c252011-11-28 01:45:00 +0000200 if (process)
201 error = process->Launch (m_options.launch_info);
202 }
Greg Claytonabb33022011-11-08 02:43:13 +0000203
Greg Claytona2f74232011-02-24 22:24:29 +0000204 if (process == NULL)
205 {
Greg Clayton527154d2011-11-15 03:53:30 +0000206 result.SetError (error, "failed to launch or debug process");
Greg Claytona2f74232011-02-24 22:24:29 +0000207 return false;
208 }
Chris Lattner24943d22010-06-08 16:52:24 +0000209 }
Greg Claytonabb33022011-11-08 02:43:13 +0000210
Greg Clayton238c0a12010-09-18 01:14:36 +0000211 if (error.Success())
212 {
Greg Clayton940b1032011-02-23 00:35:02 +0000213 const char *archname = exe_module->GetArchitecture().GetArchitectureName();
Greg Claytonc1d37752010-10-18 01:45:30 +0000214
Greg Clayton444e35b2011-10-19 18:09:39 +0000215 result.AppendMessageWithFormat ("Process %llu launched: '%s' (%s)\n", process->GetID(), filename, archname);
Greg Claytond8c62532010-10-07 04:19:01 +0000216 result.SetDidChangeProcessState (true);
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000217 if (m_options.launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
Greg Clayton238c0a12010-09-18 01:14:36 +0000218 {
Greg Claytond8c62532010-10-07 04:19:01 +0000219 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
Greg Clayton238c0a12010-09-18 01:14:36 +0000220 StateType state = process->WaitForProcessToStop (NULL);
221
222 if (state == eStateStopped)
223 {
Greg Claytond8c62532010-10-07 04:19:01 +0000224 error = process->Resume();
225 if (error.Success())
226 {
227 bool synchronous_execution = m_interpreter.GetSynchronous ();
228 if (synchronous_execution)
229 {
230 state = process->WaitForProcessToStop (NULL);
Greg Clayton20206082011-11-17 01:23:07 +0000231 const bool must_be_alive = true;
232 if (!StateIsStoppedState(state, must_be_alive))
Greg Clayton395fc332011-02-15 21:59:32 +0000233 {
Greg Clayton527154d2011-11-15 03:53:30 +0000234 result.AppendErrorWithFormat ("process isn't stopped: %s", StateAsCString(state));
Greg Clayton395fc332011-02-15 21:59:32 +0000235 }
Greg Claytond8c62532010-10-07 04:19:01 +0000236 result.SetDidChangeProcessState (true);
237 result.SetStatus (eReturnStatusSuccessFinishResult);
238 }
239 else
240 {
241 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
242 }
243 }
Greg Clayton395fc332011-02-15 21:59:32 +0000244 else
245 {
Greg Clayton527154d2011-11-15 03:53:30 +0000246 result.AppendErrorWithFormat ("process resume at entry point failed: %s", error.AsCString());
Greg Clayton395fc332011-02-15 21:59:32 +0000247 result.SetStatus (eReturnStatusFailed);
248 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000249 }
Greg Clayton395fc332011-02-15 21:59:32 +0000250 else
251 {
Greg Clayton527154d2011-11-15 03:53:30 +0000252 result.AppendErrorWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
Greg Clayton395fc332011-02-15 21:59:32 +0000253 result.SetStatus (eReturnStatusFailed);
254 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000255 }
256 }
Greg Clayton395fc332011-02-15 21:59:32 +0000257 else
258 {
Greg Claytona9eb8272011-07-02 21:07:54 +0000259 result.AppendErrorWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton395fc332011-02-15 21:59:32 +0000260 result.SetStatus (eReturnStatusFailed);
261 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000262
Chris Lattner24943d22010-06-08 16:52:24 +0000263 return result.Succeeded();
264 }
265
266protected:
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000267 ProcessLaunchCommandOptions m_options;
Chris Lattner24943d22010-06-08 16:52:24 +0000268};
269
270
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000271//#define SET1 LLDB_OPT_SET_1
272//#define SET2 LLDB_OPT_SET_2
273//#define SET3 LLDB_OPT_SET_3
274//
275//OptionDefinition
276//CommandObjectProcessLaunch::CommandOptions::g_option_table[] =
277//{
278//{ SET1 | SET2 | SET3, false, "stop-at-entry", 's', no_argument, NULL, 0, eArgTypeNone, "Stop at the entry point of the program when launching a process."},
279//{ SET1 , false, "stdin", 'i', required_argument, NULL, 0, eArgTypePath, "Redirect stdin for the process to <path>."},
280//{ SET1 , false, "stdout", 'o', required_argument, NULL, 0, eArgTypePath, "Redirect stdout for the process to <path>."},
281//{ SET1 , false, "stderr", 'e', required_argument, NULL, 0, eArgTypePath, "Redirect stderr for the process to <path>."},
282//{ SET1 | SET2 | SET3, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
283//{ SET2 , false, "tty", 't', optional_argument, NULL, 0, eArgTypePath, "Start the process in a terminal. If <path> is specified, look for a terminal whose name contains <path>, else start the process in a new terminal."},
284//{ SET3, false, "no-stdio", 'n', no_argument, NULL, 0, eArgTypeNone, "Do not set up for terminal I/O to go to running process."},
285//{ SET1 | SET2 | SET3, false, "working-dir", 'w', required_argument, NULL, 0, eArgTypePath, "Set the current working directory to <path> when running the inferior."},
286//{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
287//};
288//
289//#undef SET1
290//#undef SET2
291//#undef SET3
Chris Lattner24943d22010-06-08 16:52:24 +0000292
293//-------------------------------------------------------------------------
294// CommandObjectProcessAttach
295//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000296#pragma mark CommandObjectProcessAttach
Jim Inghamda26bd22012-06-08 21:56:10 +0000297class CommandObjectProcessAttach : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000298{
299public:
300
Chris Lattner24943d22010-06-08 16:52:24 +0000301 class CommandOptions : public Options
302 {
303 public:
304
Greg Claytonf15996e2011-04-07 22:46:35 +0000305 CommandOptions (CommandInterpreter &interpreter) :
306 Options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000307 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000308 // Keep default values of all options in one place: OptionParsingStarting ()
309 OptionParsingStarting ();
Chris Lattner24943d22010-06-08 16:52:24 +0000310 }
311
312 ~CommandOptions ()
313 {
314 }
315
316 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000317 SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +0000318 {
319 Error error;
320 char short_option = (char) m_getopt_table[option_idx].val;
321 bool success = false;
322 switch (short_option)
323 {
Johnny Chen7c099972012-05-24 00:43:00 +0000324 case 'c':
325 attach_info.SetContinueOnceAttached(true);
326 break;
327
Chris Lattner24943d22010-06-08 16:52:24 +0000328 case 'p':
Chris Lattner24943d22010-06-08 16:52:24 +0000329 {
Greg Clayton527154d2011-11-15 03:53:30 +0000330 lldb::pid_t pid = Args::StringToUInt32 (option_arg, LLDB_INVALID_PROCESS_ID, 0, &success);
331 if (!success || pid == LLDB_INVALID_PROCESS_ID)
332 {
333 error.SetErrorStringWithFormat("invalid process ID '%s'", option_arg);
334 }
335 else
336 {
337 attach_info.SetProcessID (pid);
338 }
Chris Lattner24943d22010-06-08 16:52:24 +0000339 }
340 break;
341
342 case 'P':
Greg Clayton527154d2011-11-15 03:53:30 +0000343 attach_info.SetProcessPluginName (option_arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000344 break;
345
346 case 'n':
Greg Clayton527154d2011-11-15 03:53:30 +0000347 attach_info.GetExecutableFile().SetFile(option_arg, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000348 break;
349
350 case 'w':
Greg Clayton527154d2011-11-15 03:53:30 +0000351 attach_info.SetWaitForLaunch(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000352 break;
353
354 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000355 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner24943d22010-06-08 16:52:24 +0000356 break;
357 }
358 return error;
359 }
360
361 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000362 OptionParsingStarting ()
Chris Lattner24943d22010-06-08 16:52:24 +0000363 {
Greg Clayton527154d2011-11-15 03:53:30 +0000364 attach_info.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000365 }
366
Greg Claytonb3448432011-03-24 21:19:54 +0000367 const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +0000368 GetDefinitions ()
369 {
370 return g_option_table;
371 }
372
Jim Ingham7508e732010-08-09 23:31:02 +0000373 virtual bool
Greg Claytonf15996e2011-04-07 22:46:35 +0000374 HandleOptionArgumentCompletion (Args &input,
Jim Ingham7508e732010-08-09 23:31:02 +0000375 int cursor_index,
376 int char_pos,
377 OptionElementVector &opt_element_vector,
378 int opt_element_index,
379 int match_start_point,
380 int max_return_elements,
381 bool &word_complete,
382 StringList &matches)
383 {
384 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
385 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
386
387 // We are only completing the name option for now...
388
Greg Claytonb3448432011-03-24 21:19:54 +0000389 const OptionDefinition *opt_defs = GetDefinitions();
Jim Ingham7508e732010-08-09 23:31:02 +0000390 if (opt_defs[opt_defs_index].short_option == 'n')
391 {
392 // Are we in the name?
393
394 // Look to see if there is a -P argument provided, and if so use that plugin, otherwise
395 // use the default plugin.
Jim Ingham7508e732010-08-09 23:31:02 +0000396
397 const char *partial_name = NULL;
398 partial_name = input.GetArgumentAtIndex(opt_arg_pos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000399
Greg Claytonb72d0f02011-04-12 05:54:46 +0000400 PlatformSP platform_sp (m_interpreter.GetPlatform (true));
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000401 if (platform_sp)
Jim Ingham7508e732010-08-09 23:31:02 +0000402 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000403 ProcessInstanceInfoList process_infos;
404 ProcessInstanceInfoMatch match_info;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000405 if (partial_name)
406 {
Greg Clayton527154d2011-11-15 03:53:30 +0000407 match_info.GetProcessInfo().GetExecutableFile().SetFile(partial_name, false);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000408 match_info.SetNameMatchType(eNameMatchStartsWith);
409 }
410 platform_sp->FindProcesses (match_info, process_infos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000411 const uint32_t num_matches = process_infos.GetSize();
412 if (num_matches > 0)
413 {
414 for (uint32_t i=0; i<num_matches; ++i)
415 {
416 matches.AppendString (process_infos.GetProcessNameAtIndex(i),
417 process_infos.GetProcessNameLengthAtIndex(i));
418 }
419 }
Jim Ingham7508e732010-08-09 23:31:02 +0000420 }
421 }
422
423 return false;
424 }
425
Chris Lattner24943d22010-06-08 16:52:24 +0000426 // Options table: Required for subclasses of Options.
427
Greg Claytonb3448432011-03-24 21:19:54 +0000428 static OptionDefinition g_option_table[];
Chris Lattner24943d22010-06-08 16:52:24 +0000429
430 // Instance variables to hold the values for command options.
431
Greg Clayton527154d2011-11-15 03:53:30 +0000432 ProcessAttachInfo attach_info;
Chris Lattner24943d22010-06-08 16:52:24 +0000433 };
434
Greg Clayton238c0a12010-09-18 01:14:36 +0000435 CommandObjectProcessAttach (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000436 CommandObjectParsed (interpreter,
437 "process attach",
438 "Attach to a process.",
439 "process attach <cmd-options>"),
Greg Claytonf15996e2011-04-07 22:46:35 +0000440 m_options (interpreter)
Jim Ingham7508e732010-08-09 23:31:02 +0000441 {
Jim Ingham7508e732010-08-09 23:31:02 +0000442 }
443
444 ~CommandObjectProcessAttach ()
445 {
446 }
447
Jim Inghamda26bd22012-06-08 21:56:10 +0000448 Options *
449 GetOptions ()
450 {
451 return &m_options;
452 }
453
454protected:
Jim Ingham7508e732010-08-09 23:31:02 +0000455 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000456 DoExecute (Args& command,
Jim Ingham7508e732010-08-09 23:31:02 +0000457 CommandReturnObject &result)
458 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000459 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Jim Inghamee940e22011-09-15 01:08:57 +0000460 // N.B. The attach should be synchronous. It doesn't help much to get the prompt back between initiating the attach
461 // and the target actually stopping. So even if the interpreter is set to be asynchronous, we wait for the stop
462 // ourselves here.
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000463
Greg Clayton567e7f32011-09-22 04:58:26 +0000464 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytona2f74232011-02-24 22:24:29 +0000465 StateType state = eStateInvalid;
Jim Ingham7508e732010-08-09 23:31:02 +0000466 if (process)
467 {
Greg Claytona2f74232011-02-24 22:24:29 +0000468 state = process->GetState();
469 if (process->IsAlive() && state != eStateConnected)
Jim Ingham7508e732010-08-09 23:31:02 +0000470 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000471 result.AppendErrorWithFormat ("Process %llu is currently being debugged, kill the process before attaching.\n",
Jim Ingham7508e732010-08-09 23:31:02 +0000472 process->GetID());
473 result.SetStatus (eReturnStatusFailed);
474 return false;
475 }
476 }
477
478 if (target == NULL)
479 {
480 // If there isn't a current target create one.
481 TargetSP new_target_sp;
482 FileSpec emptyFileSpec;
Jim Ingham7508e732010-08-09 23:31:02 +0000483 Error error;
484
Greg Clayton238c0a12010-09-18 01:14:36 +0000485 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
486 emptyFileSpec,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000487 NULL,
Greg Clayton238c0a12010-09-18 01:14:36 +0000488 false,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000489 NULL, // No platform options
Greg Clayton238c0a12010-09-18 01:14:36 +0000490 new_target_sp);
Jim Ingham7508e732010-08-09 23:31:02 +0000491 target = new_target_sp.get();
492 if (target == NULL || error.Fail())
493 {
Greg Claytone71e2582011-02-04 01:58:07 +0000494 result.AppendError(error.AsCString("Error creating target"));
Jim Ingham7508e732010-08-09 23:31:02 +0000495 return false;
496 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000497 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target);
Jim Ingham7508e732010-08-09 23:31:02 +0000498 }
499
500 // Record the old executable module, we want to issue a warning if the process of attaching changed the
501 // current executable (like somebody said "file foo" then attached to a PID whose executable was bar.)
502
503 ModuleSP old_exec_module_sp = target->GetExecutableModule();
504 ArchSpec old_arch_spec = target->GetArchitecture();
505
506 if (command.GetArgumentCount())
507 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000508 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 +0000509 result.SetStatus (eReturnStatusFailed);
510 }
511 else
512 {
Greg Claytona2f74232011-02-24 22:24:29 +0000513 if (state != eStateConnected)
514 {
Greg Clayton527154d2011-11-15 03:53:30 +0000515 const char *plugin_name = m_options.attach_info.GetProcessPluginName();
Greg Clayton46c9a352012-02-09 06:16:32 +0000516 process = target->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name, NULL).get();
Greg Claytona2f74232011-02-24 22:24:29 +0000517 }
Jim Ingham7508e732010-08-09 23:31:02 +0000518
519 if (process)
520 {
521 Error error;
Greg Clayton527154d2011-11-15 03:53:30 +0000522 // If no process info was specified, then use the target executable
523 // name as the process to attach to by default
524 if (!m_options.attach_info.ProcessInfoSpecified ())
Jim Ingham4805a1c2010-09-15 01:34:14 +0000525 {
526 if (old_exec_module_sp)
Greg Clayton1d1f39e2011-11-29 04:03:30 +0000527 m_options.attach_info.GetExecutableFile().GetFilename() = old_exec_module_sp->GetPlatformFileSpec().GetFilename();
Jim Ingham4805a1c2010-09-15 01:34:14 +0000528
Greg Clayton527154d2011-11-15 03:53:30 +0000529 if (!m_options.attach_info.ProcessInfoSpecified ())
530 {
531 error.SetErrorString ("no process specified, create a target with a file, or specify the --pid or --name command option");
532 }
533 }
534
535 if (error.Success())
536 {
537 error = process->Attach (m_options.attach_info);
538
Jim Ingham4805a1c2010-09-15 01:34:14 +0000539 if (error.Success())
540 {
541 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
542 }
Jim Ingham7508e732010-08-09 23:31:02 +0000543 else
544 {
Greg Clayton527154d2011-11-15 03:53:30 +0000545 result.AppendErrorWithFormat ("attach failed: %s\n", error.AsCString());
Jim Ingham4805a1c2010-09-15 01:34:14 +0000546 result.SetStatus (eReturnStatusFailed);
547 return false;
Jim Ingham7508e732010-08-09 23:31:02 +0000548 }
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000549 // If we're synchronous, wait for the stopped event and report that.
550 // Otherwise just return.
551 // FIXME: in the async case it will now be possible to get to the command
552 // interpreter with a state eStateAttaching. Make sure we handle that correctly.
Jim Inghamee940e22011-09-15 01:08:57 +0000553 StateType state = process->WaitForProcessToStop (NULL);
Greg Clayton527154d2011-11-15 03:53:30 +0000554
Jim Inghamee940e22011-09-15 01:08:57 +0000555 result.SetDidChangeProcessState (true);
Johnny Chen9986a3b2012-05-18 00:51:36 +0000556
557 if (state == eStateStopped)
558 {
559 result.AppendMessageWithFormat ("Process %llu %s\n", process->GetID(), StateAsCString (state));
560 result.SetStatus (eReturnStatusSuccessFinishNoResult);
561 }
562 else
563 {
564 result.AppendError ("attach failed: process did not stop (no such process or permission problem?)");
565 result.SetStatus (eReturnStatusFailed);
566 return false;
567 }
Jim Ingham7508e732010-08-09 23:31:02 +0000568 }
Jim Ingham7508e732010-08-09 23:31:02 +0000569 }
570 }
571
572 if (result.Succeeded())
573 {
574 // Okay, we're done. Last step is to warn if the executable module has changed:
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000575 char new_path[PATH_MAX];
Greg Clayton5beb99d2011-08-11 02:48:45 +0000576 ModuleSP new_exec_module_sp (target->GetExecutableModule());
Jim Ingham7508e732010-08-09 23:31:02 +0000577 if (!old_exec_module_sp)
578 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000579 // We might not have a module if we attached to a raw pid...
Greg Clayton5beb99d2011-08-11 02:48:45 +0000580 if (new_exec_module_sp)
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000581 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000582 new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000583 result.AppendMessageWithFormat("Executable module set to \"%s\".\n", new_path);
584 }
Jim Ingham7508e732010-08-09 23:31:02 +0000585 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000586 else if (old_exec_module_sp->GetFileSpec() != new_exec_module_sp->GetFileSpec())
Jim Ingham7508e732010-08-09 23:31:02 +0000587 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000588 char old_path[PATH_MAX];
Jim Ingham7508e732010-08-09 23:31:02 +0000589
Greg Clayton5beb99d2011-08-11 02:48:45 +0000590 old_exec_module_sp->GetFileSpec().GetPath (old_path, PATH_MAX);
591 new_exec_module_sp->GetFileSpec().GetPath (new_path, PATH_MAX);
Jim Ingham7508e732010-08-09 23:31:02 +0000592
593 result.AppendWarningWithFormat("Executable module changed from \"%s\" to \"%s\".\n",
594 old_path, new_path);
595 }
596
597 if (!old_arch_spec.IsValid())
598 {
Greg Clayton940b1032011-02-23 00:35:02 +0000599 result.AppendMessageWithFormat ("Architecture set to: %s.\n", target->GetArchitecture().GetArchitectureName());
Jim Ingham7508e732010-08-09 23:31:02 +0000600 }
601 else if (old_arch_spec != target->GetArchitecture())
602 {
603 result.AppendWarningWithFormat("Architecture changed from %s to %s.\n",
Greg Clayton940b1032011-02-23 00:35:02 +0000604 old_arch_spec.GetArchitectureName(), target->GetArchitecture().GetArchitectureName());
Jim Ingham7508e732010-08-09 23:31:02 +0000605 }
Johnny Chen7c099972012-05-24 00:43:00 +0000606
607 // This supports the use-case scenario of immediately continuing the process once attached.
608 if (m_options.attach_info.GetContinueOnceAttached())
Sean Callanan4336d932012-05-31 01:30:08 +0000609 m_interpreter.HandleCommand("process continue", eLazyBoolNo, result);
Jim Ingham7508e732010-08-09 23:31:02 +0000610 }
611 return result.Succeeded();
612 }
613
Chris Lattner24943d22010-06-08 16:52:24 +0000614 CommandOptions m_options;
615};
616
617
Greg Claytonb3448432011-03-24 21:19:54 +0000618OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000619CommandObjectProcessAttach::CommandOptions::g_option_table[] =
620{
Johnny Chen7c099972012-05-24 00:43:00 +0000621{ LLDB_OPT_SET_ALL, false, "continue",'c', no_argument, NULL, 0, eArgTypeNone, "Immediately continue the process once attached."},
622{ LLDB_OPT_SET_ALL, false, "plugin", 'P', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
623{ LLDB_OPT_SET_1, false, "pid", 'p', required_argument, NULL, 0, eArgTypePid, "The process ID of an existing process to attach to."},
624{ LLDB_OPT_SET_2, false, "name", 'n', required_argument, NULL, 0, eArgTypeProcessName, "The name of the process to attach to."},
Johnny Chenbe23f052012-06-01 23:16:58 +0000625{ 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 +0000626{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000627};
628
629//-------------------------------------------------------------------------
630// CommandObjectProcessContinue
631//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000632#pragma mark CommandObjectProcessContinue
Chris Lattner24943d22010-06-08 16:52:24 +0000633
Jim Inghamda26bd22012-06-08 21:56:10 +0000634class CommandObjectProcessContinue : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000635{
636public:
637
Greg Clayton238c0a12010-09-18 01:14:36 +0000638 CommandObjectProcessContinue (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000639 CommandObjectParsed (interpreter,
640 "process continue",
641 "Continue execution of all threads in the current process.",
642 "process continue",
643 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
Chris Lattner24943d22010-06-08 16:52:24 +0000644 {
645 }
646
647
648 ~CommandObjectProcessContinue ()
649 {
650 }
651
Jim Inghamda26bd22012-06-08 21:56:10 +0000652protected:
Chris Lattner24943d22010-06-08 16:52:24 +0000653 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000654 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000655 CommandReturnObject &result)
656 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000657 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton238c0a12010-09-18 01:14:36 +0000658 bool synchronous_execution = m_interpreter.GetSynchronous ();
Chris Lattner24943d22010-06-08 16:52:24 +0000659
660 if (process == NULL)
661 {
662 result.AppendError ("no process to continue");
663 result.SetStatus (eReturnStatusFailed);
664 return false;
665 }
666
667 StateType state = process->GetState();
668 if (state == eStateStopped)
669 {
670 if (command.GetArgumentCount() != 0)
671 {
672 result.AppendErrorWithFormat ("The '%s' command does not take any arguments.\n", m_cmd_name.c_str());
673 result.SetStatus (eReturnStatusFailed);
674 return false;
675 }
676
677 const uint32_t num_threads = process->GetThreadList().GetSize();
678
679 // Set the actions that the threads should each take when resuming
680 for (uint32_t idx=0; idx<num_threads; ++idx)
681 {
682 process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState (eStateRunning);
683 }
684
685 Error error(process->Resume());
686 if (error.Success())
687 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000688 result.AppendMessageWithFormat ("Process %llu resuming\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000689 if (synchronous_execution)
690 {
Greg Claytonbef15832010-07-14 00:18:15 +0000691 state = process->WaitForProcessToStop (NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000692
693 result.SetDidChangeProcessState (true);
Greg Clayton444e35b2011-10-19 18:09:39 +0000694 result.AppendMessageWithFormat ("Process %llu %s\n", process->GetID(), StateAsCString (state));
Chris Lattner24943d22010-06-08 16:52:24 +0000695 result.SetStatus (eReturnStatusSuccessFinishNoResult);
696 }
697 else
698 {
699 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
700 }
701 }
702 else
703 {
704 result.AppendErrorWithFormat("Failed to resume process: %s.\n", error.AsCString());
705 result.SetStatus (eReturnStatusFailed);
706 }
707 }
708 else
709 {
710 result.AppendErrorWithFormat ("Process cannot be continued from its current state (%s).\n",
711 StateAsCString(state));
712 result.SetStatus (eReturnStatusFailed);
713 }
714 return result.Succeeded();
715 }
716};
717
718//-------------------------------------------------------------------------
719// CommandObjectProcessDetach
720//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000721#pragma mark CommandObjectProcessDetach
Chris Lattner24943d22010-06-08 16:52:24 +0000722
Jim Inghamda26bd22012-06-08 21:56:10 +0000723class CommandObjectProcessDetach : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000724{
725public:
726
Greg Clayton238c0a12010-09-18 01:14:36 +0000727 CommandObjectProcessDetach (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000728 CommandObjectParsed (interpreter,
729 "process detach",
730 "Detach from the current process being debugged.",
731 "process detach",
732 eFlagProcessMustBeLaunched)
Chris Lattner24943d22010-06-08 16:52:24 +0000733 {
734 }
735
736 ~CommandObjectProcessDetach ()
737 {
738 }
739
Jim Inghamda26bd22012-06-08 21:56:10 +0000740protected:
Chris Lattner24943d22010-06-08 16:52:24 +0000741 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000742 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000743 CommandReturnObject &result)
744 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000745 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000746 if (process == NULL)
747 {
748 result.AppendError ("must have a valid process in order to detach");
749 result.SetStatus (eReturnStatusFailed);
750 return false;
751 }
752
Greg Clayton444e35b2011-10-19 18:09:39 +0000753 result.AppendMessageWithFormat ("Detaching from process %llu\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000754 Error error (process->Detach());
755 if (error.Success())
756 {
757 result.SetStatus (eReturnStatusSuccessFinishResult);
758 }
759 else
760 {
761 result.AppendErrorWithFormat ("Detach failed: %s\n", error.AsCString());
762 result.SetStatus (eReturnStatusFailed);
763 return false;
764 }
765 return result.Succeeded();
766 }
767};
768
769//-------------------------------------------------------------------------
Greg Claytone71e2582011-02-04 01:58:07 +0000770// CommandObjectProcessConnect
771//-------------------------------------------------------------------------
772#pragma mark CommandObjectProcessConnect
773
Jim Inghamda26bd22012-06-08 21:56:10 +0000774class CommandObjectProcessConnect : public CommandObjectParsed
Greg Claytone71e2582011-02-04 01:58:07 +0000775{
776public:
777
778 class CommandOptions : public Options
779 {
780 public:
781
Greg Claytonf15996e2011-04-07 22:46:35 +0000782 CommandOptions (CommandInterpreter &interpreter) :
783 Options(interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000784 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000785 // Keep default values of all options in one place: OptionParsingStarting ()
786 OptionParsingStarting ();
Greg Claytone71e2582011-02-04 01:58:07 +0000787 }
788
789 ~CommandOptions ()
790 {
791 }
792
793 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000794 SetOptionValue (uint32_t option_idx, const char *option_arg)
Greg Claytone71e2582011-02-04 01:58:07 +0000795 {
796 Error error;
797 char short_option = (char) m_getopt_table[option_idx].val;
798
799 switch (short_option)
800 {
801 case 'p':
802 plugin_name.assign (option_arg);
803 break;
804
805 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000806 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Greg Claytone71e2582011-02-04 01:58:07 +0000807 break;
808 }
809 return error;
810 }
811
812 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000813 OptionParsingStarting ()
Greg Claytone71e2582011-02-04 01:58:07 +0000814 {
Greg Claytone71e2582011-02-04 01:58:07 +0000815 plugin_name.clear();
816 }
817
Greg Claytonb3448432011-03-24 21:19:54 +0000818 const OptionDefinition*
Greg Claytone71e2582011-02-04 01:58:07 +0000819 GetDefinitions ()
820 {
821 return g_option_table;
822 }
823
824 // Options table: Required for subclasses of Options.
825
Greg Claytonb3448432011-03-24 21:19:54 +0000826 static OptionDefinition g_option_table[];
Greg Claytone71e2582011-02-04 01:58:07 +0000827
828 // Instance variables to hold the values for command options.
829
830 std::string plugin_name;
831 };
832
833 CommandObjectProcessConnect (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000834 CommandObjectParsed (interpreter,
835 "process connect",
836 "Connect to a remote debug service.",
837 "process connect <remote-url>",
838 0),
Greg Claytonf15996e2011-04-07 22:46:35 +0000839 m_options (interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000840 {
841 }
842
843 ~CommandObjectProcessConnect ()
844 {
845 }
846
847
Jim Inghamda26bd22012-06-08 21:56:10 +0000848 Options *
849 GetOptions ()
850 {
851 return &m_options;
852 }
853
854protected:
Greg Claytone71e2582011-02-04 01:58:07 +0000855 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000856 DoExecute (Args& command,
Greg Claytone71e2582011-02-04 01:58:07 +0000857 CommandReturnObject &result)
858 {
859
860 TargetSP target_sp (m_interpreter.GetDebugger().GetSelectedTarget());
861 Error error;
Greg Clayton567e7f32011-09-22 04:58:26 +0000862 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytone71e2582011-02-04 01:58:07 +0000863 if (process)
864 {
865 if (process->IsAlive())
866 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000867 result.AppendErrorWithFormat ("Process %llu is currently being debugged, kill the process before connecting.\n",
Greg Claytone71e2582011-02-04 01:58:07 +0000868 process->GetID());
869 result.SetStatus (eReturnStatusFailed);
870 return false;
871 }
872 }
873
874 if (!target_sp)
875 {
876 // If there isn't a current target create one.
877 FileSpec emptyFileSpec;
Greg Claytone71e2582011-02-04 01:58:07 +0000878
879 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
880 emptyFileSpec,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000881 NULL,
Greg Claytone71e2582011-02-04 01:58:07 +0000882 false,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000883 NULL, // No platform options
Greg Claytone71e2582011-02-04 01:58:07 +0000884 target_sp);
885 if (!target_sp || error.Fail())
886 {
887 result.AppendError(error.AsCString("Error creating target"));
888 result.SetStatus (eReturnStatusFailed);
889 return false;
890 }
891 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target_sp.get());
892 }
893
894 if (command.GetArgumentCount() == 1)
895 {
896 const char *plugin_name = NULL;
897 if (!m_options.plugin_name.empty())
898 plugin_name = m_options.plugin_name.c_str();
899
900 const char *remote_url = command.GetArgumentAtIndex(0);
Greg Clayton46c9a352012-02-09 06:16:32 +0000901 process = target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name, NULL).get();
Greg Claytone71e2582011-02-04 01:58:07 +0000902
903 if (process)
904 {
905 error = process->ConnectRemote (remote_url);
906
907 if (error.Fail())
908 {
909 result.AppendError(error.AsCString("Remote connect failed"));
910 result.SetStatus (eReturnStatusFailed);
Greg Clayton0cbb93b2012-03-31 00:10:30 +0000911 target_sp->DeleteCurrentProcess();
Greg Claytone71e2582011-02-04 01:58:07 +0000912 return false;
913 }
914 }
915 else
916 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000917 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",
918 m_cmd_name.c_str());
Greg Claytone71e2582011-02-04 01:58:07 +0000919 result.SetStatus (eReturnStatusFailed);
920 }
921 }
922 else
923 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000924 result.AppendErrorWithFormat ("'%s' takes exactly one argument:\nUsage: %s\n",
Greg Claytone71e2582011-02-04 01:58:07 +0000925 m_cmd_name.c_str(),
926 m_cmd_syntax.c_str());
927 result.SetStatus (eReturnStatusFailed);
928 }
929 return result.Succeeded();
930 }
Greg Claytone71e2582011-02-04 01:58:07 +0000931
932 CommandOptions m_options;
933};
934
935
Greg Claytonb3448432011-03-24 21:19:54 +0000936OptionDefinition
Greg Claytone71e2582011-02-04 01:58:07 +0000937CommandObjectProcessConnect::CommandOptions::g_option_table[] =
938{
939 { LLDB_OPT_SET_ALL, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
940 { 0, false, NULL, 0 , 0, NULL, 0, eArgTypeNone, NULL }
941};
942
943//-------------------------------------------------------------------------
Greg Clayton0baa3942010-11-04 01:54:29 +0000944// CommandObjectProcessLoad
945//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000946#pragma mark CommandObjectProcessLoad
Greg Clayton0baa3942010-11-04 01:54:29 +0000947
Jim Inghamda26bd22012-06-08 21:56:10 +0000948class CommandObjectProcessLoad : public CommandObjectParsed
Greg Clayton0baa3942010-11-04 01:54:29 +0000949{
950public:
951
952 CommandObjectProcessLoad (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000953 CommandObjectParsed (interpreter,
954 "process load",
955 "Load a shared library into the current process.",
956 "process load <filename> [<filename> ...]",
957 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
Greg Clayton0baa3942010-11-04 01:54:29 +0000958 {
959 }
960
961 ~CommandObjectProcessLoad ()
962 {
963 }
964
Jim Inghamda26bd22012-06-08 21:56:10 +0000965protected:
Greg Clayton0baa3942010-11-04 01:54:29 +0000966 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000967 DoExecute (Args& command,
Greg Clayton0baa3942010-11-04 01:54:29 +0000968 CommandReturnObject &result)
969 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000970 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton0baa3942010-11-04 01:54:29 +0000971 if (process == NULL)
972 {
973 result.AppendError ("must have a valid process in order to load a shared library");
974 result.SetStatus (eReturnStatusFailed);
975 return false;
976 }
977
978 const uint32_t argc = command.GetArgumentCount();
979
980 for (uint32_t i=0; i<argc; ++i)
981 {
982 Error error;
983 const char *image_path = command.GetArgumentAtIndex(i);
984 FileSpec image_spec (image_path, false);
Greg Claytonf2bf8702011-08-11 16:25:18 +0000985 process->GetTarget().GetPlatform()->ResolveRemotePath(image_spec, image_spec);
Greg Clayton0baa3942010-11-04 01:54:29 +0000986 uint32_t image_token = process->LoadImage(image_spec, error);
987 if (image_token != LLDB_INVALID_IMAGE_TOKEN)
988 {
989 result.AppendMessageWithFormat ("Loading \"%s\"...ok\nImage %u loaded.\n", image_path, image_token);
990 result.SetStatus (eReturnStatusSuccessFinishResult);
991 }
992 else
993 {
994 result.AppendErrorWithFormat ("failed to load '%s': %s", image_path, error.AsCString());
995 result.SetStatus (eReturnStatusFailed);
996 }
997 }
998 return result.Succeeded();
999 }
1000};
1001
1002
1003//-------------------------------------------------------------------------
1004// CommandObjectProcessUnload
1005//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001006#pragma mark CommandObjectProcessUnload
Greg Clayton0baa3942010-11-04 01:54:29 +00001007
Jim Inghamda26bd22012-06-08 21:56:10 +00001008class CommandObjectProcessUnload : public CommandObjectParsed
Greg Clayton0baa3942010-11-04 01:54:29 +00001009{
1010public:
1011
1012 CommandObjectProcessUnload (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001013 CommandObjectParsed (interpreter,
1014 "process unload",
1015 "Unload a shared library from the current process using the index returned by a previous call to \"process load\".",
1016 "process unload <index>",
1017 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
Greg Clayton0baa3942010-11-04 01:54:29 +00001018 {
1019 }
1020
1021 ~CommandObjectProcessUnload ()
1022 {
1023 }
1024
Jim Inghamda26bd22012-06-08 21:56:10 +00001025protected:
Greg Clayton0baa3942010-11-04 01:54:29 +00001026 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001027 DoExecute (Args& command,
Greg Clayton0baa3942010-11-04 01:54:29 +00001028 CommandReturnObject &result)
1029 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001030 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton0baa3942010-11-04 01:54:29 +00001031 if (process == NULL)
1032 {
1033 result.AppendError ("must have a valid process in order to load a shared library");
1034 result.SetStatus (eReturnStatusFailed);
1035 return false;
1036 }
1037
1038 const uint32_t argc = command.GetArgumentCount();
1039
1040 for (uint32_t i=0; i<argc; ++i)
1041 {
1042 const char *image_token_cstr = command.GetArgumentAtIndex(i);
1043 uint32_t image_token = Args::StringToUInt32(image_token_cstr, LLDB_INVALID_IMAGE_TOKEN, 0);
1044 if (image_token == LLDB_INVALID_IMAGE_TOKEN)
1045 {
1046 result.AppendErrorWithFormat ("invalid image index argument '%s'", image_token_cstr);
1047 result.SetStatus (eReturnStatusFailed);
1048 break;
1049 }
1050 else
1051 {
1052 Error error (process->UnloadImage(image_token));
1053 if (error.Success())
1054 {
1055 result.AppendMessageWithFormat ("Unloading shared library with index %u...ok\n", image_token);
1056 result.SetStatus (eReturnStatusSuccessFinishResult);
1057 }
1058 else
1059 {
1060 result.AppendErrorWithFormat ("failed to unload image: %s", error.AsCString());
1061 result.SetStatus (eReturnStatusFailed);
1062 break;
1063 }
1064 }
1065 }
1066 return result.Succeeded();
1067 }
1068};
1069
1070//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001071// CommandObjectProcessSignal
1072//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001073#pragma mark CommandObjectProcessSignal
Chris Lattner24943d22010-06-08 16:52:24 +00001074
Jim Inghamda26bd22012-06-08 21:56:10 +00001075class CommandObjectProcessSignal : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001076{
1077public:
1078
Greg Clayton238c0a12010-09-18 01:14:36 +00001079 CommandObjectProcessSignal (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001080 CommandObjectParsed (interpreter,
1081 "process signal",
1082 "Send a UNIX signal to the current process being debugged.",
1083 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001084 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001085 CommandArgumentEntry arg;
1086 CommandArgumentData signal_arg;
1087
1088 // Define the first (and only) variant of this arg.
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001089 signal_arg.arg_type = eArgTypeUnixSignal;
Caroline Tice43b014a2010-10-04 22:28:36 +00001090 signal_arg.arg_repetition = eArgRepeatPlain;
1091
1092 // There is only one variant this argument could be; put it into the argument entry.
1093 arg.push_back (signal_arg);
1094
1095 // Push the data for the first argument into the m_arguments vector.
1096 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001097 }
1098
1099 ~CommandObjectProcessSignal ()
1100 {
1101 }
1102
Jim Inghamda26bd22012-06-08 21:56:10 +00001103protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001104 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001105 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001106 CommandReturnObject &result)
1107 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001108 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001109 if (process == NULL)
1110 {
1111 result.AppendError ("no process to signal");
1112 result.SetStatus (eReturnStatusFailed);
1113 return false;
1114 }
1115
1116 if (command.GetArgumentCount() == 1)
1117 {
Greg Clayton8f6be2a2010-10-09 01:40:57 +00001118 int signo = LLDB_INVALID_SIGNAL_NUMBER;
1119
1120 const char *signal_name = command.GetArgumentAtIndex(0);
1121 if (::isxdigit (signal_name[0]))
1122 signo = Args::StringToSInt32(signal_name, LLDB_INVALID_SIGNAL_NUMBER, 0);
1123 else
1124 signo = process->GetUnixSignals().GetSignalNumberFromName (signal_name);
1125
1126 if (signo == LLDB_INVALID_SIGNAL_NUMBER)
Chris Lattner24943d22010-06-08 16:52:24 +00001127 {
1128 result.AppendErrorWithFormat ("Invalid signal argument '%s'.\n", command.GetArgumentAtIndex(0));
1129 result.SetStatus (eReturnStatusFailed);
1130 }
1131 else
1132 {
1133 Error error (process->Signal (signo));
1134 if (error.Success())
1135 {
1136 result.SetStatus (eReturnStatusSuccessFinishResult);
1137 }
1138 else
1139 {
1140 result.AppendErrorWithFormat ("Failed to send signal %i: %s\n", signo, error.AsCString());
1141 result.SetStatus (eReturnStatusFailed);
1142 }
1143 }
1144 }
1145 else
1146 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001147 result.AppendErrorWithFormat("'%s' takes exactly one signal number argument:\nUsage: %s\n", m_cmd_name.c_str(),
Chris Lattner24943d22010-06-08 16:52:24 +00001148 m_cmd_syntax.c_str());
1149 result.SetStatus (eReturnStatusFailed);
1150 }
1151 return result.Succeeded();
1152 }
1153};
1154
1155
1156//-------------------------------------------------------------------------
1157// CommandObjectProcessInterrupt
1158//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001159#pragma mark CommandObjectProcessInterrupt
Chris Lattner24943d22010-06-08 16:52:24 +00001160
Jim Inghamda26bd22012-06-08 21:56:10 +00001161class CommandObjectProcessInterrupt : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001162{
1163public:
1164
1165
Greg Clayton238c0a12010-09-18 01:14:36 +00001166 CommandObjectProcessInterrupt (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001167 CommandObjectParsed (interpreter,
1168 "process interrupt",
1169 "Interrupt the current process being debugged.",
1170 "process interrupt",
1171 eFlagProcessMustBeLaunched)
Chris Lattner24943d22010-06-08 16:52:24 +00001172 {
1173 }
1174
1175 ~CommandObjectProcessInterrupt ()
1176 {
1177 }
1178
Jim Inghamda26bd22012-06-08 21:56:10 +00001179protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001180 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001181 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001182 CommandReturnObject &result)
1183 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001184 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001185 if (process == NULL)
1186 {
1187 result.AppendError ("no process to halt");
1188 result.SetStatus (eReturnStatusFailed);
1189 return false;
1190 }
1191
1192 if (command.GetArgumentCount() == 0)
1193 {
1194 Error error(process->Halt ());
1195 if (error.Success())
1196 {
1197 result.SetStatus (eReturnStatusSuccessFinishResult);
1198
1199 // Maybe we should add a "SuspendThreadPlans so we
1200 // can halt, and keep in place all the current thread plans.
1201 process->GetThreadList().DiscardThreadPlans();
1202 }
1203 else
1204 {
1205 result.AppendErrorWithFormat ("Failed to halt process: %s\n", error.AsCString());
1206 result.SetStatus (eReturnStatusFailed);
1207 }
1208 }
1209 else
1210 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001211 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner24943d22010-06-08 16:52:24 +00001212 m_cmd_name.c_str(),
1213 m_cmd_syntax.c_str());
1214 result.SetStatus (eReturnStatusFailed);
1215 }
1216 return result.Succeeded();
1217 }
1218};
1219
1220//-------------------------------------------------------------------------
1221// CommandObjectProcessKill
1222//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001223#pragma mark CommandObjectProcessKill
Chris Lattner24943d22010-06-08 16:52:24 +00001224
Jim Inghamda26bd22012-06-08 21:56:10 +00001225class CommandObjectProcessKill : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001226{
1227public:
1228
Greg Clayton238c0a12010-09-18 01:14:36 +00001229 CommandObjectProcessKill (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001230 CommandObjectParsed (interpreter,
1231 "process kill",
1232 "Terminate the current process being debugged.",
1233 "process kill",
1234 eFlagProcessMustBeLaunched)
Chris Lattner24943d22010-06-08 16:52:24 +00001235 {
1236 }
1237
1238 ~CommandObjectProcessKill ()
1239 {
1240 }
1241
Jim Inghamda26bd22012-06-08 21:56:10 +00001242protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001243 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001244 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001245 CommandReturnObject &result)
1246 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001247 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001248 if (process == NULL)
1249 {
1250 result.AppendError ("no process to kill");
1251 result.SetStatus (eReturnStatusFailed);
1252 return false;
1253 }
1254
1255 if (command.GetArgumentCount() == 0)
1256 {
1257 Error error (process->Destroy());
1258 if (error.Success())
1259 {
1260 result.SetStatus (eReturnStatusSuccessFinishResult);
1261 }
1262 else
1263 {
1264 result.AppendErrorWithFormat ("Failed to kill process: %s\n", error.AsCString());
1265 result.SetStatus (eReturnStatusFailed);
1266 }
1267 }
1268 else
1269 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001270 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner24943d22010-06-08 16:52:24 +00001271 m_cmd_name.c_str(),
1272 m_cmd_syntax.c_str());
1273 result.SetStatus (eReturnStatusFailed);
1274 }
1275 return result.Succeeded();
1276 }
1277};
1278
1279//-------------------------------------------------------------------------
Jim Ingham41313fc2010-06-18 01:23:09 +00001280// CommandObjectProcessStatus
1281//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001282#pragma mark CommandObjectProcessStatus
1283
Jim Inghamda26bd22012-06-08 21:56:10 +00001284class CommandObjectProcessStatus : public CommandObjectParsed
Jim Ingham41313fc2010-06-18 01:23:09 +00001285{
1286public:
Greg Clayton238c0a12010-09-18 01:14:36 +00001287 CommandObjectProcessStatus (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001288 CommandObjectParsed (interpreter,
1289 "process status",
1290 "Show the current status and location of executing process.",
1291 "process status",
1292 0)
Jim Ingham41313fc2010-06-18 01:23:09 +00001293 {
1294 }
1295
1296 ~CommandObjectProcessStatus()
1297 {
1298 }
1299
1300
1301 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001302 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham41313fc2010-06-18 01:23:09 +00001303 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001304 Stream &strm = result.GetOutputStream();
Jim Ingham41313fc2010-06-18 01:23:09 +00001305 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Claytonb72d0f02011-04-12 05:54:46 +00001306 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
Greg Clayton567e7f32011-09-22 04:58:26 +00001307 Process *process = exe_ctx.GetProcessPtr();
1308 if (process)
Jim Ingham41313fc2010-06-18 01:23:09 +00001309 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001310 const bool only_threads_with_stop_reason = true;
1311 const uint32_t start_frame = 0;
1312 const uint32_t num_frames = 1;
1313 const uint32_t num_frames_with_source = 1;
Greg Clayton567e7f32011-09-22 04:58:26 +00001314 process->GetStatus(strm);
1315 process->GetThreadStatus (strm,
1316 only_threads_with_stop_reason,
1317 start_frame,
1318 num_frames,
1319 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001320
Jim Ingham41313fc2010-06-18 01:23:09 +00001321 }
1322 else
1323 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001324 result.AppendError ("No process.");
Jim Ingham41313fc2010-06-18 01:23:09 +00001325 result.SetStatus (eReturnStatusFailed);
1326 }
1327 return result.Succeeded();
1328 }
1329};
1330
1331//-------------------------------------------------------------------------
Caroline Tice23d6f272010-10-13 20:44:39 +00001332// CommandObjectProcessHandle
1333//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001334#pragma mark CommandObjectProcessHandle
Caroline Tice23d6f272010-10-13 20:44:39 +00001335
Jim Inghamda26bd22012-06-08 21:56:10 +00001336class CommandObjectProcessHandle : public CommandObjectParsed
Caroline Tice23d6f272010-10-13 20:44:39 +00001337{
1338public:
1339
1340 class CommandOptions : public Options
1341 {
1342 public:
1343
Greg Claytonf15996e2011-04-07 22:46:35 +00001344 CommandOptions (CommandInterpreter &interpreter) :
1345 Options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001346 {
Greg Clayton143fcc32011-04-13 00:18:08 +00001347 OptionParsingStarting ();
Caroline Tice23d6f272010-10-13 20:44:39 +00001348 }
1349
1350 ~CommandOptions ()
1351 {
1352 }
1353
1354 Error
Greg Clayton143fcc32011-04-13 00:18:08 +00001355 SetOptionValue (uint32_t option_idx, const char *option_arg)
Caroline Tice23d6f272010-10-13 20:44:39 +00001356 {
1357 Error error;
1358 char short_option = (char) m_getopt_table[option_idx].val;
1359
1360 switch (short_option)
1361 {
1362 case 's':
1363 stop = option_arg;
1364 break;
1365 case 'n':
1366 notify = option_arg;
1367 break;
1368 case 'p':
1369 pass = option_arg;
1370 break;
1371 default:
Greg Clayton9c236732011-10-26 00:56:27 +00001372 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Caroline Tice23d6f272010-10-13 20:44:39 +00001373 break;
1374 }
1375 return error;
1376 }
1377
1378 void
Greg Clayton143fcc32011-04-13 00:18:08 +00001379 OptionParsingStarting ()
Caroline Tice23d6f272010-10-13 20:44:39 +00001380 {
Caroline Tice23d6f272010-10-13 20:44:39 +00001381 stop.clear();
1382 notify.clear();
1383 pass.clear();
1384 }
1385
Greg Claytonb3448432011-03-24 21:19:54 +00001386 const OptionDefinition*
Caroline Tice23d6f272010-10-13 20:44:39 +00001387 GetDefinitions ()
1388 {
1389 return g_option_table;
1390 }
1391
1392 // Options table: Required for subclasses of Options.
1393
Greg Claytonb3448432011-03-24 21:19:54 +00001394 static OptionDefinition g_option_table[];
Caroline Tice23d6f272010-10-13 20:44:39 +00001395
1396 // Instance variables to hold the values for command options.
1397
1398 std::string stop;
1399 std::string notify;
1400 std::string pass;
1401 };
1402
1403
1404 CommandObjectProcessHandle (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001405 CommandObjectParsed (interpreter,
1406 "process handle",
1407 "Show or update what the process and debugger should do with various signals received from the OS.",
1408 NULL),
Greg Claytonf15996e2011-04-07 22:46:35 +00001409 m_options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001410 {
Caroline Ticee7471982010-10-14 21:31:13 +00001411 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 +00001412 CommandArgumentEntry arg;
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001413 CommandArgumentData signal_arg;
Caroline Tice23d6f272010-10-13 20:44:39 +00001414
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001415 signal_arg.arg_type = eArgTypeUnixSignal;
1416 signal_arg.arg_repetition = eArgRepeatStar;
Caroline Tice23d6f272010-10-13 20:44:39 +00001417
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001418 arg.push_back (signal_arg);
Caroline Tice23d6f272010-10-13 20:44:39 +00001419
1420 m_arguments.push_back (arg);
1421 }
1422
1423 ~CommandObjectProcessHandle ()
1424 {
1425 }
1426
1427 Options *
1428 GetOptions ()
1429 {
1430 return &m_options;
1431 }
1432
1433 bool
Caroline Ticee7471982010-10-14 21:31:13 +00001434 VerifyCommandOptionValue (const std::string &option, int &real_value)
Caroline Tice23d6f272010-10-13 20:44:39 +00001435 {
1436 bool okay = true;
1437
Caroline Ticee7471982010-10-14 21:31:13 +00001438 bool success = false;
1439 bool tmp_value = Args::StringToBoolean (option.c_str(), false, &success);
1440
1441 if (success && tmp_value)
1442 real_value = 1;
1443 else if (success && !tmp_value)
1444 real_value = 0;
Caroline Tice23d6f272010-10-13 20:44:39 +00001445 else
1446 {
1447 // If the value isn't 'true' or 'false', it had better be 0 or 1.
Caroline Ticee7471982010-10-14 21:31:13 +00001448 real_value = Args::StringToUInt32 (option.c_str(), 3);
1449 if (real_value != 0 && real_value != 1)
Caroline Tice23d6f272010-10-13 20:44:39 +00001450 okay = false;
1451 }
1452
1453 return okay;
1454 }
1455
Caroline Ticee7471982010-10-14 21:31:13 +00001456 void
1457 PrintSignalHeader (Stream &str)
1458 {
1459 str.Printf ("NAME PASS STOP NOTIFY\n");
1460 str.Printf ("========== ===== ===== ======\n");
1461 }
1462
1463 void
1464 PrintSignal (Stream &str, int32_t signo, const char *sig_name, UnixSignals &signals)
1465 {
1466 bool stop;
1467 bool suppress;
1468 bool notify;
1469
1470 str.Printf ("%-10s ", sig_name);
1471 if (signals.GetSignalInfo (signo, suppress, stop, notify))
1472 {
1473 bool pass = !suppress;
1474 str.Printf ("%s %s %s",
1475 (pass ? "true " : "false"),
1476 (stop ? "true " : "false"),
1477 (notify ? "true " : "false"));
1478 }
1479 str.Printf ("\n");
1480 }
1481
1482 void
1483 PrintSignalInformation (Stream &str, Args &signal_args, int num_valid_signals, UnixSignals &signals)
1484 {
1485 PrintSignalHeader (str);
1486
1487 if (num_valid_signals > 0)
1488 {
1489 size_t num_args = signal_args.GetArgumentCount();
1490 for (size_t i = 0; i < num_args; ++i)
1491 {
1492 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1493 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
1494 PrintSignal (str, signo, signal_args.GetArgumentAtIndex (i), signals);
1495 }
1496 }
1497 else // Print info for ALL signals
1498 {
1499 int32_t signo = signals.GetFirstSignalNumber();
1500 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1501 {
1502 PrintSignal (str, signo, signals.GetSignalAsCString (signo), signals);
1503 signo = signals.GetNextSignalNumber (signo);
1504 }
1505 }
1506 }
1507
Jim Inghamda26bd22012-06-08 21:56:10 +00001508protected:
Caroline Tice23d6f272010-10-13 20:44:39 +00001509 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001510 DoExecute (Args &signal_args, CommandReturnObject &result)
Caroline Tice23d6f272010-10-13 20:44:39 +00001511 {
1512 TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
1513
1514 if (!target_sp)
1515 {
1516 result.AppendError ("No current target;"
1517 " cannot handle signals until you have a valid target and process.\n");
1518 result.SetStatus (eReturnStatusFailed);
1519 return false;
1520 }
1521
1522 ProcessSP process_sp = target_sp->GetProcessSP();
1523
1524 if (!process_sp)
1525 {
1526 result.AppendError ("No current process; cannot handle signals until you have a valid process.\n");
1527 result.SetStatus (eReturnStatusFailed);
1528 return false;
1529 }
1530
Caroline Tice23d6f272010-10-13 20:44:39 +00001531 int stop_action = -1; // -1 means leave the current setting alone
Caroline Ticee7471982010-10-14 21:31:13 +00001532 int pass_action = -1; // -1 means leave the current setting alone
Caroline Tice23d6f272010-10-13 20:44:39 +00001533 int notify_action = -1; // -1 means leave the current setting alone
1534
1535 if (! m_options.stop.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001536 && ! VerifyCommandOptionValue (m_options.stop, stop_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001537 {
1538 result.AppendError ("Invalid argument for command option --stop; must be true or false.\n");
1539 result.SetStatus (eReturnStatusFailed);
1540 return false;
1541 }
1542
1543 if (! m_options.notify.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001544 && ! VerifyCommandOptionValue (m_options.notify, notify_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001545 {
1546 result.AppendError ("Invalid argument for command option --notify; must be true or false.\n");
1547 result.SetStatus (eReturnStatusFailed);
1548 return false;
1549 }
1550
1551 if (! m_options.pass.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001552 && ! VerifyCommandOptionValue (m_options.pass, pass_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001553 {
1554 result.AppendError ("Invalid argument for command option --pass; must be true or false.\n");
1555 result.SetStatus (eReturnStatusFailed);
1556 return false;
1557 }
1558
1559 size_t num_args = signal_args.GetArgumentCount();
1560 UnixSignals &signals = process_sp->GetUnixSignals();
1561 int num_signals_set = 0;
1562
Caroline Ticee7471982010-10-14 21:31:13 +00001563 if (num_args > 0)
Caroline Tice23d6f272010-10-13 20:44:39 +00001564 {
Caroline Ticee7471982010-10-14 21:31:13 +00001565 for (size_t i = 0; i < num_args; ++i)
Caroline Tice23d6f272010-10-13 20:44:39 +00001566 {
Caroline Ticee7471982010-10-14 21:31:13 +00001567 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1568 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
Caroline Tice23d6f272010-10-13 20:44:39 +00001569 {
Caroline Ticee7471982010-10-14 21:31:13 +00001570 // Casting the actions as bools here should be okay, because VerifyCommandOptionValue guarantees
1571 // the value is either 0 or 1.
1572 if (stop_action != -1)
1573 signals.SetShouldStop (signo, (bool) stop_action);
1574 if (pass_action != -1)
1575 {
1576 bool suppress = ! ((bool) pass_action);
1577 signals.SetShouldSuppress (signo, suppress);
1578 }
1579 if (notify_action != -1)
1580 signals.SetShouldNotify (signo, (bool) notify_action);
1581 ++num_signals_set;
Caroline Tice23d6f272010-10-13 20:44:39 +00001582 }
Caroline Ticee7471982010-10-14 21:31:13 +00001583 else
1584 {
1585 result.AppendErrorWithFormat ("Invalid signal name '%s'\n", signal_args.GetArgumentAtIndex (i));
1586 }
Caroline Tice23d6f272010-10-13 20:44:39 +00001587 }
1588 }
Caroline Ticee7471982010-10-14 21:31:13 +00001589 else
1590 {
1591 // No signal specified, if any command options were specified, update ALL signals.
1592 if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1))
1593 {
1594 if (m_interpreter.Confirm ("Do you really want to update all the signals?", false))
1595 {
1596 int32_t signo = signals.GetFirstSignalNumber();
1597 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1598 {
1599 if (notify_action != -1)
1600 signals.SetShouldNotify (signo, (bool) notify_action);
1601 if (stop_action != -1)
1602 signals.SetShouldStop (signo, (bool) stop_action);
1603 if (pass_action != -1)
1604 {
1605 bool suppress = ! ((bool) pass_action);
1606 signals.SetShouldSuppress (signo, suppress);
1607 }
1608 signo = signals.GetNextSignalNumber (signo);
1609 }
1610 }
1611 }
1612 }
1613
1614 PrintSignalInformation (result.GetOutputStream(), signal_args, num_signals_set, signals);
Caroline Tice23d6f272010-10-13 20:44:39 +00001615
1616 if (num_signals_set > 0)
1617 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1618 else
1619 result.SetStatus (eReturnStatusFailed);
1620
1621 return result.Succeeded();
1622 }
1623
Caroline Tice23d6f272010-10-13 20:44:39 +00001624 CommandOptions m_options;
1625};
1626
Greg Claytonb3448432011-03-24 21:19:54 +00001627OptionDefinition
Caroline Tice23d6f272010-10-13 20:44:39 +00001628CommandObjectProcessHandle::CommandOptions::g_option_table[] =
1629{
1630{ 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." },
1631{ 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." },
1632{ LLDB_OPT_SET_1, false, "pass", 'p', required_argument, NULL, 0, eArgTypeBoolean, "Whether or not the signal should be passed to the process." },
1633{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1634};
1635
1636//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001637// CommandObjectMultiwordProcess
1638//-------------------------------------------------------------------------
1639
Greg Clayton63094e02010-06-23 01:19:29 +00001640CommandObjectMultiwordProcess::CommandObjectMultiwordProcess (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00001641 CommandObjectMultiword (interpreter,
1642 "process",
1643 "A set of commands for operating on a process.",
1644 "process <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00001645{
Greg Claytona9eb8272011-07-02 21:07:54 +00001646 LoadSubCommand ("attach", CommandObjectSP (new CommandObjectProcessAttach (interpreter)));
1647 LoadSubCommand ("launch", CommandObjectSP (new CommandObjectProcessLaunch (interpreter)));
1648 LoadSubCommand ("continue", CommandObjectSP (new CommandObjectProcessContinue (interpreter)));
1649 LoadSubCommand ("connect", CommandObjectSP (new CommandObjectProcessConnect (interpreter)));
1650 LoadSubCommand ("detach", CommandObjectSP (new CommandObjectProcessDetach (interpreter)));
1651 LoadSubCommand ("load", CommandObjectSP (new CommandObjectProcessLoad (interpreter)));
1652 LoadSubCommand ("unload", CommandObjectSP (new CommandObjectProcessUnload (interpreter)));
1653 LoadSubCommand ("signal", CommandObjectSP (new CommandObjectProcessSignal (interpreter)));
1654 LoadSubCommand ("handle", CommandObjectSP (new CommandObjectProcessHandle (interpreter)));
1655 LoadSubCommand ("status", CommandObjectSP (new CommandObjectProcessStatus (interpreter)));
Greg Clayton238c0a12010-09-18 01:14:36 +00001656 LoadSubCommand ("interrupt", CommandObjectSP (new CommandObjectProcessInterrupt (interpreter)));
Greg Claytona9eb8272011-07-02 21:07:54 +00001657 LoadSubCommand ("kill", CommandObjectSP (new CommandObjectProcessKill (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00001658}
1659
1660CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess ()
1661{
1662}
1663