blob: 0dbf54f0f5191ee781de9fd14b86fb12f037ae4c [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
Chris Lattner24943d22010-06-08 16:52:24 +000034class CommandObjectProcessLaunch : public CommandObject
35{
36public:
37
Greg Clayton238c0a12010-09-18 01:14:36 +000038 CommandObjectProcessLaunch (CommandInterpreter &interpreter) :
39 CommandObject (interpreter,
40 "process launch",
Caroline Ticeabb507a2010-09-08 21:06:11 +000041 "Launch the executable in the debugger.",
Greg Claytonf15996e2011-04-07 22:46:35 +000042 NULL),
43 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
70 bool
Greg Claytond8c62532010-10-07 04:19:01 +000071 Execute (Args& launch_args, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +000072 {
Greg Claytonabb33022011-11-08 02:43:13 +000073 Debugger &debugger = m_interpreter.GetDebugger();
74 Target *target = debugger.GetSelectedTarget().get();
75 Error error;
Chris Lattner24943d22010-06-08 16:52:24 +000076
77 if (target == NULL)
78 {
Greg Claytone1f50b92011-05-03 22:09:39 +000079 result.AppendError ("invalid target, create a debug target using the 'target create' command");
Chris Lattner24943d22010-06-08 16:52:24 +000080 result.SetStatus (eReturnStatusFailed);
81 return false;
82 }
Chris Lattner24943d22010-06-08 16:52:24 +000083 // If our listener is NULL, users aren't allows to launch
Chris Lattner24943d22010-06-08 16:52:24 +000084 char filename[PATH_MAX];
Greg Clayton5beb99d2011-08-11 02:48:45 +000085 const Module *exe_module = target->GetExecutableModulePointer();
Greg Claytona2f74232011-02-24 22:24:29 +000086
87 if (exe_module == NULL)
88 {
Greg Claytone1f50b92011-05-03 22:09:39 +000089 result.AppendError ("no file in target, create a debug target using the 'target create' command");
Greg Claytona2f74232011-02-24 22:24:29 +000090 result.SetStatus (eReturnStatusFailed);
91 return false;
92 }
93
Greg Clayton36bc5ea2011-11-03 21:22:33 +000094 exe_module->GetFileSpec().GetPath (filename, sizeof(filename));
Chris Lattner24943d22010-06-08 16:52:24 +000095
Greg Clayton36bc5ea2011-11-03 21:22:33 +000096 const bool add_exe_file_as_first_arg = true;
Greg Clayton1d1f39e2011-11-29 04:03:30 +000097 m_options.launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), add_exe_file_as_first_arg);
Greg Clayton36bc5ea2011-11-03 21:22:33 +000098
Greg Claytona2f74232011-02-24 22:24:29 +000099 StateType state = eStateInvalid;
Greg Clayton567e7f32011-09-22 04:58:26 +0000100 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytona2f74232011-02-24 22:24:29 +0000101 if (process)
102 {
103 state = process->GetState();
104
105 if (process->IsAlive() && state != eStateConnected)
106 {
107 char message[1024];
108 if (process->GetState() == eStateAttaching)
109 ::strncpy (message, "There is a pending attach, abort it and launch a new process?", sizeof(message));
110 else
111 ::strncpy (message, "There is a running process, kill it and restart?", sizeof(message));
112
113 if (!m_interpreter.Confirm (message, true))
Jim Ingham22dc9722010-12-09 18:58:16 +0000114 {
Greg Claytona2f74232011-02-24 22:24:29 +0000115 result.SetStatus (eReturnStatusFailed);
116 return false;
Jim Ingham22dc9722010-12-09 18:58:16 +0000117 }
118 else
119 {
Greg Claytonabb33022011-11-08 02:43:13 +0000120 Error destroy_error (process->Destroy());
121 if (destroy_error.Success())
Greg Claytona2f74232011-02-24 22:24:29 +0000122 {
123 result.SetStatus (eReturnStatusSuccessFinishResult);
124 }
125 else
126 {
Greg Claytonabb33022011-11-08 02:43:13 +0000127 result.AppendErrorWithFormat ("Failed to kill process: %s\n", destroy_error.AsCString());
Greg Claytona2f74232011-02-24 22:24:29 +0000128 result.SetStatus (eReturnStatusFailed);
129 }
Jim Ingham22dc9722010-12-09 18:58:16 +0000130 }
131 }
Chris Lattner24943d22010-06-08 16:52:24 +0000132 }
Jim Ingham22dc9722010-12-09 18:58:16 +0000133
Greg Clayton527154d2011-11-15 03:53:30 +0000134 if (launch_args.GetArgumentCount() == 0)
135 {
136 const Args &process_args = target->GetRunArguments();
137 if (process_args.GetArgumentCount() > 0)
138 m_options.launch_info.GetArguments().AppendArguments (process_args);
139 }
140 else
Greg Claytonabb33022011-11-08 02:43:13 +0000141 {
Greg Clayton3e6f2cc2011-11-21 21:51:18 +0000142 // Save the arguments for subsequent runs in the current target.
143 target->SetRunArguments (launch_args);
144
Greg Claytonabb33022011-11-08 02:43:13 +0000145 m_options.launch_info.GetArguments().AppendArguments (launch_args);
146 }
Greg Claytonabb33022011-11-08 02:43:13 +0000147
Greg Clayton527154d2011-11-15 03:53:30 +0000148 if (target->GetDisableASLR())
149 m_options.launch_info.GetFlags().Set (eLaunchFlagDisableASLR);
150
151 if (target->GetDisableSTDIO())
152 m_options.launch_info.GetFlags().Set (eLaunchFlagDisableSTDIO);
153
154 m_options.launch_info.GetFlags().Set (eLaunchFlagDebug);
155
156 Args environment;
157 target->GetEnvironmentAsArgs (environment);
158 if (environment.GetArgumentCount() > 0)
159 m_options.launch_info.GetEnvironmentEntries ().AppendArguments (environment);
160
Greg Clayton464c6162011-11-17 22:14:31 +0000161 // Finalize the file actions, and if none were given, default to opening
162 // up a pseudo terminal
163 const bool default_to_use_pty = true;
164 m_options.launch_info.FinalizeFileActions (target, default_to_use_pty);
Greg Clayton527154d2011-11-15 03:53:30 +0000165
Greg Claytonabb33022011-11-08 02:43:13 +0000166 if (state == eStateConnected)
167 {
168 if (m_options.launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
169 {
170 result.AppendWarning("can't launch in tty when launching through a remote connection");
171 m_options.launch_info.GetFlags().Clear (eLaunchFlagLaunchInTTY);
172 }
173 }
174 else
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000175 {
Greg Clayton527154d2011-11-15 03:53:30 +0000176 if (!m_options.launch_info.GetArchitecture().IsValid())
Greg Clayton2d9adb72011-11-12 02:10:56 +0000177 m_options.launch_info.GetArchitecture() = target->GetArchitecture();
178
Greg Clayton75d8c252011-11-28 01:45:00 +0000179 PlatformSP platform_sp (target->GetPlatform());
180
181 if (platform_sp && platform_sp->CanDebugProcess ())
182 {
183 process = target->GetPlatform()->DebugProcess (m_options.launch_info,
184 debugger,
185 target,
186 debugger.GetListener(),
187 error).get();
188 }
189 else
190 {
191 const char *plugin_name = m_options.launch_info.GetProcessPluginName();
Greg Clayton46c9a352012-02-09 06:16:32 +0000192 process = target->CreateProcess (debugger.GetListener(), plugin_name, NULL).get();
Greg Clayton75d8c252011-11-28 01:45:00 +0000193 if (process)
194 error = process->Launch (m_options.launch_info);
195 }
Greg Claytonabb33022011-11-08 02:43:13 +0000196
Greg Claytona2f74232011-02-24 22:24:29 +0000197 if (process == NULL)
198 {
Greg Clayton527154d2011-11-15 03:53:30 +0000199 result.SetError (error, "failed to launch or debug process");
Greg Claytona2f74232011-02-24 22:24:29 +0000200 return false;
201 }
Chris Lattner24943d22010-06-08 16:52:24 +0000202 }
Greg Claytonabb33022011-11-08 02:43:13 +0000203
Greg Clayton238c0a12010-09-18 01:14:36 +0000204 if (error.Success())
205 {
Greg Clayton940b1032011-02-23 00:35:02 +0000206 const char *archname = exe_module->GetArchitecture().GetArchitectureName();
Greg Claytonc1d37752010-10-18 01:45:30 +0000207
Greg Clayton444e35b2011-10-19 18:09:39 +0000208 result.AppendMessageWithFormat ("Process %llu launched: '%s' (%s)\n", process->GetID(), filename, archname);
Greg Claytond8c62532010-10-07 04:19:01 +0000209 result.SetDidChangeProcessState (true);
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000210 if (m_options.launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
Greg Clayton238c0a12010-09-18 01:14:36 +0000211 {
Greg Claytond8c62532010-10-07 04:19:01 +0000212 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
Greg Clayton238c0a12010-09-18 01:14:36 +0000213 StateType state = process->WaitForProcessToStop (NULL);
214
215 if (state == eStateStopped)
216 {
Greg Claytond8c62532010-10-07 04:19:01 +0000217 error = process->Resume();
218 if (error.Success())
219 {
220 bool synchronous_execution = m_interpreter.GetSynchronous ();
221 if (synchronous_execution)
222 {
223 state = process->WaitForProcessToStop (NULL);
Greg Clayton20206082011-11-17 01:23:07 +0000224 const bool must_be_alive = true;
225 if (!StateIsStoppedState(state, must_be_alive))
Greg Clayton395fc332011-02-15 21:59:32 +0000226 {
Greg Clayton527154d2011-11-15 03:53:30 +0000227 result.AppendErrorWithFormat ("process isn't stopped: %s", StateAsCString(state));
Greg Clayton395fc332011-02-15 21:59:32 +0000228 }
Greg Claytond8c62532010-10-07 04:19:01 +0000229 result.SetDidChangeProcessState (true);
230 result.SetStatus (eReturnStatusSuccessFinishResult);
231 }
232 else
233 {
234 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
235 }
236 }
Greg Clayton395fc332011-02-15 21:59:32 +0000237 else
238 {
Greg Clayton527154d2011-11-15 03:53:30 +0000239 result.AppendErrorWithFormat ("process resume at entry point failed: %s", error.AsCString());
Greg Clayton395fc332011-02-15 21:59:32 +0000240 result.SetStatus (eReturnStatusFailed);
241 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000242 }
Greg Clayton395fc332011-02-15 21:59:32 +0000243 else
244 {
Greg Clayton527154d2011-11-15 03:53:30 +0000245 result.AppendErrorWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
Greg Clayton395fc332011-02-15 21:59:32 +0000246 result.SetStatus (eReturnStatusFailed);
247 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000248 }
249 }
Greg Clayton395fc332011-02-15 21:59:32 +0000250 else
251 {
Greg Claytona9eb8272011-07-02 21:07:54 +0000252 result.AppendErrorWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton395fc332011-02-15 21:59:32 +0000253 result.SetStatus (eReturnStatusFailed);
254 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000255
Chris Lattner24943d22010-06-08 16:52:24 +0000256 return result.Succeeded();
257 }
258
Jim Ingham767af882010-07-07 03:36:20 +0000259 virtual const char *GetRepeatCommand (Args &current_command_args, uint32_t index)
260 {
261 // No repeat for "process launch"...
262 return "";
263 }
264
Chris Lattner24943d22010-06-08 16:52:24 +0000265protected:
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000266 ProcessLaunchCommandOptions m_options;
Chris Lattner24943d22010-06-08 16:52:24 +0000267};
268
269
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000270//#define SET1 LLDB_OPT_SET_1
271//#define SET2 LLDB_OPT_SET_2
272//#define SET3 LLDB_OPT_SET_3
273//
274//OptionDefinition
275//CommandObjectProcessLaunch::CommandOptions::g_option_table[] =
276//{
277//{ 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."},
278//{ SET1 , false, "stdin", 'i', required_argument, NULL, 0, eArgTypePath, "Redirect stdin for the process to <path>."},
279//{ SET1 , false, "stdout", 'o', required_argument, NULL, 0, eArgTypePath, "Redirect stdout for the process to <path>."},
280//{ SET1 , false, "stderr", 'e', required_argument, NULL, 0, eArgTypePath, "Redirect stderr for the process to <path>."},
281//{ SET1 | SET2 | SET3, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
282//{ 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."},
283//{ SET3, false, "no-stdio", 'n', no_argument, NULL, 0, eArgTypeNone, "Do not set up for terminal I/O to go to running process."},
284//{ SET1 | SET2 | SET3, false, "working-dir", 'w', required_argument, NULL, 0, eArgTypePath, "Set the current working directory to <path> when running the inferior."},
285//{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
286//};
287//
288//#undef SET1
289//#undef SET2
290//#undef SET3
Chris Lattner24943d22010-06-08 16:52:24 +0000291
292//-------------------------------------------------------------------------
293// CommandObjectProcessAttach
294//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000295#pragma mark CommandObjectProcessAttach
Chris Lattner24943d22010-06-08 16:52:24 +0000296class CommandObjectProcessAttach : public CommandObject
297{
298public:
299
Chris Lattner24943d22010-06-08 16:52:24 +0000300 class CommandOptions : public Options
301 {
302 public:
303
Greg Claytonf15996e2011-04-07 22:46:35 +0000304 CommandOptions (CommandInterpreter &interpreter) :
305 Options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000306 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000307 // Keep default values of all options in one place: OptionParsingStarting ()
308 OptionParsingStarting ();
Chris Lattner24943d22010-06-08 16:52:24 +0000309 }
310
311 ~CommandOptions ()
312 {
313 }
314
315 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000316 SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +0000317 {
318 Error error;
319 char short_option = (char) m_getopt_table[option_idx].val;
320 bool success = false;
321 switch (short_option)
322 {
323 case 'p':
Chris Lattner24943d22010-06-08 16:52:24 +0000324 {
Greg Clayton527154d2011-11-15 03:53:30 +0000325 lldb::pid_t pid = Args::StringToUInt32 (option_arg, LLDB_INVALID_PROCESS_ID, 0, &success);
326 if (!success || pid == LLDB_INVALID_PROCESS_ID)
327 {
328 error.SetErrorStringWithFormat("invalid process ID '%s'", option_arg);
329 }
330 else
331 {
332 attach_info.SetProcessID (pid);
333 }
Chris Lattner24943d22010-06-08 16:52:24 +0000334 }
335 break;
336
337 case 'P':
Greg Clayton527154d2011-11-15 03:53:30 +0000338 attach_info.SetProcessPluginName (option_arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000339 break;
340
341 case 'n':
Greg Clayton527154d2011-11-15 03:53:30 +0000342 attach_info.GetExecutableFile().SetFile(option_arg, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000343 break;
344
345 case 'w':
Greg Clayton527154d2011-11-15 03:53:30 +0000346 attach_info.SetWaitForLaunch(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000347 break;
348
349 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000350 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner24943d22010-06-08 16:52:24 +0000351 break;
352 }
353 return error;
354 }
355
356 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000357 OptionParsingStarting ()
Chris Lattner24943d22010-06-08 16:52:24 +0000358 {
Greg Clayton527154d2011-11-15 03:53:30 +0000359 attach_info.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000360 }
361
Greg Claytonb3448432011-03-24 21:19:54 +0000362 const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +0000363 GetDefinitions ()
364 {
365 return g_option_table;
366 }
367
Jim Ingham7508e732010-08-09 23:31:02 +0000368 virtual bool
Greg Claytonf15996e2011-04-07 22:46:35 +0000369 HandleOptionArgumentCompletion (Args &input,
Jim Ingham7508e732010-08-09 23:31:02 +0000370 int cursor_index,
371 int char_pos,
372 OptionElementVector &opt_element_vector,
373 int opt_element_index,
374 int match_start_point,
375 int max_return_elements,
376 bool &word_complete,
377 StringList &matches)
378 {
379 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
380 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
381
382 // We are only completing the name option for now...
383
Greg Claytonb3448432011-03-24 21:19:54 +0000384 const OptionDefinition *opt_defs = GetDefinitions();
Jim Ingham7508e732010-08-09 23:31:02 +0000385 if (opt_defs[opt_defs_index].short_option == 'n')
386 {
387 // Are we in the name?
388
389 // Look to see if there is a -P argument provided, and if so use that plugin, otherwise
390 // use the default plugin.
Jim Ingham7508e732010-08-09 23:31:02 +0000391
392 const char *partial_name = NULL;
393 partial_name = input.GetArgumentAtIndex(opt_arg_pos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000394
Greg Claytonb72d0f02011-04-12 05:54:46 +0000395 PlatformSP platform_sp (m_interpreter.GetPlatform (true));
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000396 if (platform_sp)
Jim Ingham7508e732010-08-09 23:31:02 +0000397 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000398 ProcessInstanceInfoList process_infos;
399 ProcessInstanceInfoMatch match_info;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000400 if (partial_name)
401 {
Greg Clayton527154d2011-11-15 03:53:30 +0000402 match_info.GetProcessInfo().GetExecutableFile().SetFile(partial_name, false);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000403 match_info.SetNameMatchType(eNameMatchStartsWith);
404 }
405 platform_sp->FindProcesses (match_info, process_infos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000406 const uint32_t num_matches = process_infos.GetSize();
407 if (num_matches > 0)
408 {
409 for (uint32_t i=0; i<num_matches; ++i)
410 {
411 matches.AppendString (process_infos.GetProcessNameAtIndex(i),
412 process_infos.GetProcessNameLengthAtIndex(i));
413 }
414 }
Jim Ingham7508e732010-08-09 23:31:02 +0000415 }
416 }
417
418 return false;
419 }
420
Chris Lattner24943d22010-06-08 16:52:24 +0000421 // Options table: Required for subclasses of Options.
422
Greg Claytonb3448432011-03-24 21:19:54 +0000423 static OptionDefinition g_option_table[];
Chris Lattner24943d22010-06-08 16:52:24 +0000424
425 // Instance variables to hold the values for command options.
426
Greg Clayton527154d2011-11-15 03:53:30 +0000427 ProcessAttachInfo attach_info;
Chris Lattner24943d22010-06-08 16:52:24 +0000428 };
429
Greg Clayton238c0a12010-09-18 01:14:36 +0000430 CommandObjectProcessAttach (CommandInterpreter &interpreter) :
431 CommandObject (interpreter,
432 "process attach",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000433 "Attach to a process.",
Greg Claytonf15996e2011-04-07 22:46:35 +0000434 "process attach <cmd-options>"),
435 m_options (interpreter)
Jim Ingham7508e732010-08-09 23:31:02 +0000436 {
Jim Ingham7508e732010-08-09 23:31:02 +0000437 }
438
439 ~CommandObjectProcessAttach ()
440 {
441 }
442
443 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000444 Execute (Args& command,
Jim Ingham7508e732010-08-09 23:31:02 +0000445 CommandReturnObject &result)
446 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000447 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Jim Inghamee940e22011-09-15 01:08:57 +0000448 // N.B. The attach should be synchronous. It doesn't help much to get the prompt back between initiating the attach
449 // and the target actually stopping. So even if the interpreter is set to be asynchronous, we wait for the stop
450 // ourselves here.
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000451
Greg Clayton567e7f32011-09-22 04:58:26 +0000452 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytona2f74232011-02-24 22:24:29 +0000453 StateType state = eStateInvalid;
Jim Ingham7508e732010-08-09 23:31:02 +0000454 if (process)
455 {
Greg Claytona2f74232011-02-24 22:24:29 +0000456 state = process->GetState();
457 if (process->IsAlive() && state != eStateConnected)
Jim Ingham7508e732010-08-09 23:31:02 +0000458 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000459 result.AppendErrorWithFormat ("Process %llu is currently being debugged, kill the process before attaching.\n",
Jim Ingham7508e732010-08-09 23:31:02 +0000460 process->GetID());
461 result.SetStatus (eReturnStatusFailed);
462 return false;
463 }
464 }
465
466 if (target == NULL)
467 {
468 // If there isn't a current target create one.
469 TargetSP new_target_sp;
470 FileSpec emptyFileSpec;
Jim Ingham7508e732010-08-09 23:31:02 +0000471 Error error;
472
Greg Clayton238c0a12010-09-18 01:14:36 +0000473 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
474 emptyFileSpec,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000475 NULL,
Greg Clayton238c0a12010-09-18 01:14:36 +0000476 false,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000477 NULL, // No platform options
Greg Clayton238c0a12010-09-18 01:14:36 +0000478 new_target_sp);
Jim Ingham7508e732010-08-09 23:31:02 +0000479 target = new_target_sp.get();
480 if (target == NULL || error.Fail())
481 {
Greg Claytone71e2582011-02-04 01:58:07 +0000482 result.AppendError(error.AsCString("Error creating target"));
Jim Ingham7508e732010-08-09 23:31:02 +0000483 return false;
484 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000485 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target);
Jim Ingham7508e732010-08-09 23:31:02 +0000486 }
487
488 // Record the old executable module, we want to issue a warning if the process of attaching changed the
489 // current executable (like somebody said "file foo" then attached to a PID whose executable was bar.)
490
491 ModuleSP old_exec_module_sp = target->GetExecutableModule();
492 ArchSpec old_arch_spec = target->GetArchitecture();
493
494 if (command.GetArgumentCount())
495 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000496 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 +0000497 result.SetStatus (eReturnStatusFailed);
498 }
499 else
500 {
Greg Claytona2f74232011-02-24 22:24:29 +0000501 if (state != eStateConnected)
502 {
Greg Clayton527154d2011-11-15 03:53:30 +0000503 const char *plugin_name = m_options.attach_info.GetProcessPluginName();
Greg Clayton46c9a352012-02-09 06:16:32 +0000504 process = target->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name, NULL).get();
Greg Claytona2f74232011-02-24 22:24:29 +0000505 }
Jim Ingham7508e732010-08-09 23:31:02 +0000506
507 if (process)
508 {
509 Error error;
Greg Clayton527154d2011-11-15 03:53:30 +0000510 // If no process info was specified, then use the target executable
511 // name as the process to attach to by default
512 if (!m_options.attach_info.ProcessInfoSpecified ())
Jim Ingham4805a1c2010-09-15 01:34:14 +0000513 {
514 if (old_exec_module_sp)
Greg Clayton1d1f39e2011-11-29 04:03:30 +0000515 m_options.attach_info.GetExecutableFile().GetFilename() = old_exec_module_sp->GetPlatformFileSpec().GetFilename();
Jim Ingham4805a1c2010-09-15 01:34:14 +0000516
Greg Clayton527154d2011-11-15 03:53:30 +0000517 if (!m_options.attach_info.ProcessInfoSpecified ())
518 {
519 error.SetErrorString ("no process specified, create a target with a file, or specify the --pid or --name command option");
520 }
521 }
522
523 if (error.Success())
524 {
525 error = process->Attach (m_options.attach_info);
526
Jim Ingham4805a1c2010-09-15 01:34:14 +0000527 if (error.Success())
528 {
529 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
530 }
Jim Ingham7508e732010-08-09 23:31:02 +0000531 else
532 {
Greg Clayton527154d2011-11-15 03:53:30 +0000533 result.AppendErrorWithFormat ("attach failed: %s\n", error.AsCString());
Jim Ingham4805a1c2010-09-15 01:34:14 +0000534 result.SetStatus (eReturnStatusFailed);
535 return false;
Jim Ingham7508e732010-08-09 23:31:02 +0000536 }
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000537 // If we're synchronous, wait for the stopped event and report that.
538 // Otherwise just return.
539 // FIXME: in the async case it will now be possible to get to the command
540 // interpreter with a state eStateAttaching. Make sure we handle that correctly.
Jim Inghamee940e22011-09-15 01:08:57 +0000541 StateType state = process->WaitForProcessToStop (NULL);
Greg Clayton527154d2011-11-15 03:53:30 +0000542
Jim Inghamee940e22011-09-15 01:08:57 +0000543 result.SetDidChangeProcessState (true);
Greg Clayton444e35b2011-10-19 18:09:39 +0000544 result.AppendMessageWithFormat ("Process %llu %s\n", process->GetID(), StateAsCString (state));
Jim Inghamee940e22011-09-15 01:08:57 +0000545 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Jim Ingham7508e732010-08-09 23:31:02 +0000546 }
Jim Ingham7508e732010-08-09 23:31:02 +0000547 }
548 }
549
550 if (result.Succeeded())
551 {
552 // Okay, we're done. Last step is to warn if the executable module has changed:
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000553 char new_path[PATH_MAX];
Greg Clayton5beb99d2011-08-11 02:48:45 +0000554 ModuleSP new_exec_module_sp (target->GetExecutableModule());
Jim Ingham7508e732010-08-09 23:31:02 +0000555 if (!old_exec_module_sp)
556 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000557 // We might not have a module if we attached to a raw pid...
Greg Clayton5beb99d2011-08-11 02:48:45 +0000558 if (new_exec_module_sp)
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000559 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000560 new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000561 result.AppendMessageWithFormat("Executable module set to \"%s\".\n", new_path);
562 }
Jim Ingham7508e732010-08-09 23:31:02 +0000563 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000564 else if (old_exec_module_sp->GetFileSpec() != new_exec_module_sp->GetFileSpec())
Jim Ingham7508e732010-08-09 23:31:02 +0000565 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000566 char old_path[PATH_MAX];
Jim Ingham7508e732010-08-09 23:31:02 +0000567
Greg Clayton5beb99d2011-08-11 02:48:45 +0000568 old_exec_module_sp->GetFileSpec().GetPath (old_path, PATH_MAX);
569 new_exec_module_sp->GetFileSpec().GetPath (new_path, PATH_MAX);
Jim Ingham7508e732010-08-09 23:31:02 +0000570
571 result.AppendWarningWithFormat("Executable module changed from \"%s\" to \"%s\".\n",
572 old_path, new_path);
573 }
574
575 if (!old_arch_spec.IsValid())
576 {
Greg Clayton940b1032011-02-23 00:35:02 +0000577 result.AppendMessageWithFormat ("Architecture set to: %s.\n", target->GetArchitecture().GetArchitectureName());
Jim Ingham7508e732010-08-09 23:31:02 +0000578 }
579 else if (old_arch_spec != target->GetArchitecture())
580 {
581 result.AppendWarningWithFormat("Architecture changed from %s to %s.\n",
Greg Clayton940b1032011-02-23 00:35:02 +0000582 old_arch_spec.GetArchitectureName(), target->GetArchitecture().GetArchitectureName());
Jim Ingham7508e732010-08-09 23:31:02 +0000583 }
584 }
585 return result.Succeeded();
586 }
587
588 Options *
589 GetOptions ()
590 {
591 return &m_options;
592 }
593
Chris Lattner24943d22010-06-08 16:52:24 +0000594protected:
595
596 CommandOptions m_options;
597};
598
599
Greg Claytonb3448432011-03-24 21:19:54 +0000600OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000601CommandObjectProcessAttach::CommandOptions::g_option_table[] =
602{
Caroline Tice4d6675c2010-10-01 19:59:14 +0000603{ LLDB_OPT_SET_ALL, false, "plugin", 'P', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
604{ LLDB_OPT_SET_1, false, "pid", 'p', required_argument, NULL, 0, eArgTypePid, "The process ID of an existing process to attach to."},
605{ LLDB_OPT_SET_2, false, "name", 'n', required_argument, NULL, 0, eArgTypeProcessName, "The name of the process to attach to."},
606{ LLDB_OPT_SET_2, false, "waitfor",'w', no_argument, NULL, 0, eArgTypeNone, "Wait for the the process with <process-name> to launch."},
607{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000608};
609
610//-------------------------------------------------------------------------
611// CommandObjectProcessContinue
612//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000613#pragma mark CommandObjectProcessContinue
Chris Lattner24943d22010-06-08 16:52:24 +0000614
615class CommandObjectProcessContinue : public CommandObject
616{
617public:
618
Greg Clayton238c0a12010-09-18 01:14:36 +0000619 CommandObjectProcessContinue (CommandInterpreter &interpreter) :
620 CommandObject (interpreter,
621 "process continue",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000622 "Continue execution of all threads in the current process.",
Chris Lattner24943d22010-06-08 16:52:24 +0000623 "process continue",
624 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
625 {
626 }
627
628
629 ~CommandObjectProcessContinue ()
630 {
631 }
632
633 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000634 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000635 CommandReturnObject &result)
636 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000637 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton238c0a12010-09-18 01:14:36 +0000638 bool synchronous_execution = m_interpreter.GetSynchronous ();
Chris Lattner24943d22010-06-08 16:52:24 +0000639
640 if (process == NULL)
641 {
642 result.AppendError ("no process to continue");
643 result.SetStatus (eReturnStatusFailed);
644 return false;
645 }
646
647 StateType state = process->GetState();
648 if (state == eStateStopped)
649 {
650 if (command.GetArgumentCount() != 0)
651 {
652 result.AppendErrorWithFormat ("The '%s' command does not take any arguments.\n", m_cmd_name.c_str());
653 result.SetStatus (eReturnStatusFailed);
654 return false;
655 }
656
657 const uint32_t num_threads = process->GetThreadList().GetSize();
658
659 // Set the actions that the threads should each take when resuming
660 for (uint32_t idx=0; idx<num_threads; ++idx)
661 {
662 process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState (eStateRunning);
663 }
664
665 Error error(process->Resume());
666 if (error.Success())
667 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000668 result.AppendMessageWithFormat ("Process %llu resuming\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000669 if (synchronous_execution)
670 {
Greg Claytonbef15832010-07-14 00:18:15 +0000671 state = process->WaitForProcessToStop (NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000672
673 result.SetDidChangeProcessState (true);
Greg Clayton444e35b2011-10-19 18:09:39 +0000674 result.AppendMessageWithFormat ("Process %llu %s\n", process->GetID(), StateAsCString (state));
Chris Lattner24943d22010-06-08 16:52:24 +0000675 result.SetStatus (eReturnStatusSuccessFinishNoResult);
676 }
677 else
678 {
679 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
680 }
681 }
682 else
683 {
684 result.AppendErrorWithFormat("Failed to resume process: %s.\n", error.AsCString());
685 result.SetStatus (eReturnStatusFailed);
686 }
687 }
688 else
689 {
690 result.AppendErrorWithFormat ("Process cannot be continued from its current state (%s).\n",
691 StateAsCString(state));
692 result.SetStatus (eReturnStatusFailed);
693 }
694 return result.Succeeded();
695 }
696};
697
698//-------------------------------------------------------------------------
699// CommandObjectProcessDetach
700//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000701#pragma mark CommandObjectProcessDetach
Chris Lattner24943d22010-06-08 16:52:24 +0000702
703class CommandObjectProcessDetach : public CommandObject
704{
705public:
706
Greg Clayton238c0a12010-09-18 01:14:36 +0000707 CommandObjectProcessDetach (CommandInterpreter &interpreter) :
708 CommandObject (interpreter,
709 "process detach",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000710 "Detach from the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +0000711 "process detach",
712 eFlagProcessMustBeLaunched)
713 {
714 }
715
716 ~CommandObjectProcessDetach ()
717 {
718 }
719
720 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000721 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000722 CommandReturnObject &result)
723 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000724 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000725 if (process == NULL)
726 {
727 result.AppendError ("must have a valid process in order to detach");
728 result.SetStatus (eReturnStatusFailed);
729 return false;
730 }
731
Greg Clayton444e35b2011-10-19 18:09:39 +0000732 result.AppendMessageWithFormat ("Detaching from process %llu\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000733 Error error (process->Detach());
734 if (error.Success())
735 {
736 result.SetStatus (eReturnStatusSuccessFinishResult);
737 }
738 else
739 {
740 result.AppendErrorWithFormat ("Detach failed: %s\n", error.AsCString());
741 result.SetStatus (eReturnStatusFailed);
742 return false;
743 }
744 return result.Succeeded();
745 }
746};
747
748//-------------------------------------------------------------------------
Greg Claytone71e2582011-02-04 01:58:07 +0000749// CommandObjectProcessConnect
750//-------------------------------------------------------------------------
751#pragma mark CommandObjectProcessConnect
752
753class CommandObjectProcessConnect : public CommandObject
754{
755public:
756
757 class CommandOptions : public Options
758 {
759 public:
760
Greg Claytonf15996e2011-04-07 22:46:35 +0000761 CommandOptions (CommandInterpreter &interpreter) :
762 Options(interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000763 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000764 // Keep default values of all options in one place: OptionParsingStarting ()
765 OptionParsingStarting ();
Greg Claytone71e2582011-02-04 01:58:07 +0000766 }
767
768 ~CommandOptions ()
769 {
770 }
771
772 Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000773 SetOptionValue (uint32_t option_idx, const char *option_arg)
Greg Claytone71e2582011-02-04 01:58:07 +0000774 {
775 Error error;
776 char short_option = (char) m_getopt_table[option_idx].val;
777
778 switch (short_option)
779 {
780 case 'p':
781 plugin_name.assign (option_arg);
782 break;
783
784 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000785 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Greg Claytone71e2582011-02-04 01:58:07 +0000786 break;
787 }
788 return error;
789 }
790
791 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000792 OptionParsingStarting ()
Greg Claytone71e2582011-02-04 01:58:07 +0000793 {
Greg Claytone71e2582011-02-04 01:58:07 +0000794 plugin_name.clear();
795 }
796
Greg Claytonb3448432011-03-24 21:19:54 +0000797 const OptionDefinition*
Greg Claytone71e2582011-02-04 01:58:07 +0000798 GetDefinitions ()
799 {
800 return g_option_table;
801 }
802
803 // Options table: Required for subclasses of Options.
804
Greg Claytonb3448432011-03-24 21:19:54 +0000805 static OptionDefinition g_option_table[];
Greg Claytone71e2582011-02-04 01:58:07 +0000806
807 // Instance variables to hold the values for command options.
808
809 std::string plugin_name;
810 };
811
812 CommandObjectProcessConnect (CommandInterpreter &interpreter) :
Greg Claytonf15996e2011-04-07 22:46:35 +0000813 CommandObject (interpreter,
814 "process connect",
815 "Connect to a remote debug service.",
816 "process connect <remote-url>",
817 0),
818 m_options (interpreter)
Greg Claytone71e2582011-02-04 01:58:07 +0000819 {
820 }
821
822 ~CommandObjectProcessConnect ()
823 {
824 }
825
826
827 bool
828 Execute (Args& command,
829 CommandReturnObject &result)
830 {
831
832 TargetSP target_sp (m_interpreter.GetDebugger().GetSelectedTarget());
833 Error error;
Greg Clayton567e7f32011-09-22 04:58:26 +0000834 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Claytone71e2582011-02-04 01:58:07 +0000835 if (process)
836 {
837 if (process->IsAlive())
838 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000839 result.AppendErrorWithFormat ("Process %llu is currently being debugged, kill the process before connecting.\n",
Greg Claytone71e2582011-02-04 01:58:07 +0000840 process->GetID());
841 result.SetStatus (eReturnStatusFailed);
842 return false;
843 }
844 }
845
846 if (!target_sp)
847 {
848 // If there isn't a current target create one.
849 FileSpec emptyFileSpec;
Greg Claytone71e2582011-02-04 01:58:07 +0000850
851 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
852 emptyFileSpec,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000853 NULL,
Greg Claytone71e2582011-02-04 01:58:07 +0000854 false,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000855 NULL, // No platform options
Greg Claytone71e2582011-02-04 01:58:07 +0000856 target_sp);
857 if (!target_sp || error.Fail())
858 {
859 result.AppendError(error.AsCString("Error creating target"));
860 result.SetStatus (eReturnStatusFailed);
861 return false;
862 }
863 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target_sp.get());
864 }
865
866 if (command.GetArgumentCount() == 1)
867 {
868 const char *plugin_name = NULL;
869 if (!m_options.plugin_name.empty())
870 plugin_name = m_options.plugin_name.c_str();
871
872 const char *remote_url = command.GetArgumentAtIndex(0);
Greg Clayton46c9a352012-02-09 06:16:32 +0000873 process = target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name, NULL).get();
Greg Claytone71e2582011-02-04 01:58:07 +0000874
875 if (process)
876 {
877 error = process->ConnectRemote (remote_url);
878
879 if (error.Fail())
880 {
881 result.AppendError(error.AsCString("Remote connect failed"));
882 result.SetStatus (eReturnStatusFailed);
Greg Clayton0cbb93b2012-03-31 00:10:30 +0000883 target_sp->DeleteCurrentProcess();
Greg Claytone71e2582011-02-04 01:58:07 +0000884 return false;
885 }
886 }
887 else
888 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000889 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",
890 m_cmd_name.c_str());
Greg Claytone71e2582011-02-04 01:58:07 +0000891 result.SetStatus (eReturnStatusFailed);
892 }
893 }
894 else
895 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000896 result.AppendErrorWithFormat ("'%s' takes exactly one argument:\nUsage: %s\n",
Greg Claytone71e2582011-02-04 01:58:07 +0000897 m_cmd_name.c_str(),
898 m_cmd_syntax.c_str());
899 result.SetStatus (eReturnStatusFailed);
900 }
901 return result.Succeeded();
902 }
903
904 Options *
905 GetOptions ()
906 {
907 return &m_options;
908 }
909
910protected:
911
912 CommandOptions m_options;
913};
914
915
Greg Claytonb3448432011-03-24 21:19:54 +0000916OptionDefinition
Greg Claytone71e2582011-02-04 01:58:07 +0000917CommandObjectProcessConnect::CommandOptions::g_option_table[] =
918{
919 { LLDB_OPT_SET_ALL, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
920 { 0, false, NULL, 0 , 0, NULL, 0, eArgTypeNone, NULL }
921};
922
923//-------------------------------------------------------------------------
Greg Clayton0baa3942010-11-04 01:54:29 +0000924// CommandObjectProcessLoad
925//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000926#pragma mark CommandObjectProcessLoad
Greg Clayton0baa3942010-11-04 01:54:29 +0000927
928class CommandObjectProcessLoad : public CommandObject
929{
930public:
931
932 CommandObjectProcessLoad (CommandInterpreter &interpreter) :
933 CommandObject (interpreter,
934 "process load",
935 "Load a shared library into the current process.",
936 "process load <filename> [<filename> ...]",
937 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
938 {
939 }
940
941 ~CommandObjectProcessLoad ()
942 {
943 }
944
945 bool
946 Execute (Args& command,
947 CommandReturnObject &result)
948 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000949 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton0baa3942010-11-04 01:54:29 +0000950 if (process == NULL)
951 {
952 result.AppendError ("must have a valid process in order to load a shared library");
953 result.SetStatus (eReturnStatusFailed);
954 return false;
955 }
956
957 const uint32_t argc = command.GetArgumentCount();
958
959 for (uint32_t i=0; i<argc; ++i)
960 {
961 Error error;
962 const char *image_path = command.GetArgumentAtIndex(i);
963 FileSpec image_spec (image_path, false);
Greg Claytonf2bf8702011-08-11 16:25:18 +0000964 process->GetTarget().GetPlatform()->ResolveRemotePath(image_spec, image_spec);
Greg Clayton0baa3942010-11-04 01:54:29 +0000965 uint32_t image_token = process->LoadImage(image_spec, error);
966 if (image_token != LLDB_INVALID_IMAGE_TOKEN)
967 {
968 result.AppendMessageWithFormat ("Loading \"%s\"...ok\nImage %u loaded.\n", image_path, image_token);
969 result.SetStatus (eReturnStatusSuccessFinishResult);
970 }
971 else
972 {
973 result.AppendErrorWithFormat ("failed to load '%s': %s", image_path, error.AsCString());
974 result.SetStatus (eReturnStatusFailed);
975 }
976 }
977 return result.Succeeded();
978 }
979};
980
981
982//-------------------------------------------------------------------------
983// CommandObjectProcessUnload
984//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000985#pragma mark CommandObjectProcessUnload
Greg Clayton0baa3942010-11-04 01:54:29 +0000986
987class CommandObjectProcessUnload : public CommandObject
988{
989public:
990
991 CommandObjectProcessUnload (CommandInterpreter &interpreter) :
992 CommandObject (interpreter,
993 "process unload",
994 "Unload a shared library from the current process using the index returned by a previous call to \"process load\".",
995 "process unload <index>",
996 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
997 {
998 }
999
1000 ~CommandObjectProcessUnload ()
1001 {
1002 }
1003
1004 bool
1005 Execute (Args& command,
1006 CommandReturnObject &result)
1007 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001008 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton0baa3942010-11-04 01:54:29 +00001009 if (process == NULL)
1010 {
1011 result.AppendError ("must have a valid process in order to load a shared library");
1012 result.SetStatus (eReturnStatusFailed);
1013 return false;
1014 }
1015
1016 const uint32_t argc = command.GetArgumentCount();
1017
1018 for (uint32_t i=0; i<argc; ++i)
1019 {
1020 const char *image_token_cstr = command.GetArgumentAtIndex(i);
1021 uint32_t image_token = Args::StringToUInt32(image_token_cstr, LLDB_INVALID_IMAGE_TOKEN, 0);
1022 if (image_token == LLDB_INVALID_IMAGE_TOKEN)
1023 {
1024 result.AppendErrorWithFormat ("invalid image index argument '%s'", image_token_cstr);
1025 result.SetStatus (eReturnStatusFailed);
1026 break;
1027 }
1028 else
1029 {
1030 Error error (process->UnloadImage(image_token));
1031 if (error.Success())
1032 {
1033 result.AppendMessageWithFormat ("Unloading shared library with index %u...ok\n", image_token);
1034 result.SetStatus (eReturnStatusSuccessFinishResult);
1035 }
1036 else
1037 {
1038 result.AppendErrorWithFormat ("failed to unload image: %s", error.AsCString());
1039 result.SetStatus (eReturnStatusFailed);
1040 break;
1041 }
1042 }
1043 }
1044 return result.Succeeded();
1045 }
1046};
1047
1048//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001049// CommandObjectProcessSignal
1050//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001051#pragma mark CommandObjectProcessSignal
Chris Lattner24943d22010-06-08 16:52:24 +00001052
1053class CommandObjectProcessSignal : public CommandObject
1054{
1055public:
1056
Greg Clayton238c0a12010-09-18 01:14:36 +00001057 CommandObjectProcessSignal (CommandInterpreter &interpreter) :
1058 CommandObject (interpreter,
1059 "process signal",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001060 "Send a UNIX signal to the current process being debugged.",
Caroline Tice43b014a2010-10-04 22:28:36 +00001061 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001062 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001063 CommandArgumentEntry arg;
1064 CommandArgumentData signal_arg;
1065
1066 // Define the first (and only) variant of this arg.
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001067 signal_arg.arg_type = eArgTypeUnixSignal;
Caroline Tice43b014a2010-10-04 22:28:36 +00001068 signal_arg.arg_repetition = eArgRepeatPlain;
1069
1070 // There is only one variant this argument could be; put it into the argument entry.
1071 arg.push_back (signal_arg);
1072
1073 // Push the data for the first argument into the m_arguments vector.
1074 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001075 }
1076
1077 ~CommandObjectProcessSignal ()
1078 {
1079 }
1080
1081 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001082 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001083 CommandReturnObject &result)
1084 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001085 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001086 if (process == NULL)
1087 {
1088 result.AppendError ("no process to signal");
1089 result.SetStatus (eReturnStatusFailed);
1090 return false;
1091 }
1092
1093 if (command.GetArgumentCount() == 1)
1094 {
Greg Clayton8f6be2a2010-10-09 01:40:57 +00001095 int signo = LLDB_INVALID_SIGNAL_NUMBER;
1096
1097 const char *signal_name = command.GetArgumentAtIndex(0);
1098 if (::isxdigit (signal_name[0]))
1099 signo = Args::StringToSInt32(signal_name, LLDB_INVALID_SIGNAL_NUMBER, 0);
1100 else
1101 signo = process->GetUnixSignals().GetSignalNumberFromName (signal_name);
1102
1103 if (signo == LLDB_INVALID_SIGNAL_NUMBER)
Chris Lattner24943d22010-06-08 16:52:24 +00001104 {
1105 result.AppendErrorWithFormat ("Invalid signal argument '%s'.\n", command.GetArgumentAtIndex(0));
1106 result.SetStatus (eReturnStatusFailed);
1107 }
1108 else
1109 {
1110 Error error (process->Signal (signo));
1111 if (error.Success())
1112 {
1113 result.SetStatus (eReturnStatusSuccessFinishResult);
1114 }
1115 else
1116 {
1117 result.AppendErrorWithFormat ("Failed to send signal %i: %s\n", signo, error.AsCString());
1118 result.SetStatus (eReturnStatusFailed);
1119 }
1120 }
1121 }
1122 else
1123 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001124 result.AppendErrorWithFormat("'%s' takes exactly one signal number argument:\nUsage: %s\n", m_cmd_name.c_str(),
Chris Lattner24943d22010-06-08 16:52:24 +00001125 m_cmd_syntax.c_str());
1126 result.SetStatus (eReturnStatusFailed);
1127 }
1128 return result.Succeeded();
1129 }
1130};
1131
1132
1133//-------------------------------------------------------------------------
1134// CommandObjectProcessInterrupt
1135//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001136#pragma mark CommandObjectProcessInterrupt
Chris Lattner24943d22010-06-08 16:52:24 +00001137
1138class CommandObjectProcessInterrupt : public CommandObject
1139{
1140public:
1141
1142
Greg Clayton238c0a12010-09-18 01:14:36 +00001143 CommandObjectProcessInterrupt (CommandInterpreter &interpreter) :
1144 CommandObject (interpreter,
1145 "process interrupt",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001146 "Interrupt the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +00001147 "process interrupt",
1148 eFlagProcessMustBeLaunched)
1149 {
1150 }
1151
1152 ~CommandObjectProcessInterrupt ()
1153 {
1154 }
1155
1156 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001157 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001158 CommandReturnObject &result)
1159 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001160 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001161 if (process == NULL)
1162 {
1163 result.AppendError ("no process to halt");
1164 result.SetStatus (eReturnStatusFailed);
1165 return false;
1166 }
1167
1168 if (command.GetArgumentCount() == 0)
1169 {
1170 Error error(process->Halt ());
1171 if (error.Success())
1172 {
1173 result.SetStatus (eReturnStatusSuccessFinishResult);
1174
1175 // Maybe we should add a "SuspendThreadPlans so we
1176 // can halt, and keep in place all the current thread plans.
1177 process->GetThreadList().DiscardThreadPlans();
1178 }
1179 else
1180 {
1181 result.AppendErrorWithFormat ("Failed to halt process: %s\n", error.AsCString());
1182 result.SetStatus (eReturnStatusFailed);
1183 }
1184 }
1185 else
1186 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001187 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner24943d22010-06-08 16:52:24 +00001188 m_cmd_name.c_str(),
1189 m_cmd_syntax.c_str());
1190 result.SetStatus (eReturnStatusFailed);
1191 }
1192 return result.Succeeded();
1193 }
1194};
1195
1196//-------------------------------------------------------------------------
1197// CommandObjectProcessKill
1198//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001199#pragma mark CommandObjectProcessKill
Chris Lattner24943d22010-06-08 16:52:24 +00001200
1201class CommandObjectProcessKill : public CommandObject
1202{
1203public:
1204
Greg Clayton238c0a12010-09-18 01:14:36 +00001205 CommandObjectProcessKill (CommandInterpreter &interpreter) :
1206 CommandObject (interpreter,
1207 "process kill",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001208 "Terminate the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +00001209 "process kill",
1210 eFlagProcessMustBeLaunched)
1211 {
1212 }
1213
1214 ~CommandObjectProcessKill ()
1215 {
1216 }
1217
1218 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001219 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001220 CommandReturnObject &result)
1221 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001222 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001223 if (process == NULL)
1224 {
1225 result.AppendError ("no process to kill");
1226 result.SetStatus (eReturnStatusFailed);
1227 return false;
1228 }
1229
1230 if (command.GetArgumentCount() == 0)
1231 {
1232 Error error (process->Destroy());
1233 if (error.Success())
1234 {
1235 result.SetStatus (eReturnStatusSuccessFinishResult);
1236 }
1237 else
1238 {
1239 result.AppendErrorWithFormat ("Failed to kill process: %s\n", error.AsCString());
1240 result.SetStatus (eReturnStatusFailed);
1241 }
1242 }
1243 else
1244 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001245 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
Chris Lattner24943d22010-06-08 16:52:24 +00001246 m_cmd_name.c_str(),
1247 m_cmd_syntax.c_str());
1248 result.SetStatus (eReturnStatusFailed);
1249 }
1250 return result.Succeeded();
1251 }
1252};
1253
1254//-------------------------------------------------------------------------
Jim Ingham41313fc2010-06-18 01:23:09 +00001255// CommandObjectProcessStatus
1256//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001257#pragma mark CommandObjectProcessStatus
1258
Jim Ingham41313fc2010-06-18 01:23:09 +00001259class CommandObjectProcessStatus : public CommandObject
1260{
1261public:
Greg Clayton238c0a12010-09-18 01:14:36 +00001262 CommandObjectProcessStatus (CommandInterpreter &interpreter) :
1263 CommandObject (interpreter,
1264 "process status",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001265 "Show the current status and location of executing process.",
1266 "process status",
Jim Ingham41313fc2010-06-18 01:23:09 +00001267 0)
1268 {
1269 }
1270
1271 ~CommandObjectProcessStatus()
1272 {
1273 }
1274
1275
1276 bool
1277 Execute
1278 (
1279 Args& command,
Jim Ingham41313fc2010-06-18 01:23:09 +00001280 CommandReturnObject &result
1281 )
1282 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001283 Stream &strm = result.GetOutputStream();
Jim Ingham41313fc2010-06-18 01:23:09 +00001284 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Claytonb72d0f02011-04-12 05:54:46 +00001285 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
Greg Clayton567e7f32011-09-22 04:58:26 +00001286 Process *process = exe_ctx.GetProcessPtr();
1287 if (process)
Jim Ingham41313fc2010-06-18 01:23:09 +00001288 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001289 const bool only_threads_with_stop_reason = true;
1290 const uint32_t start_frame = 0;
1291 const uint32_t num_frames = 1;
1292 const uint32_t num_frames_with_source = 1;
Greg Clayton567e7f32011-09-22 04:58:26 +00001293 process->GetStatus(strm);
1294 process->GetThreadStatus (strm,
1295 only_threads_with_stop_reason,
1296 start_frame,
1297 num_frames,
1298 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001299
Jim Ingham41313fc2010-06-18 01:23:09 +00001300 }
1301 else
1302 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001303 result.AppendError ("No process.");
Jim Ingham41313fc2010-06-18 01:23:09 +00001304 result.SetStatus (eReturnStatusFailed);
1305 }
1306 return result.Succeeded();
1307 }
1308};
1309
1310//-------------------------------------------------------------------------
Caroline Tice23d6f272010-10-13 20:44:39 +00001311// CommandObjectProcessHandle
1312//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001313#pragma mark CommandObjectProcessHandle
Caroline Tice23d6f272010-10-13 20:44:39 +00001314
1315class CommandObjectProcessHandle : public CommandObject
1316{
1317public:
1318
1319 class CommandOptions : public Options
1320 {
1321 public:
1322
Greg Claytonf15996e2011-04-07 22:46:35 +00001323 CommandOptions (CommandInterpreter &interpreter) :
1324 Options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001325 {
Greg Clayton143fcc32011-04-13 00:18:08 +00001326 OptionParsingStarting ();
Caroline Tice23d6f272010-10-13 20:44:39 +00001327 }
1328
1329 ~CommandOptions ()
1330 {
1331 }
1332
1333 Error
Greg Clayton143fcc32011-04-13 00:18:08 +00001334 SetOptionValue (uint32_t option_idx, const char *option_arg)
Caroline Tice23d6f272010-10-13 20:44:39 +00001335 {
1336 Error error;
1337 char short_option = (char) m_getopt_table[option_idx].val;
1338
1339 switch (short_option)
1340 {
1341 case 's':
1342 stop = option_arg;
1343 break;
1344 case 'n':
1345 notify = option_arg;
1346 break;
1347 case 'p':
1348 pass = option_arg;
1349 break;
1350 default:
Greg Clayton9c236732011-10-26 00:56:27 +00001351 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Caroline Tice23d6f272010-10-13 20:44:39 +00001352 break;
1353 }
1354 return error;
1355 }
1356
1357 void
Greg Clayton143fcc32011-04-13 00:18:08 +00001358 OptionParsingStarting ()
Caroline Tice23d6f272010-10-13 20:44:39 +00001359 {
Caroline Tice23d6f272010-10-13 20:44:39 +00001360 stop.clear();
1361 notify.clear();
1362 pass.clear();
1363 }
1364
Greg Claytonb3448432011-03-24 21:19:54 +00001365 const OptionDefinition*
Caroline Tice23d6f272010-10-13 20:44:39 +00001366 GetDefinitions ()
1367 {
1368 return g_option_table;
1369 }
1370
1371 // Options table: Required for subclasses of Options.
1372
Greg Claytonb3448432011-03-24 21:19:54 +00001373 static OptionDefinition g_option_table[];
Caroline Tice23d6f272010-10-13 20:44:39 +00001374
1375 // Instance variables to hold the values for command options.
1376
1377 std::string stop;
1378 std::string notify;
1379 std::string pass;
1380 };
1381
1382
1383 CommandObjectProcessHandle (CommandInterpreter &interpreter) :
1384 CommandObject (interpreter,
1385 "process handle",
Caroline Ticee7471982010-10-14 21:31:13 +00001386 "Show or update what the process and debugger should do with various signals received from the OS.",
Greg Claytonf15996e2011-04-07 22:46:35 +00001387 NULL),
1388 m_options (interpreter)
Caroline Tice23d6f272010-10-13 20:44:39 +00001389 {
Caroline Ticee7471982010-10-14 21:31:13 +00001390 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 +00001391 CommandArgumentEntry arg;
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001392 CommandArgumentData signal_arg;
Caroline Tice23d6f272010-10-13 20:44:39 +00001393
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001394 signal_arg.arg_type = eArgTypeUnixSignal;
1395 signal_arg.arg_repetition = eArgRepeatStar;
Caroline Tice23d6f272010-10-13 20:44:39 +00001396
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001397 arg.push_back (signal_arg);
Caroline Tice23d6f272010-10-13 20:44:39 +00001398
1399 m_arguments.push_back (arg);
1400 }
1401
1402 ~CommandObjectProcessHandle ()
1403 {
1404 }
1405
1406 Options *
1407 GetOptions ()
1408 {
1409 return &m_options;
1410 }
1411
1412 bool
Caroline Ticee7471982010-10-14 21:31:13 +00001413 VerifyCommandOptionValue (const std::string &option, int &real_value)
Caroline Tice23d6f272010-10-13 20:44:39 +00001414 {
1415 bool okay = true;
1416
Caroline Ticee7471982010-10-14 21:31:13 +00001417 bool success = false;
1418 bool tmp_value = Args::StringToBoolean (option.c_str(), false, &success);
1419
1420 if (success && tmp_value)
1421 real_value = 1;
1422 else if (success && !tmp_value)
1423 real_value = 0;
Caroline Tice23d6f272010-10-13 20:44:39 +00001424 else
1425 {
1426 // If the value isn't 'true' or 'false', it had better be 0 or 1.
Caroline Ticee7471982010-10-14 21:31:13 +00001427 real_value = Args::StringToUInt32 (option.c_str(), 3);
1428 if (real_value != 0 && real_value != 1)
Caroline Tice23d6f272010-10-13 20:44:39 +00001429 okay = false;
1430 }
1431
1432 return okay;
1433 }
1434
Caroline Ticee7471982010-10-14 21:31:13 +00001435 void
1436 PrintSignalHeader (Stream &str)
1437 {
1438 str.Printf ("NAME PASS STOP NOTIFY\n");
1439 str.Printf ("========== ===== ===== ======\n");
1440 }
1441
1442 void
1443 PrintSignal (Stream &str, int32_t signo, const char *sig_name, UnixSignals &signals)
1444 {
1445 bool stop;
1446 bool suppress;
1447 bool notify;
1448
1449 str.Printf ("%-10s ", sig_name);
1450 if (signals.GetSignalInfo (signo, suppress, stop, notify))
1451 {
1452 bool pass = !suppress;
1453 str.Printf ("%s %s %s",
1454 (pass ? "true " : "false"),
1455 (stop ? "true " : "false"),
1456 (notify ? "true " : "false"));
1457 }
1458 str.Printf ("\n");
1459 }
1460
1461 void
1462 PrintSignalInformation (Stream &str, Args &signal_args, int num_valid_signals, UnixSignals &signals)
1463 {
1464 PrintSignalHeader (str);
1465
1466 if (num_valid_signals > 0)
1467 {
1468 size_t num_args = signal_args.GetArgumentCount();
1469 for (size_t i = 0; i < num_args; ++i)
1470 {
1471 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1472 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
1473 PrintSignal (str, signo, signal_args.GetArgumentAtIndex (i), signals);
1474 }
1475 }
1476 else // Print info for ALL signals
1477 {
1478 int32_t signo = signals.GetFirstSignalNumber();
1479 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1480 {
1481 PrintSignal (str, signo, signals.GetSignalAsCString (signo), signals);
1482 signo = signals.GetNextSignalNumber (signo);
1483 }
1484 }
1485 }
1486
Caroline Tice23d6f272010-10-13 20:44:39 +00001487 bool
1488 Execute (Args &signal_args, CommandReturnObject &result)
1489 {
1490 TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
1491
1492 if (!target_sp)
1493 {
1494 result.AppendError ("No current target;"
1495 " cannot handle signals until you have a valid target and process.\n");
1496 result.SetStatus (eReturnStatusFailed);
1497 return false;
1498 }
1499
1500 ProcessSP process_sp = target_sp->GetProcessSP();
1501
1502 if (!process_sp)
1503 {
1504 result.AppendError ("No current process; cannot handle signals until you have a valid process.\n");
1505 result.SetStatus (eReturnStatusFailed);
1506 return false;
1507 }
1508
Caroline Tice23d6f272010-10-13 20:44:39 +00001509 int stop_action = -1; // -1 means leave the current setting alone
Caroline Ticee7471982010-10-14 21:31:13 +00001510 int pass_action = -1; // -1 means leave the current setting alone
Caroline Tice23d6f272010-10-13 20:44:39 +00001511 int notify_action = -1; // -1 means leave the current setting alone
1512
1513 if (! m_options.stop.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001514 && ! VerifyCommandOptionValue (m_options.stop, stop_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001515 {
1516 result.AppendError ("Invalid argument for command option --stop; must be true or false.\n");
1517 result.SetStatus (eReturnStatusFailed);
1518 return false;
1519 }
1520
1521 if (! m_options.notify.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001522 && ! VerifyCommandOptionValue (m_options.notify, notify_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001523 {
1524 result.AppendError ("Invalid argument for command option --notify; must be true or false.\n");
1525 result.SetStatus (eReturnStatusFailed);
1526 return false;
1527 }
1528
1529 if (! m_options.pass.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001530 && ! VerifyCommandOptionValue (m_options.pass, pass_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001531 {
1532 result.AppendError ("Invalid argument for command option --pass; must be true or false.\n");
1533 result.SetStatus (eReturnStatusFailed);
1534 return false;
1535 }
1536
1537 size_t num_args = signal_args.GetArgumentCount();
1538 UnixSignals &signals = process_sp->GetUnixSignals();
1539 int num_signals_set = 0;
1540
Caroline Ticee7471982010-10-14 21:31:13 +00001541 if (num_args > 0)
Caroline Tice23d6f272010-10-13 20:44:39 +00001542 {
Caroline Ticee7471982010-10-14 21:31:13 +00001543 for (size_t i = 0; i < num_args; ++i)
Caroline Tice23d6f272010-10-13 20:44:39 +00001544 {
Caroline Ticee7471982010-10-14 21:31:13 +00001545 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1546 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
Caroline Tice23d6f272010-10-13 20:44:39 +00001547 {
Caroline Ticee7471982010-10-14 21:31:13 +00001548 // Casting the actions as bools here should be okay, because VerifyCommandOptionValue guarantees
1549 // the value is either 0 or 1.
1550 if (stop_action != -1)
1551 signals.SetShouldStop (signo, (bool) stop_action);
1552 if (pass_action != -1)
1553 {
1554 bool suppress = ! ((bool) pass_action);
1555 signals.SetShouldSuppress (signo, suppress);
1556 }
1557 if (notify_action != -1)
1558 signals.SetShouldNotify (signo, (bool) notify_action);
1559 ++num_signals_set;
Caroline Tice23d6f272010-10-13 20:44:39 +00001560 }
Caroline Ticee7471982010-10-14 21:31:13 +00001561 else
1562 {
1563 result.AppendErrorWithFormat ("Invalid signal name '%s'\n", signal_args.GetArgumentAtIndex (i));
1564 }
Caroline Tice23d6f272010-10-13 20:44:39 +00001565 }
1566 }
Caroline Ticee7471982010-10-14 21:31:13 +00001567 else
1568 {
1569 // No signal specified, if any command options were specified, update ALL signals.
1570 if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1))
1571 {
1572 if (m_interpreter.Confirm ("Do you really want to update all the signals?", false))
1573 {
1574 int32_t signo = signals.GetFirstSignalNumber();
1575 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1576 {
1577 if (notify_action != -1)
1578 signals.SetShouldNotify (signo, (bool) notify_action);
1579 if (stop_action != -1)
1580 signals.SetShouldStop (signo, (bool) stop_action);
1581 if (pass_action != -1)
1582 {
1583 bool suppress = ! ((bool) pass_action);
1584 signals.SetShouldSuppress (signo, suppress);
1585 }
1586 signo = signals.GetNextSignalNumber (signo);
1587 }
1588 }
1589 }
1590 }
1591
1592 PrintSignalInformation (result.GetOutputStream(), signal_args, num_signals_set, signals);
Caroline Tice23d6f272010-10-13 20:44:39 +00001593
1594 if (num_signals_set > 0)
1595 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1596 else
1597 result.SetStatus (eReturnStatusFailed);
1598
1599 return result.Succeeded();
1600 }
1601
1602protected:
1603
1604 CommandOptions m_options;
1605};
1606
Greg Claytonb3448432011-03-24 21:19:54 +00001607OptionDefinition
Caroline Tice23d6f272010-10-13 20:44:39 +00001608CommandObjectProcessHandle::CommandOptions::g_option_table[] =
1609{
1610{ 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." },
1611{ 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." },
1612{ LLDB_OPT_SET_1, false, "pass", 'p', required_argument, NULL, 0, eArgTypeBoolean, "Whether or not the signal should be passed to the process." },
1613{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1614};
1615
1616//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001617// CommandObjectMultiwordProcess
1618//-------------------------------------------------------------------------
1619
Greg Clayton63094e02010-06-23 01:19:29 +00001620CommandObjectMultiwordProcess::CommandObjectMultiwordProcess (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00001621 CommandObjectMultiword (interpreter,
1622 "process",
1623 "A set of commands for operating on a process.",
1624 "process <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00001625{
Greg Claytona9eb8272011-07-02 21:07:54 +00001626 LoadSubCommand ("attach", CommandObjectSP (new CommandObjectProcessAttach (interpreter)));
1627 LoadSubCommand ("launch", CommandObjectSP (new CommandObjectProcessLaunch (interpreter)));
1628 LoadSubCommand ("continue", CommandObjectSP (new CommandObjectProcessContinue (interpreter)));
1629 LoadSubCommand ("connect", CommandObjectSP (new CommandObjectProcessConnect (interpreter)));
1630 LoadSubCommand ("detach", CommandObjectSP (new CommandObjectProcessDetach (interpreter)));
1631 LoadSubCommand ("load", CommandObjectSP (new CommandObjectProcessLoad (interpreter)));
1632 LoadSubCommand ("unload", CommandObjectSP (new CommandObjectProcessUnload (interpreter)));
1633 LoadSubCommand ("signal", CommandObjectSP (new CommandObjectProcessSignal (interpreter)));
1634 LoadSubCommand ("handle", CommandObjectSP (new CommandObjectProcessHandle (interpreter)));
1635 LoadSubCommand ("status", CommandObjectSP (new CommandObjectProcessStatus (interpreter)));
Greg Clayton238c0a12010-09-18 01:14:36 +00001636 LoadSubCommand ("interrupt", CommandObjectSP (new CommandObjectProcessInterrupt (interpreter)));
Greg Claytona9eb8272011-07-02 21:07:54 +00001637 LoadSubCommand ("kill", CommandObjectSP (new CommandObjectProcessKill (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00001638}
1639
1640CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess ()
1641{
1642}
1643