blob: c07de3ee6aee1eb12208fbf077dc39ac3dd2852c [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"
19#include "lldb/Interpreter/CommandInterpreter.h"
20#include "lldb/Interpreter/CommandReturnObject.h"
Jim Ingham41313fc2010-06-18 01:23:09 +000021#include "./CommandObjectThread.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "lldb/Target/Process.h"
23#include "lldb/Target/Target.h"
24#include "lldb/Target/Thread.h"
25
26using namespace lldb;
27using namespace lldb_private;
28
29//-------------------------------------------------------------------------
30// CommandObjectProcessLaunch
31//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +000032#pragma mark CommandObjectProjectLaunch
Chris Lattner24943d22010-06-08 16:52:24 +000033class CommandObjectProcessLaunch : public CommandObject
34{
35public:
36
37 class CommandOptions : public Options
38 {
39 public:
40
41 CommandOptions () :
42 Options()
43 {
44 // Keep default values of all options in one place: ResetOptionValues ()
45 ResetOptionValues ();
46 }
47
48 ~CommandOptions ()
49 {
50 }
51
52 Error
53 SetOptionValue (int option_idx, const char *option_arg)
54 {
55 Error error;
56 char short_option = (char) m_getopt_table[option_idx].val;
57
58 switch (short_option)
59 {
60 case 's': stop_at_entry = true; break;
61 case 'e': stderr_path = option_arg; break;
62 case 'i': stdin_path = option_arg; break;
63 case 'o': stdout_path = option_arg; break;
64 case 'p': plugin_name = option_arg; break;
Caroline Ticebd666012010-12-03 18:46:09 +000065 case 'n': no_stdio = true; break;
Greg Claytonbb0c91f2010-10-19 23:16:00 +000066 case 't':
67 if (option_arg && option_arg[0])
68 tty_name.assign (option_arg);
69 in_new_tty = true;
70 break;
Chris Lattner24943d22010-06-08 16:52:24 +000071 default:
72 error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
73 break;
74
75 }
76 return error;
77 }
78
79 void
80 ResetOptionValues ()
81 {
82 Options::ResetOptionValues();
83 stop_at_entry = false;
Greg Claytonc1d37752010-10-18 01:45:30 +000084 in_new_tty = false;
Greg Claytonbb0c91f2010-10-19 23:16:00 +000085 tty_name.clear();
Chris Lattner24943d22010-06-08 16:52:24 +000086 stdin_path.clear();
87 stdout_path.clear();
88 stderr_path.clear();
89 plugin_name.clear();
Caroline Ticebd666012010-12-03 18:46:09 +000090 no_stdio = false;
Chris Lattner24943d22010-06-08 16:52:24 +000091 }
92
93 const lldb::OptionDefinition*
94 GetDefinitions ()
95 {
96 return g_option_table;
97 }
98
99 // Options table: Required for subclasses of Options.
100
101 static lldb::OptionDefinition g_option_table[];
102
103 // Instance variables to hold the values for command options.
104
105 bool stop_at_entry;
Greg Claytonc1d37752010-10-18 01:45:30 +0000106 bool in_new_tty;
Caroline Ticebd666012010-12-03 18:46:09 +0000107 bool no_stdio;
Greg Claytonbb0c91f2010-10-19 23:16:00 +0000108 std::string tty_name;
Chris Lattner24943d22010-06-08 16:52:24 +0000109 std::string stderr_path;
110 std::string stdin_path;
111 std::string stdout_path;
112 std::string plugin_name;
113
114 };
115
Greg Clayton238c0a12010-09-18 01:14:36 +0000116 CommandObjectProcessLaunch (CommandInterpreter &interpreter) :
117 CommandObject (interpreter,
118 "process launch",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000119 "Launch the executable in the debugger.",
Caroline Tice43b014a2010-10-04 22:28:36 +0000120 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000121 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000122 CommandArgumentEntry arg;
123 CommandArgumentData run_args_arg;
124
125 // Define the first (and only) variant of this arg.
126 run_args_arg.arg_type = eArgTypeRunArgs;
127 run_args_arg.arg_repetition = eArgRepeatOptional;
128
129 // There is only one variant this argument could be; put it into the argument entry.
130 arg.push_back (run_args_arg);
131
132 // Push the data for the first argument into the m_arguments vector.
133 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000134 }
135
136
137 ~CommandObjectProcessLaunch ()
138 {
139 }
140
141 Options *
142 GetOptions ()
143 {
144 return &m_options;
145 }
146
147 bool
Greg Claytond8c62532010-10-07 04:19:01 +0000148 Execute (Args& launch_args, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +0000149 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000150 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000151
152 if (target == NULL)
153 {
154 result.AppendError ("invalid target, set executable file using 'file' command");
155 result.SetStatus (eReturnStatusFailed);
156 return false;
157 }
158
159 // If our listener is NULL, users aren't allows to launch
Chris Lattner24943d22010-06-08 16:52:24 +0000160 char filename[PATH_MAX];
Greg Claytonc1d37752010-10-18 01:45:30 +0000161 const Module *exe_module = target->GetExecutableModule().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000162 exe_module->GetFileSpec().GetPath(filename, sizeof(filename));
163
Greg Clayton238c0a12010-09-18 01:14:36 +0000164 Process *process = m_interpreter.GetDebugger().GetExecutionContext().process;
165 if (process && process->IsAlive())
Jim Ingham22dc9722010-12-09 18:58:16 +0000166 {
167 if (!m_interpreter.Confirm ("There is a running process, kill it and restart?", true))
168 {
169 result.AppendErrorWithFormat ("Process %u is currently being debugged, restart cancelled.\n",
170 process->GetID());
171 result.SetStatus (eReturnStatusFailed);
172 return false;
173 }
174 else
175 {
176 Error error (process->Destroy());
177 if (error.Success())
178 {
179 result.SetStatus (eReturnStatusSuccessFinishResult);
180 }
181 else
182 {
183 result.AppendErrorWithFormat ("Failed to kill process: %s\n", error.AsCString());
184 result.SetStatus (eReturnStatusFailed);
185 }
186 }
Chris Lattner24943d22010-06-08 16:52:24 +0000187 }
Jim Ingham22dc9722010-12-09 18:58:16 +0000188
Chris Lattner24943d22010-06-08 16:52:24 +0000189 const char *plugin_name;
190 if (!m_options.plugin_name.empty())
191 plugin_name = m_options.plugin_name.c_str();
192 else
193 plugin_name = NULL;
194
Greg Clayton238c0a12010-09-18 01:14:36 +0000195 process = target->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name).get();
Chris Lattner24943d22010-06-08 16:52:24 +0000196
Greg Clayton238c0a12010-09-18 01:14:36 +0000197 if (process == NULL)
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000198 {
Caroline Tice734b4832010-10-15 21:52:38 +0000199 result.AppendErrorWithFormat ("Failed to find a process plugin for executable.\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000200 result.SetStatus (eReturnStatusFailed);
201 return false;
202 }
203
Greg Clayton238c0a12010-09-18 01:14:36 +0000204 // If no launch args were given on the command line, then use any that
205 // might have been set using the "run-args" set variable.
206 if (launch_args.GetArgumentCount() == 0)
207 {
208 if (process->GetRunArguments().GetArgumentCount() > 0)
209 launch_args = process->GetRunArguments();
210 }
211
Greg Claytonc1d37752010-10-18 01:45:30 +0000212 if (m_options.in_new_tty)
213 {
214 char exec_file_path[PATH_MAX];
215 if (exe_module->GetFileSpec().GetPath(exec_file_path, sizeof(exec_file_path)))
216 {
217 launch_args.InsertArgumentAtIndex(0, exec_file_path);
218 }
219 else
220 {
221 result.AppendError("invalid executable");
222 result.SetStatus (eReturnStatusFailed);
223 return false;
224 }
225 }
226
Greg Clayton238c0a12010-09-18 01:14:36 +0000227 Args environment;
228
229 process->GetEnvironmentAsArgs (environment);
230
231 uint32_t launch_flags = eLaunchFlagNone;
232
233 if (process->GetDisableASLR())
234 launch_flags |= eLaunchFlagDisableASLR;
Greg Claytonc1d37752010-10-18 01:45:30 +0000235
Caroline Ticebd666012010-12-03 18:46:09 +0000236 if (m_options.no_stdio)
237 launch_flags |= eLaunchFlagDisableSTDIO;
238 else if (!m_options.in_new_tty
239 && m_options.stdin_path.empty()
240 && m_options.stdout_path.empty()
241 && m_options.stderr_path.empty())
242 {
243 // Only use the settings value if the user hasn't specified any options that would override it.
244 if (process->GetDisableSTDIO())
245 launch_flags |= eLaunchFlagDisableSTDIO;
246 }
247
Greg Claytonc1d37752010-10-18 01:45:30 +0000248 const char **inferior_argv = launch_args.GetArgumentCount() ? launch_args.GetConstArgumentVector() : NULL;
249 const char **inferior_envp = environment.GetArgumentCount() ? environment.GetConstArgumentVector() : NULL;
Greg Clayton238c0a12010-09-18 01:14:36 +0000250
Greg Claytonc1d37752010-10-18 01:45:30 +0000251 Error error;
Greg Clayton238c0a12010-09-18 01:14:36 +0000252
Greg Claytonc1d37752010-10-18 01:45:30 +0000253 if (m_options.in_new_tty)
Greg Clayton238c0a12010-09-18 01:14:36 +0000254 {
Greg Claytonc1d37752010-10-18 01:45:30 +0000255
Greg Claytonbb0c91f2010-10-19 23:16:00 +0000256 lldb::pid_t pid = Host::LaunchInNewTerminal (m_options.tty_name.c_str(),
257 inferior_argv,
Greg Clayton36f63a92010-10-19 03:25:40 +0000258 inferior_envp,
259 &exe_module->GetArchitecture(),
260 true,
261 process->GetDisableASLR());
Greg Claytonc1d37752010-10-18 01:45:30 +0000262
Greg Clayton36f63a92010-10-19 03:25:40 +0000263 if (pid != LLDB_INVALID_PROCESS_ID)
264 error = process->Attach (pid);
Greg Clayton238c0a12010-09-18 01:14:36 +0000265 }
266 else
267 {
Greg Claytonc1d37752010-10-18 01:45:30 +0000268 const char * stdin_path = NULL;
269 const char * stdout_path = NULL;
270 const char * stderr_path = NULL;
271
272 // Were any standard input/output/error paths given on the command line?
273 if (m_options.stdin_path.empty() &&
274 m_options.stdout_path.empty() &&
275 m_options.stderr_path.empty())
276 {
277 // No standard file handles were given on the command line, check
278 // with the process object in case they were give using "set settings"
279 stdin_path = process->GetStandardInputPath();
280 stdout_path = process->GetStandardOutputPath();
281 stderr_path = process->GetStandardErrorPath();
282 }
283 else
284 {
285 stdin_path = m_options.stdin_path.empty() ? NULL : m_options.stdin_path.c_str();
286 stdout_path = m_options.stdout_path.empty() ? NULL : m_options.stdout_path.c_str();
287 stderr_path = m_options.stderr_path.empty() ? NULL : m_options.stderr_path.c_str();
288 }
289
290 if (stdin_path == NULL)
291 stdin_path = "/dev/null";
292 if (stdout_path == NULL)
293 stdout_path = "/dev/null";
294 if (stderr_path == NULL)
295 stderr_path = "/dev/null";
296
297 error = process->Launch (inferior_argv,
298 inferior_envp,
299 launch_flags,
300 stdin_path,
301 stdout_path,
302 stderr_path);
Greg Clayton238c0a12010-09-18 01:14:36 +0000303 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000304
305 if (error.Success())
306 {
Greg Claytonc1d37752010-10-18 01:45:30 +0000307 const char *archname = exe_module->GetArchitecture().AsCString();
308
309 result.AppendMessageWithFormat ("Process %i launched: '%s' (%s)\n", process->GetID(), filename, archname);
Greg Claytond8c62532010-10-07 04:19:01 +0000310 result.SetDidChangeProcessState (true);
Greg Clayton238c0a12010-09-18 01:14:36 +0000311 if (m_options.stop_at_entry == false)
312 {
Greg Claytond8c62532010-10-07 04:19:01 +0000313 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
Greg Clayton238c0a12010-09-18 01:14:36 +0000314 StateType state = process->WaitForProcessToStop (NULL);
315
316 if (state == eStateStopped)
317 {
Greg Claytond8c62532010-10-07 04:19:01 +0000318 error = process->Resume();
319 if (error.Success())
320 {
321 bool synchronous_execution = m_interpreter.GetSynchronous ();
322 if (synchronous_execution)
323 {
324 state = process->WaitForProcessToStop (NULL);
325 result.SetDidChangeProcessState (true);
326 result.SetStatus (eReturnStatusSuccessFinishResult);
327 }
328 else
329 {
330 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
331 }
332 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000333 }
334 }
335 }
336
Chris Lattner24943d22010-06-08 16:52:24 +0000337 return result.Succeeded();
338 }
339
Jim Ingham767af882010-07-07 03:36:20 +0000340 virtual const char *GetRepeatCommand (Args &current_command_args, uint32_t index)
341 {
342 // No repeat for "process launch"...
343 return "";
344 }
345
Chris Lattner24943d22010-06-08 16:52:24 +0000346protected:
347
348 CommandOptions m_options;
349};
350
351
Greg Claytonc1d37752010-10-18 01:45:30 +0000352#define SET1 LLDB_OPT_SET_1
353#define SET2 LLDB_OPT_SET_2
Caroline Ticebd666012010-12-03 18:46:09 +0000354#define SET3 LLDB_OPT_SET_3
Greg Claytonc1d37752010-10-18 01:45:30 +0000355
Chris Lattner24943d22010-06-08 16:52:24 +0000356lldb::OptionDefinition
357CommandObjectProcessLaunch::CommandOptions::g_option_table[] =
358{
Caroline Ticebd666012010-12-03 18:46:09 +0000359{ 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."},
360{ SET1 , false, "stdin", 'i', required_argument, NULL, 0, eArgTypePath, "Redirect stdin for the process to <path>."},
361{ SET1 , false, "stdout", 'o', required_argument, NULL, 0, eArgTypePath, "Redirect stdout for the process to <path>."},
362{ SET1 , false, "stderr", 'e', required_argument, NULL, 0, eArgTypePath, "Redirect stderr for the process to <path>."},
363{ SET1 | SET2 | SET3, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
364{ 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."},
365{ SET3, false, "no-stdio", 'n', no_argument, NULL, 0, eArgTypeNone, "Do not set up for terminal I/O to go to running process."},
366{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000367};
368
Greg Claytonc1d37752010-10-18 01:45:30 +0000369#undef SET1
370#undef SET2
Caroline Ticebd666012010-12-03 18:46:09 +0000371#undef SET3
Chris Lattner24943d22010-06-08 16:52:24 +0000372
373//-------------------------------------------------------------------------
374// CommandObjectProcessAttach
375//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000376#pragma mark CommandObjectProcessAttach
Chris Lattner24943d22010-06-08 16:52:24 +0000377class CommandObjectProcessAttach : public CommandObject
378{
379public:
380
Chris Lattner24943d22010-06-08 16:52:24 +0000381 class CommandOptions : public Options
382 {
383 public:
384
385 CommandOptions () :
386 Options()
387 {
388 // Keep default values of all options in one place: ResetOptionValues ()
389 ResetOptionValues ();
390 }
391
392 ~CommandOptions ()
393 {
394 }
395
396 Error
397 SetOptionValue (int option_idx, const char *option_arg)
398 {
399 Error error;
400 char short_option = (char) m_getopt_table[option_idx].val;
401 bool success = false;
402 switch (short_option)
403 {
404 case 'p':
405 pid = Args::StringToUInt32 (option_arg, LLDB_INVALID_PROCESS_ID, 0, &success);
406 if (!success || pid == LLDB_INVALID_PROCESS_ID)
407 {
408 error.SetErrorStringWithFormat("Invalid process ID '%s'.\n", option_arg);
409 }
410 break;
411
412 case 'P':
413 plugin_name = option_arg;
414 break;
415
416 case 'n':
417 name.assign(option_arg);
418 break;
419
420 case 'w':
421 waitfor = true;
422 break;
423
424 default:
425 error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
426 break;
427 }
428 return error;
429 }
430
431 void
432 ResetOptionValues ()
433 {
434 Options::ResetOptionValues();
435 pid = LLDB_INVALID_PROCESS_ID;
436 name.clear();
437 waitfor = false;
438 }
439
440 const lldb::OptionDefinition*
441 GetDefinitions ()
442 {
443 return g_option_table;
444 }
445
Jim Ingham7508e732010-08-09 23:31:02 +0000446 virtual bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000447 HandleOptionArgumentCompletion (CommandInterpreter &interpeter,
Jim Ingham7508e732010-08-09 23:31:02 +0000448 Args &input,
449 int cursor_index,
450 int char_pos,
451 OptionElementVector &opt_element_vector,
452 int opt_element_index,
453 int match_start_point,
454 int max_return_elements,
455 bool &word_complete,
456 StringList &matches)
457 {
458 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
459 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
460
461 // We are only completing the name option for now...
462
463 const lldb::OptionDefinition *opt_defs = GetDefinitions();
464 if (opt_defs[opt_defs_index].short_option == 'n')
465 {
466 // Are we in the name?
467
468 // Look to see if there is a -P argument provided, and if so use that plugin, otherwise
469 // use the default plugin.
Greg Clayton238c0a12010-09-18 01:14:36 +0000470 Process *process = interpeter.GetDebugger().GetExecutionContext().process;
Jim Ingham7508e732010-08-09 23:31:02 +0000471 bool need_to_delete_process = false;
472
473 const char *partial_name = NULL;
474 partial_name = input.GetArgumentAtIndex(opt_arg_pos);
475
476 if (process && process->IsAlive())
477 return true;
478
Greg Clayton238c0a12010-09-18 01:14:36 +0000479 Target *target = interpeter.GetDebugger().GetSelectedTarget().get();
Jim Ingham7508e732010-08-09 23:31:02 +0000480 if (target == NULL)
481 {
482 // No target has been set yet, for now do host completion. Otherwise I don't know how we would
483 // figure out what the right target to use is...
484 std::vector<lldb::pid_t> pids;
485 Host::ListProcessesMatchingName (partial_name, matches, pids);
486 return true;
487 }
488 if (!process)
489 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000490 process = target->CreateProcess (interpeter.GetDebugger().GetListener(), partial_name).get();
Jim Ingham7508e732010-08-09 23:31:02 +0000491 need_to_delete_process = true;
492 }
493
494 if (process)
495 {
496 matches.Clear();
497 std::vector<lldb::pid_t> pids;
498 process->ListProcessesMatchingName (NULL, matches, pids);
499 if (need_to_delete_process)
500 target->DeleteCurrentProcess();
501 return true;
502 }
503 }
504
505 return false;
506 }
507
Chris Lattner24943d22010-06-08 16:52:24 +0000508 // Options table: Required for subclasses of Options.
509
510 static lldb::OptionDefinition g_option_table[];
511
512 // Instance variables to hold the values for command options.
513
514 lldb::pid_t pid;
515 std::string plugin_name;
516 std::string name;
517 bool waitfor;
518 };
519
Greg Clayton238c0a12010-09-18 01:14:36 +0000520 CommandObjectProcessAttach (CommandInterpreter &interpreter) :
521 CommandObject (interpreter,
522 "process attach",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000523 "Attach to a process.",
Jim Ingham7508e732010-08-09 23:31:02 +0000524 "process attach <cmd-options>")
525 {
Jim Ingham7508e732010-08-09 23:31:02 +0000526 }
527
528 ~CommandObjectProcessAttach ()
529 {
530 }
531
532 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000533 Execute (Args& command,
Jim Ingham7508e732010-08-09 23:31:02 +0000534 CommandReturnObject &result)
535 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000536 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Jim Ingham7508e732010-08-09 23:31:02 +0000537
Greg Clayton238c0a12010-09-18 01:14:36 +0000538 Process *process = m_interpreter.GetDebugger().GetExecutionContext().process;
Jim Ingham7508e732010-08-09 23:31:02 +0000539 if (process)
540 {
541 if (process->IsAlive())
542 {
543 result.AppendErrorWithFormat ("Process %u is currently being debugged, kill the process before attaching.\n",
544 process->GetID());
545 result.SetStatus (eReturnStatusFailed);
546 return false;
547 }
548 }
549
550 if (target == NULL)
551 {
552 // If there isn't a current target create one.
553 TargetSP new_target_sp;
554 FileSpec emptyFileSpec;
555 ArchSpec emptyArchSpec;
556 Error error;
557
Greg Clayton238c0a12010-09-18 01:14:36 +0000558 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
559 emptyFileSpec,
560 emptyArchSpec,
561 NULL,
562 false,
563 new_target_sp);
Jim Ingham7508e732010-08-09 23:31:02 +0000564 target = new_target_sp.get();
565 if (target == NULL || error.Fail())
566 {
567 result.AppendError(error.AsCString("Error creating empty target"));
568 return false;
569 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000570 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target);
Jim Ingham7508e732010-08-09 23:31:02 +0000571 }
572
573 // Record the old executable module, we want to issue a warning if the process of attaching changed the
574 // current executable (like somebody said "file foo" then attached to a PID whose executable was bar.)
575
576 ModuleSP old_exec_module_sp = target->GetExecutableModule();
577 ArchSpec old_arch_spec = target->GetArchitecture();
578
579 if (command.GetArgumentCount())
580 {
581 result.AppendErrorWithFormat("Invalid arguments for '%s'.\nUsage: \n", m_cmd_name.c_str(), m_cmd_syntax.c_str());
582 result.SetStatus (eReturnStatusFailed);
583 }
584 else
585 {
586 const char *plugin_name = NULL;
587
588 if (!m_options.plugin_name.empty())
589 plugin_name = m_options.plugin_name.c_str();
590
Greg Clayton238c0a12010-09-18 01:14:36 +0000591 process = target->CreateProcess (m_interpreter.GetDebugger().GetListener(), plugin_name).get();
Jim Ingham7508e732010-08-09 23:31:02 +0000592
593 if (process)
594 {
595 Error error;
596 int attach_pid = m_options.pid;
597
Jim Ingham4805a1c2010-09-15 01:34:14 +0000598 const char *wait_name = NULL;
599
600 if (m_options.name.empty())
601 {
602 if (old_exec_module_sp)
603 {
604 wait_name = old_exec_module_sp->GetFileSpec().GetFilename().AsCString();
605 }
606 }
607 else
608 {
609 wait_name = m_options.name.c_str();
610 }
611
Jim Ingham7508e732010-08-09 23:31:02 +0000612 // If we are waiting for a process with this name to show up, do that first.
613 if (m_options.waitfor)
614 {
Jim Ingham4805a1c2010-09-15 01:34:14 +0000615
616 if (wait_name == NULL)
Jim Ingham7508e732010-08-09 23:31:02 +0000617 {
Jim Ingham4805a1c2010-09-15 01:34:14 +0000618 result.AppendError("Invalid arguments: must have a file loaded or supply a process name with the waitfor option.\n");
Jim Ingham7508e732010-08-09 23:31:02 +0000619 result.SetStatus (eReturnStatusFailed);
620 return false;
621 }
Jim Ingham4805a1c2010-09-15 01:34:14 +0000622
Greg Clayton238c0a12010-09-18 01:14:36 +0000623 m_interpreter.GetDebugger().GetOutputStream().Printf("Waiting to attach to a process named \"%s\".\n", wait_name);
Jim Ingham4805a1c2010-09-15 01:34:14 +0000624 error = process->Attach (wait_name, m_options.waitfor);
625 if (error.Success())
626 {
627 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
628 }
Jim Ingham7508e732010-08-09 23:31:02 +0000629 else
630 {
Jim Ingham4805a1c2010-09-15 01:34:14 +0000631 result.AppendErrorWithFormat ("Waiting for a process to launch named '%s': %s\n",
632 wait_name,
633 error.AsCString());
634 result.SetStatus (eReturnStatusFailed);
635 return false;
Jim Ingham7508e732010-08-09 23:31:02 +0000636 }
637 }
638 else
639 {
640 // If the process was specified by name look it up, so we can warn if there are multiple
641 // processes with this pid.
642
Jim Ingham4805a1c2010-09-15 01:34:14 +0000643 if (attach_pid == LLDB_INVALID_PROCESS_ID && wait_name != NULL)
Jim Ingham7508e732010-08-09 23:31:02 +0000644 {
645 std::vector<lldb::pid_t> pids;
646 StringList matches;
647
Jim Ingham4805a1c2010-09-15 01:34:14 +0000648 process->ListProcessesMatchingName(wait_name, matches, pids);
Jim Ingham7508e732010-08-09 23:31:02 +0000649 if (matches.GetSize() > 1)
650 {
Jim Ingham4805a1c2010-09-15 01:34:14 +0000651 result.AppendErrorWithFormat("More than one process named %s\n", wait_name);
Jim Ingham7508e732010-08-09 23:31:02 +0000652 result.SetStatus (eReturnStatusFailed);
653 return false;
654 }
655 else if (matches.GetSize() == 0)
656 {
Jim Ingham4805a1c2010-09-15 01:34:14 +0000657 result.AppendErrorWithFormat("Could not find a process named %s\n", wait_name);
Jim Ingham7508e732010-08-09 23:31:02 +0000658 result.SetStatus (eReturnStatusFailed);
659 return false;
660 }
661 else
662 {
663 attach_pid = pids[0];
664 }
665
666 }
667
668 if (attach_pid != LLDB_INVALID_PROCESS_ID)
669 {
670 error = process->Attach (attach_pid);
671 if (error.Success())
672 {
673 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
674 }
675 else
676 {
677 result.AppendErrorWithFormat ("Attaching to process %i failed: %s.\n",
678 attach_pid,
679 error.AsCString());
680 result.SetStatus (eReturnStatusFailed);
681 }
682 }
683 else
684 {
685 result.AppendErrorWithFormat ("No PID specified for attach\n",
686 attach_pid,
687 error.AsCString());
688 result.SetStatus (eReturnStatusFailed);
689
690 }
691 }
692 }
693 }
694
695 if (result.Succeeded())
696 {
697 // Okay, we're done. Last step is to warn if the executable module has changed:
698 if (!old_exec_module_sp)
699 {
700 char new_path[PATH_MAX + 1];
701 target->GetExecutableModule()->GetFileSpec().GetPath(new_path, PATH_MAX);
702
703 result.AppendMessageWithFormat("Executable module set to \"%s\".\n",
704 new_path);
705 }
706 else if (old_exec_module_sp->GetFileSpec() != target->GetExecutableModule()->GetFileSpec())
707 {
708 char old_path[PATH_MAX + 1];
709 char new_path[PATH_MAX + 1];
710
711 old_exec_module_sp->GetFileSpec().GetPath(old_path, PATH_MAX);
712 target->GetExecutableModule()->GetFileSpec().GetPath (new_path, PATH_MAX);
713
714 result.AppendWarningWithFormat("Executable module changed from \"%s\" to \"%s\".\n",
715 old_path, new_path);
716 }
717
718 if (!old_arch_spec.IsValid())
719 {
720 result.AppendMessageWithFormat ("Architecture set to: %s.\n", target->GetArchitecture().AsCString());
721 }
722 else if (old_arch_spec != target->GetArchitecture())
723 {
724 result.AppendWarningWithFormat("Architecture changed from %s to %s.\n",
725 old_arch_spec.AsCString(), target->GetArchitecture().AsCString());
726 }
727 }
728 return result.Succeeded();
729 }
730
731 Options *
732 GetOptions ()
733 {
734 return &m_options;
735 }
736
Chris Lattner24943d22010-06-08 16:52:24 +0000737protected:
738
739 CommandOptions m_options;
740};
741
742
743lldb::OptionDefinition
744CommandObjectProcessAttach::CommandOptions::g_option_table[] =
745{
Caroline Tice4d6675c2010-10-01 19:59:14 +0000746{ LLDB_OPT_SET_ALL, false, "plugin", 'P', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
747{ LLDB_OPT_SET_1, false, "pid", 'p', required_argument, NULL, 0, eArgTypePid, "The process ID of an existing process to attach to."},
748{ LLDB_OPT_SET_2, false, "name", 'n', required_argument, NULL, 0, eArgTypeProcessName, "The name of the process to attach to."},
749{ LLDB_OPT_SET_2, false, "waitfor",'w', no_argument, NULL, 0, eArgTypeNone, "Wait for the the process with <process-name> to launch."},
750{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000751};
752
753//-------------------------------------------------------------------------
754// CommandObjectProcessContinue
755//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000756#pragma mark CommandObjectProcessContinue
Chris Lattner24943d22010-06-08 16:52:24 +0000757
758class CommandObjectProcessContinue : public CommandObject
759{
760public:
761
Greg Clayton238c0a12010-09-18 01:14:36 +0000762 CommandObjectProcessContinue (CommandInterpreter &interpreter) :
763 CommandObject (interpreter,
764 "process continue",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000765 "Continue execution of all threads in the current process.",
Chris Lattner24943d22010-06-08 16:52:24 +0000766 "process continue",
767 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
768 {
769 }
770
771
772 ~CommandObjectProcessContinue ()
773 {
774 }
775
776 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000777 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000778 CommandReturnObject &result)
779 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000780 Process *process = m_interpreter.GetDebugger().GetExecutionContext().process;
781 bool synchronous_execution = m_interpreter.GetSynchronous ();
Chris Lattner24943d22010-06-08 16:52:24 +0000782
783 if (process == NULL)
784 {
785 result.AppendError ("no process to continue");
786 result.SetStatus (eReturnStatusFailed);
787 return false;
788 }
789
790 StateType state = process->GetState();
791 if (state == eStateStopped)
792 {
793 if (command.GetArgumentCount() != 0)
794 {
795 result.AppendErrorWithFormat ("The '%s' command does not take any arguments.\n", m_cmd_name.c_str());
796 result.SetStatus (eReturnStatusFailed);
797 return false;
798 }
799
800 const uint32_t num_threads = process->GetThreadList().GetSize();
801
802 // Set the actions that the threads should each take when resuming
803 for (uint32_t idx=0; idx<num_threads; ++idx)
804 {
805 process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState (eStateRunning);
806 }
807
808 Error error(process->Resume());
809 if (error.Success())
810 {
Greg Claytonc1d37752010-10-18 01:45:30 +0000811 result.AppendMessageWithFormat ("Process %i resuming\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000812 if (synchronous_execution)
813 {
Greg Claytonbef15832010-07-14 00:18:15 +0000814 state = process->WaitForProcessToStop (NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000815
816 result.SetDidChangeProcessState (true);
817 result.AppendMessageWithFormat ("Process %i %s\n", process->GetID(), StateAsCString (state));
818 result.SetStatus (eReturnStatusSuccessFinishNoResult);
819 }
820 else
821 {
822 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
823 }
824 }
825 else
826 {
827 result.AppendErrorWithFormat("Failed to resume process: %s.\n", error.AsCString());
828 result.SetStatus (eReturnStatusFailed);
829 }
830 }
831 else
832 {
833 result.AppendErrorWithFormat ("Process cannot be continued from its current state (%s).\n",
834 StateAsCString(state));
835 result.SetStatus (eReturnStatusFailed);
836 }
837 return result.Succeeded();
838 }
839};
840
841//-------------------------------------------------------------------------
842// CommandObjectProcessDetach
843//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000844#pragma mark CommandObjectProcessDetach
Chris Lattner24943d22010-06-08 16:52:24 +0000845
846class CommandObjectProcessDetach : public CommandObject
847{
848public:
849
Greg Clayton238c0a12010-09-18 01:14:36 +0000850 CommandObjectProcessDetach (CommandInterpreter &interpreter) :
851 CommandObject (interpreter,
852 "process detach",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000853 "Detach from the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +0000854 "process detach",
855 eFlagProcessMustBeLaunched)
856 {
857 }
858
859 ~CommandObjectProcessDetach ()
860 {
861 }
862
863 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000864 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000865 CommandReturnObject &result)
866 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000867 Process *process = m_interpreter.GetDebugger().GetExecutionContext().process;
Chris Lattner24943d22010-06-08 16:52:24 +0000868 if (process == NULL)
869 {
870 result.AppendError ("must have a valid process in order to detach");
871 result.SetStatus (eReturnStatusFailed);
872 return false;
873 }
874
Caroline Tice90b42252010-11-02 16:16:53 +0000875 result.AppendMessageWithFormat ("Detaching from process %i\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000876 Error error (process->Detach());
877 if (error.Success())
878 {
879 result.SetStatus (eReturnStatusSuccessFinishResult);
880 }
881 else
882 {
883 result.AppendErrorWithFormat ("Detach failed: %s\n", error.AsCString());
884 result.SetStatus (eReturnStatusFailed);
885 return false;
886 }
887 return result.Succeeded();
888 }
889};
890
891//-------------------------------------------------------------------------
Greg Clayton0baa3942010-11-04 01:54:29 +0000892// CommandObjectProcessLoad
893//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000894#pragma mark CommandObjectProcessLoad
Greg Clayton0baa3942010-11-04 01:54:29 +0000895
896class CommandObjectProcessLoad : public CommandObject
897{
898public:
899
900 CommandObjectProcessLoad (CommandInterpreter &interpreter) :
901 CommandObject (interpreter,
902 "process load",
903 "Load a shared library into the current process.",
904 "process load <filename> [<filename> ...]",
905 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
906 {
907 }
908
909 ~CommandObjectProcessLoad ()
910 {
911 }
912
913 bool
914 Execute (Args& command,
915 CommandReturnObject &result)
916 {
917 Process *process = m_interpreter.GetDebugger().GetExecutionContext().process;
918 if (process == NULL)
919 {
920 result.AppendError ("must have a valid process in order to load a shared library");
921 result.SetStatus (eReturnStatusFailed);
922 return false;
923 }
924
925 const uint32_t argc = command.GetArgumentCount();
926
927 for (uint32_t i=0; i<argc; ++i)
928 {
929 Error error;
930 const char *image_path = command.GetArgumentAtIndex(i);
931 FileSpec image_spec (image_path, false);
932 uint32_t image_token = process->LoadImage(image_spec, error);
933 if (image_token != LLDB_INVALID_IMAGE_TOKEN)
934 {
935 result.AppendMessageWithFormat ("Loading \"%s\"...ok\nImage %u loaded.\n", image_path, image_token);
936 result.SetStatus (eReturnStatusSuccessFinishResult);
937 }
938 else
939 {
940 result.AppendErrorWithFormat ("failed to load '%s': %s", image_path, error.AsCString());
941 result.SetStatus (eReturnStatusFailed);
942 }
943 }
944 return result.Succeeded();
945 }
946};
947
948
949//-------------------------------------------------------------------------
950// CommandObjectProcessUnload
951//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +0000952#pragma mark CommandObjectProcessUnload
Greg Clayton0baa3942010-11-04 01:54:29 +0000953
954class CommandObjectProcessUnload : public CommandObject
955{
956public:
957
958 CommandObjectProcessUnload (CommandInterpreter &interpreter) :
959 CommandObject (interpreter,
960 "process unload",
961 "Unload a shared library from the current process using the index returned by a previous call to \"process load\".",
962 "process unload <index>",
963 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
964 {
965 }
966
967 ~CommandObjectProcessUnload ()
968 {
969 }
970
971 bool
972 Execute (Args& command,
973 CommandReturnObject &result)
974 {
975 Process *process = m_interpreter.GetDebugger().GetExecutionContext().process;
976 if (process == NULL)
977 {
978 result.AppendError ("must have a valid process in order to load a shared library");
979 result.SetStatus (eReturnStatusFailed);
980 return false;
981 }
982
983 const uint32_t argc = command.GetArgumentCount();
984
985 for (uint32_t i=0; i<argc; ++i)
986 {
987 const char *image_token_cstr = command.GetArgumentAtIndex(i);
988 uint32_t image_token = Args::StringToUInt32(image_token_cstr, LLDB_INVALID_IMAGE_TOKEN, 0);
989 if (image_token == LLDB_INVALID_IMAGE_TOKEN)
990 {
991 result.AppendErrorWithFormat ("invalid image index argument '%s'", image_token_cstr);
992 result.SetStatus (eReturnStatusFailed);
993 break;
994 }
995 else
996 {
997 Error error (process->UnloadImage(image_token));
998 if (error.Success())
999 {
1000 result.AppendMessageWithFormat ("Unloading shared library with index %u...ok\n", image_token);
1001 result.SetStatus (eReturnStatusSuccessFinishResult);
1002 }
1003 else
1004 {
1005 result.AppendErrorWithFormat ("failed to unload image: %s", error.AsCString());
1006 result.SetStatus (eReturnStatusFailed);
1007 break;
1008 }
1009 }
1010 }
1011 return result.Succeeded();
1012 }
1013};
1014
1015//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001016// CommandObjectProcessSignal
1017//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001018#pragma mark CommandObjectProcessSignal
Chris Lattner24943d22010-06-08 16:52:24 +00001019
1020class CommandObjectProcessSignal : public CommandObject
1021{
1022public:
1023
Greg Clayton238c0a12010-09-18 01:14:36 +00001024 CommandObjectProcessSignal (CommandInterpreter &interpreter) :
1025 CommandObject (interpreter,
1026 "process signal",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001027 "Send a UNIX signal to the current process being debugged.",
Caroline Tice43b014a2010-10-04 22:28:36 +00001028 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001029 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001030 CommandArgumentEntry arg;
1031 CommandArgumentData signal_arg;
1032
1033 // Define the first (and only) variant of this arg.
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001034 signal_arg.arg_type = eArgTypeUnixSignal;
Caroline Tice43b014a2010-10-04 22:28:36 +00001035 signal_arg.arg_repetition = eArgRepeatPlain;
1036
1037 // There is only one variant this argument could be; put it into the argument entry.
1038 arg.push_back (signal_arg);
1039
1040 // Push the data for the first argument into the m_arguments vector.
1041 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001042 }
1043
1044 ~CommandObjectProcessSignal ()
1045 {
1046 }
1047
1048 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001049 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001050 CommandReturnObject &result)
1051 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001052 Process *process = m_interpreter.GetDebugger().GetExecutionContext().process;
Chris Lattner24943d22010-06-08 16:52:24 +00001053 if (process == NULL)
1054 {
1055 result.AppendError ("no process to signal");
1056 result.SetStatus (eReturnStatusFailed);
1057 return false;
1058 }
1059
1060 if (command.GetArgumentCount() == 1)
1061 {
Greg Clayton8f6be2a2010-10-09 01:40:57 +00001062 int signo = LLDB_INVALID_SIGNAL_NUMBER;
1063
1064 const char *signal_name = command.GetArgumentAtIndex(0);
1065 if (::isxdigit (signal_name[0]))
1066 signo = Args::StringToSInt32(signal_name, LLDB_INVALID_SIGNAL_NUMBER, 0);
1067 else
1068 signo = process->GetUnixSignals().GetSignalNumberFromName (signal_name);
1069
1070 if (signo == LLDB_INVALID_SIGNAL_NUMBER)
Chris Lattner24943d22010-06-08 16:52:24 +00001071 {
1072 result.AppendErrorWithFormat ("Invalid signal argument '%s'.\n", command.GetArgumentAtIndex(0));
1073 result.SetStatus (eReturnStatusFailed);
1074 }
1075 else
1076 {
1077 Error error (process->Signal (signo));
1078 if (error.Success())
1079 {
1080 result.SetStatus (eReturnStatusSuccessFinishResult);
1081 }
1082 else
1083 {
1084 result.AppendErrorWithFormat ("Failed to send signal %i: %s\n", signo, error.AsCString());
1085 result.SetStatus (eReturnStatusFailed);
1086 }
1087 }
1088 }
1089 else
1090 {
1091 result.AppendErrorWithFormat("'%s' takes exactly one signal number argument:\nUsage: \n", m_cmd_name.c_str(),
1092 m_cmd_syntax.c_str());
1093 result.SetStatus (eReturnStatusFailed);
1094 }
1095 return result.Succeeded();
1096 }
1097};
1098
1099
1100//-------------------------------------------------------------------------
1101// CommandObjectProcessInterrupt
1102//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001103#pragma mark CommandObjectProcessInterrupt
Chris Lattner24943d22010-06-08 16:52:24 +00001104
1105class CommandObjectProcessInterrupt : public CommandObject
1106{
1107public:
1108
1109
Greg Clayton238c0a12010-09-18 01:14:36 +00001110 CommandObjectProcessInterrupt (CommandInterpreter &interpreter) :
1111 CommandObject (interpreter,
1112 "process interrupt",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001113 "Interrupt the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +00001114 "process interrupt",
1115 eFlagProcessMustBeLaunched)
1116 {
1117 }
1118
1119 ~CommandObjectProcessInterrupt ()
1120 {
1121 }
1122
1123 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001124 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001125 CommandReturnObject &result)
1126 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001127 Process *process = m_interpreter.GetDebugger().GetExecutionContext().process;
Chris Lattner24943d22010-06-08 16:52:24 +00001128 if (process == NULL)
1129 {
1130 result.AppendError ("no process to halt");
1131 result.SetStatus (eReturnStatusFailed);
1132 return false;
1133 }
1134
1135 if (command.GetArgumentCount() == 0)
1136 {
1137 Error error(process->Halt ());
1138 if (error.Success())
1139 {
1140 result.SetStatus (eReturnStatusSuccessFinishResult);
1141
1142 // Maybe we should add a "SuspendThreadPlans so we
1143 // can halt, and keep in place all the current thread plans.
1144 process->GetThreadList().DiscardThreadPlans();
1145 }
1146 else
1147 {
1148 result.AppendErrorWithFormat ("Failed to halt process: %s\n", error.AsCString());
1149 result.SetStatus (eReturnStatusFailed);
1150 }
1151 }
1152 else
1153 {
1154 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: \n",
1155 m_cmd_name.c_str(),
1156 m_cmd_syntax.c_str());
1157 result.SetStatus (eReturnStatusFailed);
1158 }
1159 return result.Succeeded();
1160 }
1161};
1162
1163//-------------------------------------------------------------------------
1164// CommandObjectProcessKill
1165//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001166#pragma mark CommandObjectProcessKill
Chris Lattner24943d22010-06-08 16:52:24 +00001167
1168class CommandObjectProcessKill : public CommandObject
1169{
1170public:
1171
Greg Clayton238c0a12010-09-18 01:14:36 +00001172 CommandObjectProcessKill (CommandInterpreter &interpreter) :
1173 CommandObject (interpreter,
1174 "process kill",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001175 "Terminate the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +00001176 "process kill",
1177 eFlagProcessMustBeLaunched)
1178 {
1179 }
1180
1181 ~CommandObjectProcessKill ()
1182 {
1183 }
1184
1185 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001186 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001187 CommandReturnObject &result)
1188 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001189 Process *process = m_interpreter.GetDebugger().GetExecutionContext().process;
Chris Lattner24943d22010-06-08 16:52:24 +00001190 if (process == NULL)
1191 {
1192 result.AppendError ("no process to kill");
1193 result.SetStatus (eReturnStatusFailed);
1194 return false;
1195 }
1196
1197 if (command.GetArgumentCount() == 0)
1198 {
1199 Error error (process->Destroy());
1200 if (error.Success())
1201 {
1202 result.SetStatus (eReturnStatusSuccessFinishResult);
1203 }
1204 else
1205 {
1206 result.AppendErrorWithFormat ("Failed to kill process: %s\n", error.AsCString());
1207 result.SetStatus (eReturnStatusFailed);
1208 }
1209 }
1210 else
1211 {
1212 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: \n",
1213 m_cmd_name.c_str(),
1214 m_cmd_syntax.c_str());
1215 result.SetStatus (eReturnStatusFailed);
1216 }
1217 return result.Succeeded();
1218 }
1219};
1220
1221//-------------------------------------------------------------------------
Jim Ingham41313fc2010-06-18 01:23:09 +00001222// CommandObjectProcessStatus
1223//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001224#pragma mark CommandObjectProcessStatus
1225
Jim Ingham41313fc2010-06-18 01:23:09 +00001226class CommandObjectProcessStatus : public CommandObject
1227{
1228public:
Greg Clayton238c0a12010-09-18 01:14:36 +00001229 CommandObjectProcessStatus (CommandInterpreter &interpreter) :
1230 CommandObject (interpreter,
1231 "process status",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001232 "Show the current status and location of executing process.",
1233 "process status",
Jim Ingham41313fc2010-06-18 01:23:09 +00001234 0)
1235 {
1236 }
1237
1238 ~CommandObjectProcessStatus()
1239 {
1240 }
1241
1242
1243 bool
1244 Execute
1245 (
1246 Args& command,
Jim Ingham41313fc2010-06-18 01:23:09 +00001247 CommandReturnObject &result
1248 )
1249 {
1250 StreamString &output_stream = result.GetOutputStream();
1251 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Clayton238c0a12010-09-18 01:14:36 +00001252 ExecutionContext exe_ctx(m_interpreter.GetDebugger().GetExecutionContext());
Jim Ingham41313fc2010-06-18 01:23:09 +00001253 if (exe_ctx.process)
1254 {
1255 const StateType state = exe_ctx.process->GetState();
1256 if (StateIsStoppedState(state))
1257 {
1258 if (state == eStateExited)
1259 {
1260 int exit_status = exe_ctx.process->GetExitStatus();
1261 const char *exit_description = exe_ctx.process->GetExitDescription();
1262 output_stream.Printf ("Process %d exited with status = %i (0x%8.8x) %s\n",
1263 exe_ctx.process->GetID(),
1264 exit_status,
1265 exit_status,
1266 exit_description ? exit_description : "");
1267 }
1268 else
1269 {
1270 output_stream.Printf ("Process %d %s\n", exe_ctx.process->GetID(), StateAsCString (state));
1271 if (exe_ctx.thread == NULL)
1272 exe_ctx.thread = exe_ctx.process->GetThreadList().GetThreadAtIndex(0).get();
1273 if (exe_ctx.thread != NULL)
1274 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001275 DisplayThreadsInfo (m_interpreter, &exe_ctx, result, true, true);
Jim Ingham41313fc2010-06-18 01:23:09 +00001276 }
1277 else
1278 {
1279 result.AppendError ("No valid thread found in current process.");
1280 result.SetStatus (eReturnStatusFailed);
1281 }
1282 }
1283 }
1284 else
1285 {
1286 output_stream.Printf ("Process %d is running.\n",
1287 exe_ctx.process->GetID());
1288 }
1289 }
1290 else
1291 {
1292 result.AppendError ("No current location or status available.");
1293 result.SetStatus (eReturnStatusFailed);
1294 }
1295 return result.Succeeded();
1296 }
1297};
1298
1299//-------------------------------------------------------------------------
Caroline Tice23d6f272010-10-13 20:44:39 +00001300// CommandObjectProcessHandle
1301//-------------------------------------------------------------------------
Jim Ingham22dc9722010-12-09 18:58:16 +00001302#pragma mark CommandObjectProcessHandle
Caroline Tice23d6f272010-10-13 20:44:39 +00001303
1304class CommandObjectProcessHandle : public CommandObject
1305{
1306public:
1307
1308 class CommandOptions : public Options
1309 {
1310 public:
1311
1312 CommandOptions () :
1313 Options ()
1314 {
1315 ResetOptionValues ();
1316 }
1317
1318 ~CommandOptions ()
1319 {
1320 }
1321
1322 Error
1323 SetOptionValue (int option_idx, const char *option_arg)
1324 {
1325 Error error;
1326 char short_option = (char) m_getopt_table[option_idx].val;
1327
1328 switch (short_option)
1329 {
1330 case 's':
1331 stop = option_arg;
1332 break;
1333 case 'n':
1334 notify = option_arg;
1335 break;
1336 case 'p':
1337 pass = option_arg;
1338 break;
1339 default:
1340 error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
1341 break;
1342 }
1343 return error;
1344 }
1345
1346 void
1347 ResetOptionValues ()
1348 {
1349 Options::ResetOptionValues();
1350 stop.clear();
1351 notify.clear();
1352 pass.clear();
1353 }
1354
1355 const lldb::OptionDefinition*
1356 GetDefinitions ()
1357 {
1358 return g_option_table;
1359 }
1360
1361 // Options table: Required for subclasses of Options.
1362
1363 static lldb::OptionDefinition g_option_table[];
1364
1365 // Instance variables to hold the values for command options.
1366
1367 std::string stop;
1368 std::string notify;
1369 std::string pass;
1370 };
1371
1372
1373 CommandObjectProcessHandle (CommandInterpreter &interpreter) :
1374 CommandObject (interpreter,
1375 "process handle",
Caroline Ticee7471982010-10-14 21:31:13 +00001376 "Show or update what the process and debugger should do with various signals received from the OS.",
Caroline Tice23d6f272010-10-13 20:44:39 +00001377 NULL)
1378 {
Caroline Ticee7471982010-10-14 21:31:13 +00001379 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 +00001380 CommandArgumentEntry arg;
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001381 CommandArgumentData signal_arg;
Caroline Tice23d6f272010-10-13 20:44:39 +00001382
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001383 signal_arg.arg_type = eArgTypeUnixSignal;
1384 signal_arg.arg_repetition = eArgRepeatStar;
Caroline Tice23d6f272010-10-13 20:44:39 +00001385
Caroline Tice3a62e6d2010-10-18 22:56:57 +00001386 arg.push_back (signal_arg);
Caroline Tice23d6f272010-10-13 20:44:39 +00001387
1388 m_arguments.push_back (arg);
1389 }
1390
1391 ~CommandObjectProcessHandle ()
1392 {
1393 }
1394
1395 Options *
1396 GetOptions ()
1397 {
1398 return &m_options;
1399 }
1400
1401 bool
Caroline Ticee7471982010-10-14 21:31:13 +00001402 VerifyCommandOptionValue (const std::string &option, int &real_value)
Caroline Tice23d6f272010-10-13 20:44:39 +00001403 {
1404 bool okay = true;
1405
Caroline Ticee7471982010-10-14 21:31:13 +00001406 bool success = false;
1407 bool tmp_value = Args::StringToBoolean (option.c_str(), false, &success);
1408
1409 if (success && tmp_value)
1410 real_value = 1;
1411 else if (success && !tmp_value)
1412 real_value = 0;
Caroline Tice23d6f272010-10-13 20:44:39 +00001413 else
1414 {
1415 // If the value isn't 'true' or 'false', it had better be 0 or 1.
Caroline Ticee7471982010-10-14 21:31:13 +00001416 real_value = Args::StringToUInt32 (option.c_str(), 3);
1417 if (real_value != 0 && real_value != 1)
Caroline Tice23d6f272010-10-13 20:44:39 +00001418 okay = false;
1419 }
1420
1421 return okay;
1422 }
1423
Caroline Ticee7471982010-10-14 21:31:13 +00001424 void
1425 PrintSignalHeader (Stream &str)
1426 {
1427 str.Printf ("NAME PASS STOP NOTIFY\n");
1428 str.Printf ("========== ===== ===== ======\n");
1429 }
1430
1431 void
1432 PrintSignal (Stream &str, int32_t signo, const char *sig_name, UnixSignals &signals)
1433 {
1434 bool stop;
1435 bool suppress;
1436 bool notify;
1437
1438 str.Printf ("%-10s ", sig_name);
1439 if (signals.GetSignalInfo (signo, suppress, stop, notify))
1440 {
1441 bool pass = !suppress;
1442 str.Printf ("%s %s %s",
1443 (pass ? "true " : "false"),
1444 (stop ? "true " : "false"),
1445 (notify ? "true " : "false"));
1446 }
1447 str.Printf ("\n");
1448 }
1449
1450 void
1451 PrintSignalInformation (Stream &str, Args &signal_args, int num_valid_signals, UnixSignals &signals)
1452 {
1453 PrintSignalHeader (str);
1454
1455 if (num_valid_signals > 0)
1456 {
1457 size_t num_args = signal_args.GetArgumentCount();
1458 for (size_t i = 0; i < num_args; ++i)
1459 {
1460 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1461 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
1462 PrintSignal (str, signo, signal_args.GetArgumentAtIndex (i), signals);
1463 }
1464 }
1465 else // Print info for ALL signals
1466 {
1467 int32_t signo = signals.GetFirstSignalNumber();
1468 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1469 {
1470 PrintSignal (str, signo, signals.GetSignalAsCString (signo), signals);
1471 signo = signals.GetNextSignalNumber (signo);
1472 }
1473 }
1474 }
1475
Caroline Tice23d6f272010-10-13 20:44:39 +00001476 bool
1477 Execute (Args &signal_args, CommandReturnObject &result)
1478 {
1479 TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
1480
1481 if (!target_sp)
1482 {
1483 result.AppendError ("No current target;"
1484 " cannot handle signals until you have a valid target and process.\n");
1485 result.SetStatus (eReturnStatusFailed);
1486 return false;
1487 }
1488
1489 ProcessSP process_sp = target_sp->GetProcessSP();
1490
1491 if (!process_sp)
1492 {
1493 result.AppendError ("No current process; cannot handle signals until you have a valid process.\n");
1494 result.SetStatus (eReturnStatusFailed);
1495 return false;
1496 }
1497
Caroline Tice23d6f272010-10-13 20:44:39 +00001498 int stop_action = -1; // -1 means leave the current setting alone
Caroline Ticee7471982010-10-14 21:31:13 +00001499 int pass_action = -1; // -1 means leave the current setting alone
Caroline Tice23d6f272010-10-13 20:44:39 +00001500 int notify_action = -1; // -1 means leave the current setting alone
1501
1502 if (! m_options.stop.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001503 && ! VerifyCommandOptionValue (m_options.stop, stop_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001504 {
1505 result.AppendError ("Invalid argument for command option --stop; must be true or false.\n");
1506 result.SetStatus (eReturnStatusFailed);
1507 return false;
1508 }
1509
1510 if (! m_options.notify.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001511 && ! VerifyCommandOptionValue (m_options.notify, notify_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001512 {
1513 result.AppendError ("Invalid argument for command option --notify; must be true or false.\n");
1514 result.SetStatus (eReturnStatusFailed);
1515 return false;
1516 }
1517
1518 if (! m_options.pass.empty()
Caroline Ticee7471982010-10-14 21:31:13 +00001519 && ! VerifyCommandOptionValue (m_options.pass, pass_action))
Caroline Tice23d6f272010-10-13 20:44:39 +00001520 {
1521 result.AppendError ("Invalid argument for command option --pass; must be true or false.\n");
1522 result.SetStatus (eReturnStatusFailed);
1523 return false;
1524 }
1525
1526 size_t num_args = signal_args.GetArgumentCount();
1527 UnixSignals &signals = process_sp->GetUnixSignals();
1528 int num_signals_set = 0;
1529
Caroline Ticee7471982010-10-14 21:31:13 +00001530 if (num_args > 0)
Caroline Tice23d6f272010-10-13 20:44:39 +00001531 {
Caroline Ticee7471982010-10-14 21:31:13 +00001532 for (size_t i = 0; i < num_args; ++i)
Caroline Tice23d6f272010-10-13 20:44:39 +00001533 {
Caroline Ticee7471982010-10-14 21:31:13 +00001534 int32_t signo = signals.GetSignalNumberFromName (signal_args.GetArgumentAtIndex (i));
1535 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
Caroline Tice23d6f272010-10-13 20:44:39 +00001536 {
Caroline Ticee7471982010-10-14 21:31:13 +00001537 // Casting the actions as bools here should be okay, because VerifyCommandOptionValue guarantees
1538 // the value is either 0 or 1.
1539 if (stop_action != -1)
1540 signals.SetShouldStop (signo, (bool) stop_action);
1541 if (pass_action != -1)
1542 {
1543 bool suppress = ! ((bool) pass_action);
1544 signals.SetShouldSuppress (signo, suppress);
1545 }
1546 if (notify_action != -1)
1547 signals.SetShouldNotify (signo, (bool) notify_action);
1548 ++num_signals_set;
Caroline Tice23d6f272010-10-13 20:44:39 +00001549 }
Caroline Ticee7471982010-10-14 21:31:13 +00001550 else
1551 {
1552 result.AppendErrorWithFormat ("Invalid signal name '%s'\n", signal_args.GetArgumentAtIndex (i));
1553 }
Caroline Tice23d6f272010-10-13 20:44:39 +00001554 }
1555 }
Caroline Ticee7471982010-10-14 21:31:13 +00001556 else
1557 {
1558 // No signal specified, if any command options were specified, update ALL signals.
1559 if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1))
1560 {
1561 if (m_interpreter.Confirm ("Do you really want to update all the signals?", false))
1562 {
1563 int32_t signo = signals.GetFirstSignalNumber();
1564 while (signo != LLDB_INVALID_SIGNAL_NUMBER)
1565 {
1566 if (notify_action != -1)
1567 signals.SetShouldNotify (signo, (bool) notify_action);
1568 if (stop_action != -1)
1569 signals.SetShouldStop (signo, (bool) stop_action);
1570 if (pass_action != -1)
1571 {
1572 bool suppress = ! ((bool) pass_action);
1573 signals.SetShouldSuppress (signo, suppress);
1574 }
1575 signo = signals.GetNextSignalNumber (signo);
1576 }
1577 }
1578 }
1579 }
1580
1581 PrintSignalInformation (result.GetOutputStream(), signal_args, num_signals_set, signals);
Caroline Tice23d6f272010-10-13 20:44:39 +00001582
1583 if (num_signals_set > 0)
1584 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1585 else
1586 result.SetStatus (eReturnStatusFailed);
1587
1588 return result.Succeeded();
1589 }
1590
1591protected:
1592
1593 CommandOptions m_options;
1594};
1595
1596lldb::OptionDefinition
1597CommandObjectProcessHandle::CommandOptions::g_option_table[] =
1598{
1599{ 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." },
1600{ 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." },
1601{ LLDB_OPT_SET_1, false, "pass", 'p', required_argument, NULL, 0, eArgTypeBoolean, "Whether or not the signal should be passed to the process." },
1602{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1603};
1604
1605//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001606// CommandObjectMultiwordProcess
1607//-------------------------------------------------------------------------
1608
Greg Clayton63094e02010-06-23 01:19:29 +00001609CommandObjectMultiwordProcess::CommandObjectMultiwordProcess (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00001610 CommandObjectMultiword (interpreter,
1611 "process",
1612 "A set of commands for operating on a process.",
1613 "process <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00001614{
Greg Clayton238c0a12010-09-18 01:14:36 +00001615 LoadSubCommand ("attach", CommandObjectSP (new CommandObjectProcessAttach (interpreter)));
1616 LoadSubCommand ("launch", CommandObjectSP (new CommandObjectProcessLaunch (interpreter)));
1617 LoadSubCommand ("continue", CommandObjectSP (new CommandObjectProcessContinue (interpreter)));
1618 LoadSubCommand ("detach", CommandObjectSP (new CommandObjectProcessDetach (interpreter)));
Greg Clayton0baa3942010-11-04 01:54:29 +00001619 LoadSubCommand ("load", CommandObjectSP (new CommandObjectProcessLoad (interpreter)));
1620 LoadSubCommand ("unload", CommandObjectSP (new CommandObjectProcessUnload (interpreter)));
Greg Clayton238c0a12010-09-18 01:14:36 +00001621 LoadSubCommand ("signal", CommandObjectSP (new CommandObjectProcessSignal (interpreter)));
Caroline Tice23d6f272010-10-13 20:44:39 +00001622 LoadSubCommand ("handle", CommandObjectSP (new CommandObjectProcessHandle (interpreter)));
Greg Clayton238c0a12010-09-18 01:14:36 +00001623 LoadSubCommand ("status", CommandObjectSP (new CommandObjectProcessStatus (interpreter)));
1624 LoadSubCommand ("interrupt", CommandObjectSP (new CommandObjectProcessInterrupt (interpreter)));
1625 LoadSubCommand ("kill", CommandObjectSP (new CommandObjectProcessKill (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00001626}
1627
1628CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess ()
1629{
1630}
1631