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 AddSimpleArgumentList(eArgTypeUnsignedInteger
);
27 CommandObjectQuit::~CommandObjectQuit() = default;
29 // returns true if there is at least one alive process is_a_detach will be true
30 // if all alive processes will be detached when you quit and false if at least
31 // one process will be killed instead
32 bool CommandObjectQuit::ShouldAskForConfirmation(bool &is_a_detach
) {
33 if (!m_interpreter
.GetPromptOnQuit())
35 bool should_prompt
= false;
37 for (uint32_t debugger_idx
= 0; debugger_idx
< Debugger::GetNumDebuggers();
39 DebuggerSP
debugger_sp(Debugger::GetDebuggerAtIndex(debugger_idx
));
42 const TargetList
&target_list(debugger_sp
->GetTargetList());
43 for (uint32_t target_idx
= 0;
44 target_idx
< static_cast<uint32_t>(target_list
.GetNumTargets());
46 TargetSP
target_sp(target_list
.GetTargetAtIndex(target_idx
));
49 ProcessSP
process_sp(target_sp
->GetProcessSP());
50 if (process_sp
&& process_sp
->IsValid() && process_sp
->IsAlive() &&
51 process_sp
->WarnBeforeDetach()) {
53 if (!process_sp
->GetShouldDetach()) {
54 // if we need to kill at least one process, just say so and return
64 void CommandObjectQuit::DoExecute(Args
&command
, CommandReturnObject
&result
) {
65 bool is_a_detach
= true;
66 if (ShouldAskForConfirmation(is_a_detach
)) {
68 message
.Printf("Quitting LLDB will %s one or more processes. Do you really "
70 (is_a_detach
? "detach from" : "kill"));
71 if (!m_interpreter
.Confirm(message
.GetString(), true)) {
72 result
.SetStatus(eReturnStatusFailed
);
77 if (command
.GetArgumentCount() > 1) {
78 result
.AppendError("Too many arguments for 'quit'. Only an optional exit "
83 // We parse the exit code argument if there is one.
84 if (command
.GetArgumentCount() == 1) {
85 llvm::StringRef arg
= command
.GetArgumentAtIndex(0);
87 if (arg
.getAsInteger(/*autodetect radix*/ 0, exit_code
)) {
88 lldb_private::StreamString s
;
89 std::string arg_str
= arg
.str();
90 s
.Printf("Couldn't parse '%s' as integer for exit code.", arg_str
.data());
91 result
.AppendError(s
.GetString());
94 if (!m_interpreter
.SetQuitExitCode(exit_code
)) {
95 result
.AppendError("The current driver doesn't allow custom exit codes"
96 " for the quit command.");
101 const uint32_t event_type
=
102 CommandInterpreter::eBroadcastBitQuitCommandReceived
;
103 m_interpreter
.BroadcastEvent(event_type
);
104 result
.SetStatus(eReturnStatusQuit
);