1ac7ddfbfSEd Maste //===-- CommandObjectQuit.cpp -----------------------------------*- C++ -*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste // The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste
10ac7ddfbfSEd Maste #include "CommandObjectQuit.h"
11ac7ddfbfSEd Maste
12ac7ddfbfSEd Maste #include "lldb/Interpreter/CommandInterpreter.h"
13ac7ddfbfSEd Maste #include "lldb/Interpreter/CommandReturnObject.h"
141c3bbb01SEd Maste #include "lldb/Target/Process.h"
154ba319b5SDimitry Andric #include "lldb/Utility/StreamString.h"
16ac7ddfbfSEd Maste
17ac7ddfbfSEd Maste using namespace lldb;
18ac7ddfbfSEd Maste using namespace lldb_private;
19ac7ddfbfSEd Maste
20ac7ddfbfSEd Maste //-------------------------------------------------------------------------
21ac7ddfbfSEd Maste // CommandObjectQuit
22ac7ddfbfSEd Maste //-------------------------------------------------------------------------
23ac7ddfbfSEd Maste
CommandObjectQuit(CommandInterpreter & interpreter)244bb0738eSEd Maste CommandObjectQuit::CommandObjectQuit(CommandInterpreter &interpreter)
25435933ddSDimitry Andric : CommandObjectParsed(interpreter, "quit", "Quit the LLDB debugger.",
264ba319b5SDimitry Andric "quit [exit-code]") {}
27ac7ddfbfSEd Maste
~CommandObjectQuit()28435933ddSDimitry Andric CommandObjectQuit::~CommandObjectQuit() {}
29ac7ddfbfSEd Maste
304ba319b5SDimitry Andric // returns true if there is at least one alive process is_a_detach will be true
314ba319b5SDimitry Andric // if all alive processes will be detached when you quit and false if at least
324ba319b5SDimitry Andric // one process will be killed instead
ShouldAskForConfirmation(bool & is_a_detach)33435933ddSDimitry Andric bool CommandObjectQuit::ShouldAskForConfirmation(bool &is_a_detach) {
34*b5893f02SDimitry Andric if (!m_interpreter.GetPromptOnQuit())
35ac7ddfbfSEd Maste return false;
36ac7ddfbfSEd Maste bool should_prompt = false;
37ac7ddfbfSEd Maste is_a_detach = true;
38435933ddSDimitry Andric for (uint32_t debugger_idx = 0; debugger_idx < Debugger::GetNumDebuggers();
39435933ddSDimitry Andric debugger_idx++) {
40ac7ddfbfSEd Maste DebuggerSP debugger_sp(Debugger::GetDebuggerAtIndex(debugger_idx));
41ac7ddfbfSEd Maste if (!debugger_sp)
42ac7ddfbfSEd Maste continue;
43ac7ddfbfSEd Maste const TargetList &target_list(debugger_sp->GetTargetList());
44ac7ddfbfSEd Maste for (uint32_t target_idx = 0;
450127ef0fSEd Maste target_idx < static_cast<uint32_t>(target_list.GetNumTargets());
46435933ddSDimitry Andric target_idx++) {
47ac7ddfbfSEd Maste TargetSP target_sp(target_list.GetTargetAtIndex(target_idx));
48ac7ddfbfSEd Maste if (!target_sp)
49ac7ddfbfSEd Maste continue;
50ac7ddfbfSEd Maste ProcessSP process_sp(target_sp->GetProcessSP());
51435933ddSDimitry Andric if (process_sp && process_sp->IsValid() && process_sp->IsAlive() &&
52435933ddSDimitry Andric process_sp->WarnBeforeDetach()) {
53ac7ddfbfSEd Maste should_prompt = true;
54*b5893f02SDimitry Andric if (!process_sp->GetShouldDetach()) {
55ac7ddfbfSEd Maste // if we need to kill at least one process, just say so and return
56ac7ddfbfSEd Maste is_a_detach = false;
57ac7ddfbfSEd Maste return should_prompt;
58ac7ddfbfSEd Maste }
59ac7ddfbfSEd Maste }
60ac7ddfbfSEd Maste }
61ac7ddfbfSEd Maste }
62ac7ddfbfSEd Maste return should_prompt;
63ac7ddfbfSEd Maste }
64ac7ddfbfSEd Maste
DoExecute(Args & command,CommandReturnObject & result)65435933ddSDimitry Andric bool CommandObjectQuit::DoExecute(Args &command, CommandReturnObject &result) {
66ac7ddfbfSEd Maste bool is_a_detach = true;
67435933ddSDimitry Andric if (ShouldAskForConfirmation(is_a_detach)) {
68ac7ddfbfSEd Maste StreamString message;
69435933ddSDimitry Andric message.Printf("Quitting LLDB will %s one or more processes. Do you really "
70435933ddSDimitry Andric "want to proceed",
71435933ddSDimitry Andric (is_a_detach ? "detach from" : "kill"));
72435933ddSDimitry Andric if (!m_interpreter.Confirm(message.GetString(), true)) {
73ac7ddfbfSEd Maste result.SetStatus(eReturnStatusFailed);
74ac7ddfbfSEd Maste return false;
75ac7ddfbfSEd Maste }
76ac7ddfbfSEd Maste }
774ba319b5SDimitry Andric
784ba319b5SDimitry Andric if (command.GetArgumentCount() > 1) {
794ba319b5SDimitry Andric result.AppendError("Too many arguments for 'quit'. Only an optional exit "
804ba319b5SDimitry Andric "code is allowed");
814ba319b5SDimitry Andric result.SetStatus(eReturnStatusFailed);
824ba319b5SDimitry Andric return false;
834ba319b5SDimitry Andric }
844ba319b5SDimitry Andric
854ba319b5SDimitry Andric // We parse the exit code argument if there is one.
864ba319b5SDimitry Andric if (command.GetArgumentCount() == 1) {
874ba319b5SDimitry Andric llvm::StringRef arg = command.GetArgumentAtIndex(0);
884ba319b5SDimitry Andric int exit_code;
894ba319b5SDimitry Andric if (arg.getAsInteger(/*autodetect radix*/ 0, exit_code)) {
904ba319b5SDimitry Andric lldb_private::StreamString s;
914ba319b5SDimitry Andric std::string arg_str = arg.str();
924ba319b5SDimitry Andric s.Printf("Couldn't parse '%s' as integer for exit code.", arg_str.data());
934ba319b5SDimitry Andric result.AppendError(s.GetString());
944ba319b5SDimitry Andric result.SetStatus(eReturnStatusFailed);
954ba319b5SDimitry Andric return false;
964ba319b5SDimitry Andric }
974ba319b5SDimitry Andric if (!m_interpreter.SetQuitExitCode(exit_code)) {
984ba319b5SDimitry Andric result.AppendError("The current driver doesn't allow custom exit codes"
994ba319b5SDimitry Andric " for the quit command.");
1004ba319b5SDimitry Andric result.SetStatus(eReturnStatusFailed);
1014ba319b5SDimitry Andric return false;
1024ba319b5SDimitry Andric }
1034ba319b5SDimitry Andric }
1044ba319b5SDimitry Andric
105435933ddSDimitry Andric const uint32_t event_type =
106435933ddSDimitry Andric CommandInterpreter::eBroadcastBitQuitCommandReceived;
10712b93ac6SEd Maste m_interpreter.BroadcastEvent(event_type);
108ac7ddfbfSEd Maste result.SetStatus(eReturnStatusQuit);
109ac7ddfbfSEd Maste return true;
110ac7ddfbfSEd Maste }
111