1 //===-- CommandObjectQuit.cpp ---------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "CommandObjectQuit.h"
11 #include "lldb/Interpreter/CommandInterpreter.h"
12 #include "lldb/Interpreter/CommandReturnObject.h"
13 #include "lldb/Target/Process.h"
14 #include "lldb/Utility/StreamString.h"
17 using namespace lldb_private
;
21 CommandObjectQuit::CommandObjectQuit(CommandInterpreter
&interpreter
)
22 : CommandObjectParsed(interpreter
, "quit", "Quit the LLDB debugger.",
24 CommandArgumentData exit_code_arg
{eArgTypeUnsignedInteger
, eArgRepeatPlain
};
25 m_arguments
.push_back({exit_code_arg
});
28 CommandObjectQuit::~CommandObjectQuit() = default;
30 // returns true if there is at least one alive process is_a_detach will be true
31 // if all alive processes will be detached when you quit and false if at least
32 // one process will be killed instead
33 bool CommandObjectQuit::ShouldAskForConfirmation(bool &is_a_detach
) {
34 if (!m_interpreter
.GetPromptOnQuit())
36 bool should_prompt
= false;
38 for (uint32_t debugger_idx
= 0; debugger_idx
< Debugger::GetNumDebuggers();
40 DebuggerSP
debugger_sp(Debugger::GetDebuggerAtIndex(debugger_idx
));
43 const TargetList
&target_list(debugger_sp
->GetTargetList());
44 for (uint32_t target_idx
= 0;
45 target_idx
< static_cast<uint32_t>(target_list
.GetNumTargets());
47 TargetSP
target_sp(target_list
.GetTargetAtIndex(target_idx
));
50 ProcessSP
process_sp(target_sp
->GetProcessSP());
51 if (process_sp
&& process_sp
->IsValid() && process_sp
->IsAlive() &&
52 process_sp
->WarnBeforeDetach()) {
54 if (!process_sp
->GetShouldDetach()) {
55 // if we need to kill at least one process, just say so and return
65 void CommandObjectQuit::DoExecute(Args
&command
, CommandReturnObject
&result
) {
66 bool is_a_detach
= true;
67 if (ShouldAskForConfirmation(is_a_detach
)) {
69 message
.Printf("Quitting LLDB will %s one or more processes. Do you really "
71 (is_a_detach
? "detach from" : "kill"));
72 if (!m_interpreter
.Confirm(message
.GetString(), true)) {
73 result
.SetStatus(eReturnStatusFailed
);
78 if (command
.GetArgumentCount() > 1) {
79 result
.AppendError("Too many arguments for 'quit'. Only an optional exit "
84 // We parse the exit code argument if there is one.
85 if (command
.GetArgumentCount() == 1) {
86 llvm::StringRef arg
= command
.GetArgumentAtIndex(0);
88 if (arg
.getAsInteger(/*autodetect radix*/ 0, exit_code
)) {
89 lldb_private::StreamString s
;
90 std::string arg_str
= arg
.str();
91 s
.Printf("Couldn't parse '%s' as integer for exit code.", arg_str
.data());
92 result
.AppendError(s
.GetString());
95 if (!m_interpreter
.SetQuitExitCode(exit_code
)) {
96 result
.AppendError("The current driver doesn't allow custom exit codes"
97 " for the quit command.");
102 const uint32_t event_type
=
103 CommandInterpreter::eBroadcastBitQuitCommandReceived
;
104 m_interpreter
.BroadcastEvent(event_type
);
105 result
.SetStatus(eReturnStatusQuit
);