blob: 301cd88f1795965f221d6efaabfee21e05a95c42 [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//-------------------------------------------------------------------------
32
33class 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;
65 default:
66 error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
67 break;
68
69 }
70 return error;
71 }
72
73 void
74 ResetOptionValues ()
75 {
76 Options::ResetOptionValues();
77 stop_at_entry = false;
78 stdin_path.clear();
79 stdout_path.clear();
80 stderr_path.clear();
81 plugin_name.clear();
82 }
83
84 const lldb::OptionDefinition*
85 GetDefinitions ()
86 {
87 return g_option_table;
88 }
89
90 // Options table: Required for subclasses of Options.
91
92 static lldb::OptionDefinition g_option_table[];
93
94 // Instance variables to hold the values for command options.
95
96 bool stop_at_entry;
97 std::string stderr_path;
98 std::string stdin_path;
99 std::string stdout_path;
100 std::string plugin_name;
101
102 };
103
104 CommandObjectProcessLaunch () :
105 CommandObject ("process launch",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000106 "Launch the executable in the debugger.",
Chris Lattner24943d22010-06-08 16:52:24 +0000107 "process launch [<cmd-options>] [<arguments-for-running-the-program>]")
108 {
109 }
110
111
112 ~CommandObjectProcessLaunch ()
113 {
114 }
115
116 Options *
117 GetOptions ()
118 {
119 return &m_options;
120 }
121
122 bool
Greg Clayton63094e02010-06-23 01:19:29 +0000123 Execute (CommandInterpreter &interpreter,
124 Args& launch_args,
Chris Lattner24943d22010-06-08 16:52:24 +0000125 CommandReturnObject &result)
126 {
Jim Inghamc8332952010-08-26 21:32:51 +0000127 Target *target = interpreter.GetDebugger().GetSelectedTarget().get();
Greg Clayton63094e02010-06-23 01:19:29 +0000128 bool synchronous_execution = interpreter.GetSynchronous ();
Chris Lattner24943d22010-06-08 16:52:24 +0000129 // bool launched = false;
130 // bool stopped_after_launch = false;
131
132 if (target == NULL)
133 {
134 result.AppendError ("invalid target, set executable file using 'file' command");
135 result.SetStatus (eReturnStatusFailed);
136 return false;
137 }
138
139 // If our listener is NULL, users aren't allows to launch
Chris Lattner24943d22010-06-08 16:52:24 +0000140 char filename[PATH_MAX];
141 Module *exe_module = target->GetExecutableModule().get();
142 exe_module->GetFileSpec().GetPath(filename, sizeof(filename));
143
Greg Clayton63094e02010-06-23 01:19:29 +0000144 Process *process = interpreter.GetDebugger().GetExecutionContext().process;
Chris Lattner24943d22010-06-08 16:52:24 +0000145 if (process)
146 {
147 if (process->IsAlive())
148 {
149 result.AppendErrorWithFormat ("Process %u is currently being debugged, kill the process before running again.\n",
150 process->GetID());
151 result.SetStatus (eReturnStatusFailed);
152 return false;
153 }
154 }
155
156 const char *plugin_name;
157 if (!m_options.plugin_name.empty())
158 plugin_name = m_options.plugin_name.c_str();
159 else
160 plugin_name = NULL;
161
Greg Clayton63094e02010-06-23 01:19:29 +0000162 process = target->CreateProcess (interpreter.GetDebugger().GetListener(), plugin_name).get();
Chris Lattner24943d22010-06-08 16:52:24 +0000163
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000164 const char *process_name = process->GetInstanceName().AsCString();
Caroline Tice1d2aefd2010-09-09 06:25:08 +0000165 const char *debugger_instance_name = interpreter.GetDebugger().GetInstanceName().AsCString();
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000166 StreamString run_args_var_name;
167 StreamString env_vars_var_name;
168 StreamString disable_aslr_var_name;
169 lldb::SettableVariableType var_type;
170
171 Args *run_args = NULL;
172 run_args_var_name.Printf ("process.[%s].run-args", process_name);
173 StringList run_args_value = Debugger::GetSettingsController()->GetVariable (run_args_var_name.GetData(),
Caroline Tice1d2aefd2010-09-09 06:25:08 +0000174 var_type, debugger_instance_name);
175
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000176 if (run_args_value.GetSize() > 0)
177 {
178 run_args = new Args;
Chris Lattner0f6fa732010-09-08 22:55:31 +0000179 for (unsigned i = 0, e = run_args_value.GetSize(); i != e; ++i)
180 run_args->AppendArgument(run_args_value.GetStringAtIndex(i));
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000181 }
182
183 Args *environment = NULL;
184 env_vars_var_name.Printf ("process.[%s].env-vars", process_name);
185 StringList env_vars_value = Debugger::GetSettingsController()->GetVariable (env_vars_var_name.GetData(),
Caroline Tice1d2aefd2010-09-09 06:25:08 +0000186 var_type, debugger_instance_name);
187
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000188 if (env_vars_value.GetSize() > 0)
189 {
190 environment = new Args;
Chris Lattner0f6fa732010-09-08 22:55:31 +0000191 for (unsigned i = 0, e = env_vars_value.GetSize(); i != e; ++i)
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000192 environment->AppendArgument (env_vars_value.GetStringAtIndex (i));
193 }
194
195 uint32_t launch_flags = eLaunchFlagNone;
196 disable_aslr_var_name.Printf ("process.[%s].disable-aslr", process_name);
197 StringList disable_aslr_value = Debugger::GetSettingsController()->GetVariable(disable_aslr_var_name.GetData(),
Caroline Tice1d2aefd2010-09-09 06:25:08 +0000198 var_type,
199 debugger_instance_name);
200
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000201 if (disable_aslr_value.GetSize() > 0)
202 {
203 if (strcmp (disable_aslr_value.GetStringAtIndex(0), "true") == 0)
204 launch_flags |= eLaunchFlagDisableASLR;
205
206 }
Chris Lattner24943d22010-06-08 16:52:24 +0000207
208 // There are two possible sources of args to be passed to the process upon launching: Those the user
209 // typed at the run command (launch_args); or those the user pre-set in the run-args variable (run_args).
210
211 // If launch_args is empty, use run_args.
212 if (launch_args.GetArgumentCount() == 0)
213 {
214 if (run_args != NULL)
215 launch_args.AppendArguments (*run_args);
216 }
217 else
218 {
219 // launch-args was not empty; use that, AND re-set run-args to contains launch-args values.
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000220 std::string new_run_args;
221 launch_args.GetCommandString (new_run_args);
Caroline Tice1d2aefd2010-09-09 06:25:08 +0000222 Debugger::GetSettingsController()->SetVariable (run_args_var_name.GetData(), new_run_args.c_str(),
223 lldb::eVarSetOperationAssign, false,
224 interpreter.GetDebugger().GetInstanceName().AsCString());
Chris Lattner24943d22010-06-08 16:52:24 +0000225 }
226
227
228 if (process)
229 {
230 const char *archname = exe_module->GetArchitecture().AsCString();
231
232 const char * stdin_path = NULL;
233 const char * stdout_path = NULL;
234 const char * stderr_path = NULL;
235
236 if (!(m_options.stdin_path.empty() &&
237 m_options.stdout_path.empty() &&
238 m_options.stderr_path.empty()))
239 {
240 stdin_path = m_options.stdin_path.empty() ? "/dev/null" : m_options.stdin_path.c_str();
241 stdout_path = m_options.stdout_path.empty() ? "/dev/null" : m_options.stdout_path.c_str();
242 stderr_path = m_options.stderr_path.empty() ? "/dev/null" : m_options.stderr_path.c_str();
243 }
244
245 Error error (process->Launch (launch_args.GetConstArgumentVector(),
246 environment ? environment->GetConstArgumentVector() : NULL,
Greg Clayton452bf612010-08-31 18:35:14 +0000247 launch_flags,
Chris Lattner24943d22010-06-08 16:52:24 +0000248 stdin_path,
249 stdout_path,
250 stderr_path));
251
252 if (error.Success())
253 {
254 result.AppendMessageWithFormat ("Launching '%s' (%s)\n", filename, archname);
255 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
256 if (m_options.stop_at_entry == false)
257 {
258 StateType state = process->WaitForProcessToStop (NULL);
259
260 if (state == eStateStopped)
261 {
262 // Call continue_command.
263 CommandReturnObject continue_result;
Greg Clayton63094e02010-06-23 01:19:29 +0000264 interpreter.HandleCommand("process continue", false, continue_result);
Chris Lattner24943d22010-06-08 16:52:24 +0000265 }
266
267 if (synchronous_execution)
268 {
269 result.SetDidChangeProcessState (true);
270 result.SetStatus (eReturnStatusSuccessFinishNoResult);
271 }
272 }
273 }
274 else
275 {
276 result.AppendErrorWithFormat ("Process launch failed: %s",
277 error.AsCString());
278 result.SetStatus (eReturnStatusFailed);
279 }
280 }
281 else
282 {
283 result.AppendErrorWithFormat ("Process launch failed: unable to create a process object.\n");
284 result.SetStatus (eReturnStatusFailed);
285 return false;
286 }
287
288 return result.Succeeded();
289 }
290
Jim Ingham767af882010-07-07 03:36:20 +0000291 virtual const char *GetRepeatCommand (Args &current_command_args, uint32_t index)
292 {
293 // No repeat for "process launch"...
294 return "";
295 }
296
Chris Lattner24943d22010-06-08 16:52:24 +0000297protected:
298
299 CommandOptions m_options;
300};
301
302
303lldb::OptionDefinition
304CommandObjectProcessLaunch::CommandOptions::g_option_table[] =
305{
Jim Ingham34e9a982010-06-15 18:47:14 +0000306{ LLDB_OPT_SET_1, false, "stop-at-entry", 's', no_argument, NULL, 0, NULL, "Stop at the entry point of the program when launching a process."},
307{ LLDB_OPT_SET_1, false, "stdin", 'i', required_argument, NULL, 0, "<path>", "Redirect stdin for the process to <path>."},
308{ LLDB_OPT_SET_1, false, "stdout", 'o', required_argument, NULL, 0, "<path>", "Redirect stdout for the process to <path>."},
309{ LLDB_OPT_SET_1, false, "stderr", 'e', required_argument, NULL, 0, "<path>", "Redirect stderr for the process to <path>."},
310{ LLDB_OPT_SET_1, false, "plugin", 'p', required_argument, NULL, 0, "<plugin>", "Name of the process plugin you want to use."},
Chris Lattner24943d22010-06-08 16:52:24 +0000311{ 0, false, NULL, 0, 0, NULL, 0, NULL, NULL }
312};
313
314
315//-------------------------------------------------------------------------
316// CommandObjectProcessAttach
317//-------------------------------------------------------------------------
318
319class CommandObjectProcessAttach : public CommandObject
320{
321public:
322
Chris Lattner24943d22010-06-08 16:52:24 +0000323 class CommandOptions : public Options
324 {
325 public:
326
327 CommandOptions () :
328 Options()
329 {
330 // Keep default values of all options in one place: ResetOptionValues ()
331 ResetOptionValues ();
332 }
333
334 ~CommandOptions ()
335 {
336 }
337
338 Error
339 SetOptionValue (int option_idx, const char *option_arg)
340 {
341 Error error;
342 char short_option = (char) m_getopt_table[option_idx].val;
343 bool success = false;
344 switch (short_option)
345 {
346 case 'p':
347 pid = Args::StringToUInt32 (option_arg, LLDB_INVALID_PROCESS_ID, 0, &success);
348 if (!success || pid == LLDB_INVALID_PROCESS_ID)
349 {
350 error.SetErrorStringWithFormat("Invalid process ID '%s'.\n", option_arg);
351 }
352 break;
353
354 case 'P':
355 plugin_name = option_arg;
356 break;
357
358 case 'n':
359 name.assign(option_arg);
360 break;
361
362 case 'w':
363 waitfor = true;
364 break;
365
366 default:
367 error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
368 break;
369 }
370 return error;
371 }
372
373 void
374 ResetOptionValues ()
375 {
376 Options::ResetOptionValues();
377 pid = LLDB_INVALID_PROCESS_ID;
378 name.clear();
379 waitfor = false;
380 }
381
382 const lldb::OptionDefinition*
383 GetDefinitions ()
384 {
385 return g_option_table;
386 }
387
Jim Ingham7508e732010-08-09 23:31:02 +0000388 virtual bool
389 HandleOptionArgumentCompletion (CommandInterpreter &interpreter,
390 Args &input,
391 int cursor_index,
392 int char_pos,
393 OptionElementVector &opt_element_vector,
394 int opt_element_index,
395 int match_start_point,
396 int max_return_elements,
397 bool &word_complete,
398 StringList &matches)
399 {
400 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
401 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
402
403 // We are only completing the name option for now...
404
405 const lldb::OptionDefinition *opt_defs = GetDefinitions();
406 if (opt_defs[opt_defs_index].short_option == 'n')
407 {
408 // Are we in the name?
409
410 // Look to see if there is a -P argument provided, and if so use that plugin, otherwise
411 // use the default plugin.
412 Process *process = interpreter.GetDebugger().GetExecutionContext().process;
413 bool need_to_delete_process = false;
414
415 const char *partial_name = NULL;
416 partial_name = input.GetArgumentAtIndex(opt_arg_pos);
417
418 if (process && process->IsAlive())
419 return true;
420
Jim Inghamc8332952010-08-26 21:32:51 +0000421 Target *target = interpreter.GetDebugger().GetSelectedTarget().get();
Jim Ingham7508e732010-08-09 23:31:02 +0000422 if (target == NULL)
423 {
424 // No target has been set yet, for now do host completion. Otherwise I don't know how we would
425 // figure out what the right target to use is...
426 std::vector<lldb::pid_t> pids;
427 Host::ListProcessesMatchingName (partial_name, matches, pids);
428 return true;
429 }
430 if (!process)
431 {
432 process = target->CreateProcess (interpreter.GetDebugger().GetListener(), partial_name).get();
433 need_to_delete_process = true;
434 }
435
436 if (process)
437 {
438 matches.Clear();
439 std::vector<lldb::pid_t> pids;
440 process->ListProcessesMatchingName (NULL, matches, pids);
441 if (need_to_delete_process)
442 target->DeleteCurrentProcess();
443 return true;
444 }
445 }
446
447 return false;
448 }
449
Chris Lattner24943d22010-06-08 16:52:24 +0000450 // Options table: Required for subclasses of Options.
451
452 static lldb::OptionDefinition g_option_table[];
453
454 // Instance variables to hold the values for command options.
455
456 lldb::pid_t pid;
457 std::string plugin_name;
458 std::string name;
459 bool waitfor;
460 };
461
Jim Ingham7508e732010-08-09 23:31:02 +0000462 CommandObjectProcessAttach () :
463 CommandObject ("process attach",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000464 "Attach to a process.",
Jim Ingham7508e732010-08-09 23:31:02 +0000465 "process attach <cmd-options>")
466 {
Jim Ingham7508e732010-08-09 23:31:02 +0000467 }
468
469 ~CommandObjectProcessAttach ()
470 {
471 }
472
473 bool
474 Execute (CommandInterpreter &interpreter,
475 Args& command,
476 CommandReturnObject &result)
477 {
Jim Inghamc8332952010-08-26 21:32:51 +0000478 Target *target = interpreter.GetDebugger().GetSelectedTarget().get();
Jim Ingham7508e732010-08-09 23:31:02 +0000479
480 Process *process = interpreter.GetDebugger().GetExecutionContext().process;
481 if (process)
482 {
483 if (process->IsAlive())
484 {
485 result.AppendErrorWithFormat ("Process %u is currently being debugged, kill the process before attaching.\n",
486 process->GetID());
487 result.SetStatus (eReturnStatusFailed);
488 return false;
489 }
490 }
491
492 if (target == NULL)
493 {
494 // If there isn't a current target create one.
495 TargetSP new_target_sp;
496 FileSpec emptyFileSpec;
497 ArchSpec emptyArchSpec;
498 Error error;
499
500 error = interpreter.GetDebugger().GetTargetList().CreateTarget(interpreter.GetDebugger(),
501 emptyFileSpec,
502 emptyArchSpec,
503 NULL,
504 false,
505 new_target_sp);
506 target = new_target_sp.get();
507 if (target == NULL || error.Fail())
508 {
509 result.AppendError(error.AsCString("Error creating empty target"));
510 return false;
511 }
Jim Inghamc8332952010-08-26 21:32:51 +0000512 interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target);
Jim Ingham7508e732010-08-09 23:31:02 +0000513 }
514
515 // Record the old executable module, we want to issue a warning if the process of attaching changed the
516 // current executable (like somebody said "file foo" then attached to a PID whose executable was bar.)
517
518 ModuleSP old_exec_module_sp = target->GetExecutableModule();
519 ArchSpec old_arch_spec = target->GetArchitecture();
520
521 if (command.GetArgumentCount())
522 {
523 result.AppendErrorWithFormat("Invalid arguments for '%s'.\nUsage: \n", m_cmd_name.c_str(), m_cmd_syntax.c_str());
524 result.SetStatus (eReturnStatusFailed);
525 }
526 else
527 {
528 const char *plugin_name = NULL;
529
530 if (!m_options.plugin_name.empty())
531 plugin_name = m_options.plugin_name.c_str();
532
533 process = target->CreateProcess (interpreter.GetDebugger().GetListener(), plugin_name).get();
534
535 if (process)
536 {
537 Error error;
538 int attach_pid = m_options.pid;
539
540 // If we are waiting for a process with this name to show up, do that first.
541 if (m_options.waitfor)
542 {
543 if (m_options.name.empty())
544 {
545 result.AppendError("Invalid arguments: must supply a process name with the waitfor option.\n");
546 result.SetStatus (eReturnStatusFailed);
547 return false;
548 }
549 else
550 {
551 error = process->Attach (m_options.name.c_str(), m_options.waitfor);
552 if (error.Success())
553 {
554 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
555 }
556 else
557 {
558 result.AppendErrorWithFormat ("Waiting for a process to launch named '%s': %s\n",
559 m_options.name.c_str(),
560 error.AsCString());
561 result.SetStatus (eReturnStatusFailed);
562 return false;
563 }
564 }
565 }
566 else
567 {
568 // If the process was specified by name look it up, so we can warn if there are multiple
569 // processes with this pid.
570
571 if (attach_pid == LLDB_INVALID_PROCESS_ID && !m_options.name.empty())
572 {
573 std::vector<lldb::pid_t> pids;
574 StringList matches;
575
576 process->ListProcessesMatchingName(m_options.name.c_str(), matches, pids);
577 if (matches.GetSize() > 1)
578 {
579 result.AppendErrorWithFormat("More than one process named %s\n", m_options.name.c_str());
580 result.SetStatus (eReturnStatusFailed);
581 return false;
582 }
583 else if (matches.GetSize() == 0)
584 {
585 result.AppendErrorWithFormat("Could not find a process named %s\n", m_options.name.c_str());
586 result.SetStatus (eReturnStatusFailed);
587 return false;
588 }
589 else
590 {
591 attach_pid = pids[0];
592 }
593
594 }
595
596 if (attach_pid != LLDB_INVALID_PROCESS_ID)
597 {
598 error = process->Attach (attach_pid);
599 if (error.Success())
600 {
601 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
602 }
603 else
604 {
605 result.AppendErrorWithFormat ("Attaching to process %i failed: %s.\n",
606 attach_pid,
607 error.AsCString());
608 result.SetStatus (eReturnStatusFailed);
609 }
610 }
611 else
612 {
613 result.AppendErrorWithFormat ("No PID specified for attach\n",
614 attach_pid,
615 error.AsCString());
616 result.SetStatus (eReturnStatusFailed);
617
618 }
619 }
620 }
621 }
622
623 if (result.Succeeded())
624 {
625 // Okay, we're done. Last step is to warn if the executable module has changed:
626 if (!old_exec_module_sp)
627 {
628 char new_path[PATH_MAX + 1];
629 target->GetExecutableModule()->GetFileSpec().GetPath(new_path, PATH_MAX);
630
631 result.AppendMessageWithFormat("Executable module set to \"%s\".\n",
632 new_path);
633 }
634 else if (old_exec_module_sp->GetFileSpec() != target->GetExecutableModule()->GetFileSpec())
635 {
636 char old_path[PATH_MAX + 1];
637 char new_path[PATH_MAX + 1];
638
639 old_exec_module_sp->GetFileSpec().GetPath(old_path, PATH_MAX);
640 target->GetExecutableModule()->GetFileSpec().GetPath (new_path, PATH_MAX);
641
642 result.AppendWarningWithFormat("Executable module changed from \"%s\" to \"%s\".\n",
643 old_path, new_path);
644 }
645
646 if (!old_arch_spec.IsValid())
647 {
648 result.AppendMessageWithFormat ("Architecture set to: %s.\n", target->GetArchitecture().AsCString());
649 }
650 else if (old_arch_spec != target->GetArchitecture())
651 {
652 result.AppendWarningWithFormat("Architecture changed from %s to %s.\n",
653 old_arch_spec.AsCString(), target->GetArchitecture().AsCString());
654 }
655 }
656 return result.Succeeded();
657 }
658
659 Options *
660 GetOptions ()
661 {
662 return &m_options;
663 }
664
Chris Lattner24943d22010-06-08 16:52:24 +0000665protected:
666
667 CommandOptions m_options;
668};
669
670
671lldb::OptionDefinition
672CommandObjectProcessAttach::CommandOptions::g_option_table[] =
673{
Jim Ingham34e9a982010-06-15 18:47:14 +0000674{ LLDB_OPT_SET_ALL, false, "plugin", 'P', required_argument, NULL, 0, "<plugin>", "Name of the process plugin you want to use."},
675{ LLDB_OPT_SET_1, false, "pid", 'p', required_argument, NULL, 0, "<pid>", "The process ID of an existing process to attach to."},
676{ LLDB_OPT_SET_2, true, "name", 'n', required_argument, NULL, 0, "<process-name>", "The name of the process to attach to."},
677{ LLDB_OPT_SET_2, false, "waitfor", 'w', no_argument, NULL, 0, NULL, "Wait for the the process with <process-name> to launch."},
Chris Lattner24943d22010-06-08 16:52:24 +0000678{ 0, false, NULL, 0, 0, NULL, 0, NULL, NULL }
679};
680
681//-------------------------------------------------------------------------
682// CommandObjectProcessContinue
683//-------------------------------------------------------------------------
684
685class CommandObjectProcessContinue : public CommandObject
686{
687public:
688
689 CommandObjectProcessContinue () :
690 CommandObject ("process continue",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000691 "Continue execution of all threads in the current process.",
Chris Lattner24943d22010-06-08 16:52:24 +0000692 "process continue",
693 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
694 {
695 }
696
697
698 ~CommandObjectProcessContinue ()
699 {
700 }
701
702 bool
Greg Clayton63094e02010-06-23 01:19:29 +0000703 Execute (CommandInterpreter &interpreter,
704 Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000705 CommandReturnObject &result)
706 {
Greg Clayton63094e02010-06-23 01:19:29 +0000707 Process *process = interpreter.GetDebugger().GetExecutionContext().process;
708 bool synchronous_execution = interpreter.GetSynchronous ();
Chris Lattner24943d22010-06-08 16:52:24 +0000709
710 if (process == NULL)
711 {
712 result.AppendError ("no process to continue");
713 result.SetStatus (eReturnStatusFailed);
714 return false;
715 }
716
717 StateType state = process->GetState();
718 if (state == eStateStopped)
719 {
720 if (command.GetArgumentCount() != 0)
721 {
722 result.AppendErrorWithFormat ("The '%s' command does not take any arguments.\n", m_cmd_name.c_str());
723 result.SetStatus (eReturnStatusFailed);
724 return false;
725 }
726
727 const uint32_t num_threads = process->GetThreadList().GetSize();
728
729 // Set the actions that the threads should each take when resuming
730 for (uint32_t idx=0; idx<num_threads; ++idx)
731 {
732 process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState (eStateRunning);
733 }
734
735 Error error(process->Resume());
736 if (error.Success())
737 {
738 result.AppendMessageWithFormat ("Resuming process %i\n", process->GetID());
739 if (synchronous_execution)
740 {
Greg Claytonbef15832010-07-14 00:18:15 +0000741 state = process->WaitForProcessToStop (NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000742
743 result.SetDidChangeProcessState (true);
744 result.AppendMessageWithFormat ("Process %i %s\n", process->GetID(), StateAsCString (state));
745 result.SetStatus (eReturnStatusSuccessFinishNoResult);
746 }
747 else
748 {
749 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
750 }
751 }
752 else
753 {
754 result.AppendErrorWithFormat("Failed to resume process: %s.\n", error.AsCString());
755 result.SetStatus (eReturnStatusFailed);
756 }
757 }
758 else
759 {
760 result.AppendErrorWithFormat ("Process cannot be continued from its current state (%s).\n",
761 StateAsCString(state));
762 result.SetStatus (eReturnStatusFailed);
763 }
764 return result.Succeeded();
765 }
766};
767
768//-------------------------------------------------------------------------
769// CommandObjectProcessDetach
770//-------------------------------------------------------------------------
771
772class CommandObjectProcessDetach : public CommandObject
773{
774public:
775
776 CommandObjectProcessDetach () :
777 CommandObject ("process detach",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000778 "Detach from the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +0000779 "process detach",
780 eFlagProcessMustBeLaunched)
781 {
782 }
783
784 ~CommandObjectProcessDetach ()
785 {
786 }
787
788 bool
Greg Clayton63094e02010-06-23 01:19:29 +0000789 Execute (CommandInterpreter &interpreter,
790 Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000791 CommandReturnObject &result)
792 {
Greg Clayton63094e02010-06-23 01:19:29 +0000793 Process *process = interpreter.GetDebugger().GetExecutionContext().process;
Chris Lattner24943d22010-06-08 16:52:24 +0000794 if (process == NULL)
795 {
796 result.AppendError ("must have a valid process in order to detach");
797 result.SetStatus (eReturnStatusFailed);
798 return false;
799 }
800
801 Error error (process->Detach());
802 if (error.Success())
803 {
804 result.SetStatus (eReturnStatusSuccessFinishResult);
805 }
806 else
807 {
808 result.AppendErrorWithFormat ("Detach failed: %s\n", error.AsCString());
809 result.SetStatus (eReturnStatusFailed);
810 return false;
811 }
812 return result.Succeeded();
813 }
814};
815
816//-------------------------------------------------------------------------
817// CommandObjectProcessSignal
818//-------------------------------------------------------------------------
819
820class CommandObjectProcessSignal : public CommandObject
821{
822public:
823
824 CommandObjectProcessSignal () :
825 CommandObject ("process signal",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000826 "Send a UNIX signal to the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +0000827 "process signal <unix-signal-number>")
828 {
829 }
830
831 ~CommandObjectProcessSignal ()
832 {
833 }
834
835 bool
Greg Clayton63094e02010-06-23 01:19:29 +0000836 Execute (CommandInterpreter &interpreter,
837 Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000838 CommandReturnObject &result)
839 {
Greg Clayton63094e02010-06-23 01:19:29 +0000840 Process *process = interpreter.GetDebugger().GetExecutionContext().process;
Chris Lattner24943d22010-06-08 16:52:24 +0000841 if (process == NULL)
842 {
843 result.AppendError ("no process to signal");
844 result.SetStatus (eReturnStatusFailed);
845 return false;
846 }
847
848 if (command.GetArgumentCount() == 1)
849 {
850 int signo = Args::StringToSInt32(command.GetArgumentAtIndex(0), -1, 0);
851 if (signo == -1)
852 {
853 result.AppendErrorWithFormat ("Invalid signal argument '%s'.\n", command.GetArgumentAtIndex(0));
854 result.SetStatus (eReturnStatusFailed);
855 }
856 else
857 {
858 Error error (process->Signal (signo));
859 if (error.Success())
860 {
861 result.SetStatus (eReturnStatusSuccessFinishResult);
862 }
863 else
864 {
865 result.AppendErrorWithFormat ("Failed to send signal %i: %s\n", signo, error.AsCString());
866 result.SetStatus (eReturnStatusFailed);
867 }
868 }
869 }
870 else
871 {
872 result.AppendErrorWithFormat("'%s' takes exactly one signal number argument:\nUsage: \n", m_cmd_name.c_str(),
873 m_cmd_syntax.c_str());
874 result.SetStatus (eReturnStatusFailed);
875 }
876 return result.Succeeded();
877 }
878};
879
880
881//-------------------------------------------------------------------------
882// CommandObjectProcessInterrupt
883//-------------------------------------------------------------------------
884
885class CommandObjectProcessInterrupt : public CommandObject
886{
887public:
888
889
890 CommandObjectProcessInterrupt () :
891 CommandObject ("process interrupt",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000892 "Interrupt the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +0000893 "process interrupt",
894 eFlagProcessMustBeLaunched)
895 {
896 }
897
898 ~CommandObjectProcessInterrupt ()
899 {
900 }
901
902 bool
Greg Clayton63094e02010-06-23 01:19:29 +0000903 Execute (CommandInterpreter &interpreter,
904 Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000905 CommandReturnObject &result)
906 {
Greg Clayton63094e02010-06-23 01:19:29 +0000907 Process *process = interpreter.GetDebugger().GetExecutionContext().process;
Chris Lattner24943d22010-06-08 16:52:24 +0000908 if (process == NULL)
909 {
910 result.AppendError ("no process to halt");
911 result.SetStatus (eReturnStatusFailed);
912 return false;
913 }
914
915 if (command.GetArgumentCount() == 0)
916 {
917 Error error(process->Halt ());
918 if (error.Success())
919 {
920 result.SetStatus (eReturnStatusSuccessFinishResult);
921
922 // Maybe we should add a "SuspendThreadPlans so we
923 // can halt, and keep in place all the current thread plans.
924 process->GetThreadList().DiscardThreadPlans();
925 }
926 else
927 {
928 result.AppendErrorWithFormat ("Failed to halt process: %s\n", error.AsCString());
929 result.SetStatus (eReturnStatusFailed);
930 }
931 }
932 else
933 {
934 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: \n",
935 m_cmd_name.c_str(),
936 m_cmd_syntax.c_str());
937 result.SetStatus (eReturnStatusFailed);
938 }
939 return result.Succeeded();
940 }
941};
942
943//-------------------------------------------------------------------------
944// CommandObjectProcessKill
945//-------------------------------------------------------------------------
946
947class CommandObjectProcessKill : public CommandObject
948{
949public:
950
951 CommandObjectProcessKill () :
952 CommandObject ("process kill",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000953 "Terminate the current process being debugged.",
Chris Lattner24943d22010-06-08 16:52:24 +0000954 "process kill",
955 eFlagProcessMustBeLaunched)
956 {
957 }
958
959 ~CommandObjectProcessKill ()
960 {
961 }
962
963 bool
Greg Clayton63094e02010-06-23 01:19:29 +0000964 Execute (CommandInterpreter &interpreter,
965 Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000966 CommandReturnObject &result)
967 {
Greg Clayton63094e02010-06-23 01:19:29 +0000968 Process *process = interpreter.GetDebugger().GetExecutionContext().process;
Chris Lattner24943d22010-06-08 16:52:24 +0000969 if (process == NULL)
970 {
971 result.AppendError ("no process to kill");
972 result.SetStatus (eReturnStatusFailed);
973 return false;
974 }
975
976 if (command.GetArgumentCount() == 0)
977 {
978 Error error (process->Destroy());
979 if (error.Success())
980 {
981 result.SetStatus (eReturnStatusSuccessFinishResult);
982 }
983 else
984 {
985 result.AppendErrorWithFormat ("Failed to kill process: %s\n", error.AsCString());
986 result.SetStatus (eReturnStatusFailed);
987 }
988 }
989 else
990 {
991 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: \n",
992 m_cmd_name.c_str(),
993 m_cmd_syntax.c_str());
994 result.SetStatus (eReturnStatusFailed);
995 }
996 return result.Succeeded();
997 }
998};
999
1000//-------------------------------------------------------------------------
Jim Ingham41313fc2010-06-18 01:23:09 +00001001// CommandObjectProcessStatus
1002//-------------------------------------------------------------------------
1003class CommandObjectProcessStatus : public CommandObject
1004{
1005public:
1006 CommandObjectProcessStatus () :
Caroline Ticeabb507a2010-09-08 21:06:11 +00001007 CommandObject ("process status",
1008 "Show the current status and location of executing process.",
1009 "process status",
Jim Ingham41313fc2010-06-18 01:23:09 +00001010 0)
1011 {
1012 }
1013
1014 ~CommandObjectProcessStatus()
1015 {
1016 }
1017
1018
1019 bool
1020 Execute
1021 (
Greg Clayton63094e02010-06-23 01:19:29 +00001022 CommandInterpreter &interpreter,
Jim Ingham41313fc2010-06-18 01:23:09 +00001023 Args& command,
Jim Ingham41313fc2010-06-18 01:23:09 +00001024 CommandReturnObject &result
1025 )
1026 {
1027 StreamString &output_stream = result.GetOutputStream();
1028 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Clayton63094e02010-06-23 01:19:29 +00001029 ExecutionContext exe_ctx(interpreter.GetDebugger().GetExecutionContext());
Jim Ingham41313fc2010-06-18 01:23:09 +00001030 if (exe_ctx.process)
1031 {
1032 const StateType state = exe_ctx.process->GetState();
1033 if (StateIsStoppedState(state))
1034 {
1035 if (state == eStateExited)
1036 {
1037 int exit_status = exe_ctx.process->GetExitStatus();
1038 const char *exit_description = exe_ctx.process->GetExitDescription();
1039 output_stream.Printf ("Process %d exited with status = %i (0x%8.8x) %s\n",
1040 exe_ctx.process->GetID(),
1041 exit_status,
1042 exit_status,
1043 exit_description ? exit_description : "");
1044 }
1045 else
1046 {
1047 output_stream.Printf ("Process %d %s\n", exe_ctx.process->GetID(), StateAsCString (state));
1048 if (exe_ctx.thread == NULL)
1049 exe_ctx.thread = exe_ctx.process->GetThreadList().GetThreadAtIndex(0).get();
1050 if (exe_ctx.thread != NULL)
1051 {
1052 DisplayThreadsInfo (interpreter, &exe_ctx, result, true, true);
1053 }
1054 else
1055 {
1056 result.AppendError ("No valid thread found in current process.");
1057 result.SetStatus (eReturnStatusFailed);
1058 }
1059 }
1060 }
1061 else
1062 {
1063 output_stream.Printf ("Process %d is running.\n",
1064 exe_ctx.process->GetID());
1065 }
1066 }
1067 else
1068 {
1069 result.AppendError ("No current location or status available.");
1070 result.SetStatus (eReturnStatusFailed);
1071 }
1072 return result.Succeeded();
1073 }
1074};
1075
1076//-------------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001077// CommandObjectMultiwordProcess
1078//-------------------------------------------------------------------------
1079
Greg Clayton63094e02010-06-23 01:19:29 +00001080CommandObjectMultiwordProcess::CommandObjectMultiwordProcess (CommandInterpreter &interpreter) :
Chris Lattner24943d22010-06-08 16:52:24 +00001081 CommandObjectMultiword ("process",
1082 "A set of commands for operating on a process.",
1083 "process <subcommand> [<subcommand-options>]")
1084{
Greg Clayton63094e02010-06-23 01:19:29 +00001085 LoadSubCommand (interpreter, "attach", CommandObjectSP (new CommandObjectProcessAttach ()));
1086 LoadSubCommand (interpreter, "launch", CommandObjectSP (new CommandObjectProcessLaunch ()));
1087 LoadSubCommand (interpreter, "continue", CommandObjectSP (new CommandObjectProcessContinue ()));
1088 LoadSubCommand (interpreter, "detach", CommandObjectSP (new CommandObjectProcessDetach ()));
1089 LoadSubCommand (interpreter, "signal", CommandObjectSP (new CommandObjectProcessSignal ()));
1090 LoadSubCommand (interpreter, "status", CommandObjectSP (new CommandObjectProcessStatus ()));
1091 LoadSubCommand (interpreter, "interrupt", CommandObjectSP (new CommandObjectProcessInterrupt ()));
1092 LoadSubCommand (interpreter, "kill", CommandObjectSP (new CommandObjectProcessKill ()));
Chris Lattner24943d22010-06-08 16:52:24 +00001093}
1094
1095CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess ()
1096{
1097}
1098