1 //===-- CommandObjectQuit.cpp -----------------------------------*- C++ -*-===//
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.",
23 "quit [exit-code]") {}
25 CommandObjectQuit::~CommandObjectQuit() {}
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
30 bool CommandObjectQuit::ShouldAskForConfirmation(bool &is_a_detach
) {
31 if (!m_interpreter
.GetPromptOnQuit())
33 bool should_prompt
= false;
35 for (uint32_t debugger_idx
= 0; debugger_idx
< Debugger::GetNumDebuggers();
37 DebuggerSP
debugger_sp(Debugger::GetDebuggerAtIndex(debugger_idx
));
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());
44 TargetSP
target_sp(target_list
.GetTargetAtIndex(target_idx
));
47 ProcessSP
process_sp(target_sp
->GetProcessSP());
48 if (process_sp
&& process_sp
->IsValid() && process_sp
->IsAlive() &&
49 process_sp
->WarnBeforeDetach()) {
51 if (!process_sp
->GetShouldDetach()) {
52 // if we need to kill at least one process, just say so and return
62 bool CommandObjectQuit::DoExecute(Args
&command
, CommandReturnObject
&result
) {
63 bool is_a_detach
= true;
64 if (ShouldAskForConfirmation(is_a_detach
)) {
66 message
.Printf("Quitting LLDB will %s one or more processes. Do you really "
68 (is_a_detach
? "detach from" : "kill"));
69 if (!m_interpreter
.Confirm(message
.GetString(), true)) {
70 result
.SetStatus(eReturnStatusFailed
);
75 if (command
.GetArgumentCount() > 1) {
76 result
.AppendError("Too many arguments for 'quit'. Only an optional exit "
78 result
.SetStatus(eReturnStatusFailed
);
82 // We parse the exit code argument if there is one.
83 if (command
.GetArgumentCount() == 1) {
84 llvm::StringRef arg
= command
.GetArgumentAtIndex(0);
86 if (arg
.getAsInteger(/*autodetect radix*/ 0, exit_code
)) {
87 lldb_private::StreamString s
;
88 std::string arg_str
= arg
.str();
89 s
.Printf("Couldn't parse '%s' as integer for exit code.", arg_str
.data());
90 result
.AppendError(s
.GetString());
91 result
.SetStatus(eReturnStatusFailed
);
94 if (!m_interpreter
.SetQuitExitCode(exit_code
)) {
95 result
.AppendError("The current driver doesn't allow custom exit codes"
96 " for the quit command.");
97 result
.SetStatus(eReturnStatusFailed
);
102 const uint32_t event_type
=
103 CommandInterpreter::eBroadcastBitQuitCommandReceived
;
104 m_interpreter
.BroadcastEvent(event_type
);
105 result
.SetStatus(eReturnStatusQuit
);