1 //===-- CommandObjectQuit.cpp ---------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "CommandObjectQuit.h"
10 
11 #include "lldb/Interpreter/CommandInterpreter.h"
12 #include "lldb/Interpreter/CommandReturnObject.h"
13 #include "lldb/Target/Process.h"
14 #include "lldb/Utility/StreamString.h"
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 
19 // CommandObjectQuit
20 
CommandObjectQuit(CommandInterpreter & interpreter)21 CommandObjectQuit::CommandObjectQuit(CommandInterpreter &interpreter)
22     : CommandObjectParsed(interpreter, "quit", "Quit the LLDB debugger.",
23                           "quit [exit-code]") {}
24 
25 CommandObjectQuit::~CommandObjectQuit() = default;
26 
27 // returns true if there is at least one alive process is_a_detach will be true
28 // if all alive processes will be detached when you quit and false if at least
29 // one process will be killed instead
ShouldAskForConfirmation(bool & is_a_detach)30 bool CommandObjectQuit::ShouldAskForConfirmation(bool &is_a_detach) {
31   if (!m_interpreter.GetPromptOnQuit())
32     return false;
33   bool should_prompt = false;
34   is_a_detach = true;
35   for (uint32_t debugger_idx = 0; debugger_idx < Debugger::GetNumDebuggers();
36        debugger_idx++) {
37     DebuggerSP debugger_sp(Debugger::GetDebuggerAtIndex(debugger_idx));
38     if (!debugger_sp)
39       continue;
40     const TargetList &target_list(debugger_sp->GetTargetList());
41     for (uint32_t target_idx = 0;
42          target_idx < static_cast<uint32_t>(target_list.GetNumTargets());
43          target_idx++) {
44       TargetSP target_sp(target_list.GetTargetAtIndex(target_idx));
45       if (!target_sp)
46         continue;
47       ProcessSP process_sp(target_sp->GetProcessSP());
48       if (process_sp && process_sp->IsValid() && process_sp->IsAlive() &&
49           process_sp->WarnBeforeDetach()) {
50         should_prompt = true;
51         if (!process_sp->GetShouldDetach()) {
52           // if we need to kill at least one process, just say so and return
53           is_a_detach = false;
54           return should_prompt;
55         }
56       }
57     }
58   }
59   return should_prompt;
60 }
61 
DoExecute(Args & command,CommandReturnObject & result)62 bool CommandObjectQuit::DoExecute(Args &command, CommandReturnObject &result) {
63   bool is_a_detach = true;
64   if (ShouldAskForConfirmation(is_a_detach)) {
65     StreamString message;
66     message.Printf("Quitting LLDB will %s one or more processes. Do you really "
67                    "want to proceed",
68                    (is_a_detach ? "detach from" : "kill"));
69     if (!m_interpreter.Confirm(message.GetString(), true)) {
70       result.SetStatus(eReturnStatusFailed);
71       return false;
72     }
73   }
74 
75   if (command.GetArgumentCount() > 1) {
76     result.AppendError("Too many arguments for 'quit'. Only an optional exit "
77                        "code is allowed");
78     return false;
79   }
80 
81   // We parse the exit code argument if there is one.
82   if (command.GetArgumentCount() == 1) {
83     llvm::StringRef arg = command.GetArgumentAtIndex(0);
84     int exit_code;
85     if (arg.getAsInteger(/*autodetect radix*/ 0, exit_code)) {
86       lldb_private::StreamString s;
87       std::string arg_str = arg.str();
88       s.Printf("Couldn't parse '%s' as integer for exit code.", arg_str.data());
89       result.AppendError(s.GetString());
90       return false;
91     }
92     if (!m_interpreter.SetQuitExitCode(exit_code)) {
93       result.AppendError("The current driver doesn't allow custom exit codes"
94                          " for the quit command.");
95       return false;
96     }
97   }
98 
99   const uint32_t event_type =
100       CommandInterpreter::eBroadcastBitQuitCommandReceived;
101   m_interpreter.BroadcastEvent(event_type);
102   result.SetStatus(eReturnStatusQuit);
103 
104   return true;
105 }
106