1dda28197Spatrick //===-- CommandObjectQuit.cpp ---------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick
9061da546Spatrick #include "CommandObjectQuit.h"
10061da546Spatrick
11061da546Spatrick #include "lldb/Interpreter/CommandInterpreter.h"
12061da546Spatrick #include "lldb/Interpreter/CommandReturnObject.h"
13061da546Spatrick #include "lldb/Target/Process.h"
14061da546Spatrick #include "lldb/Utility/StreamString.h"
15061da546Spatrick
16061da546Spatrick using namespace lldb;
17061da546Spatrick using namespace lldb_private;
18061da546Spatrick
19061da546Spatrick // CommandObjectQuit
20061da546Spatrick
CommandObjectQuit(CommandInterpreter & interpreter)21061da546Spatrick CommandObjectQuit::CommandObjectQuit(CommandInterpreter &interpreter)
22061da546Spatrick : CommandObjectParsed(interpreter, "quit", "Quit the LLDB debugger.",
23*f6aab3d8Srobert "quit [exit-code]") {
24*f6aab3d8Srobert CommandArgumentData exit_code_arg{eArgTypeUnsignedInteger, eArgRepeatPlain};
25*f6aab3d8Srobert m_arguments.push_back({exit_code_arg});
26*f6aab3d8Srobert }
27061da546Spatrick
28be691f3bSpatrick CommandObjectQuit::~CommandObjectQuit() = default;
29061da546Spatrick
30061da546Spatrick // returns true if there is at least one alive process is_a_detach will be true
31061da546Spatrick // if all alive processes will be detached when you quit and false if at least
32061da546Spatrick // one process will be killed instead
ShouldAskForConfirmation(bool & is_a_detach)33061da546Spatrick bool CommandObjectQuit::ShouldAskForConfirmation(bool &is_a_detach) {
34061da546Spatrick if (!m_interpreter.GetPromptOnQuit())
35061da546Spatrick return false;
36061da546Spatrick bool should_prompt = false;
37061da546Spatrick is_a_detach = true;
38061da546Spatrick for (uint32_t debugger_idx = 0; debugger_idx < Debugger::GetNumDebuggers();
39061da546Spatrick debugger_idx++) {
40061da546Spatrick DebuggerSP debugger_sp(Debugger::GetDebuggerAtIndex(debugger_idx));
41061da546Spatrick if (!debugger_sp)
42061da546Spatrick continue;
43061da546Spatrick const TargetList &target_list(debugger_sp->GetTargetList());
44061da546Spatrick for (uint32_t target_idx = 0;
45061da546Spatrick target_idx < static_cast<uint32_t>(target_list.GetNumTargets());
46061da546Spatrick target_idx++) {
47061da546Spatrick TargetSP target_sp(target_list.GetTargetAtIndex(target_idx));
48061da546Spatrick if (!target_sp)
49061da546Spatrick continue;
50061da546Spatrick ProcessSP process_sp(target_sp->GetProcessSP());
51061da546Spatrick if (process_sp && process_sp->IsValid() && process_sp->IsAlive() &&
52061da546Spatrick process_sp->WarnBeforeDetach()) {
53061da546Spatrick should_prompt = true;
54061da546Spatrick if (!process_sp->GetShouldDetach()) {
55061da546Spatrick // if we need to kill at least one process, just say so and return
56061da546Spatrick is_a_detach = false;
57061da546Spatrick return should_prompt;
58061da546Spatrick }
59061da546Spatrick }
60061da546Spatrick }
61061da546Spatrick }
62061da546Spatrick return should_prompt;
63061da546Spatrick }
64061da546Spatrick
DoExecute(Args & command,CommandReturnObject & result)65061da546Spatrick bool CommandObjectQuit::DoExecute(Args &command, CommandReturnObject &result) {
66061da546Spatrick bool is_a_detach = true;
67061da546Spatrick if (ShouldAskForConfirmation(is_a_detach)) {
68061da546Spatrick StreamString message;
69061da546Spatrick message.Printf("Quitting LLDB will %s one or more processes. Do you really "
70061da546Spatrick "want to proceed",
71061da546Spatrick (is_a_detach ? "detach from" : "kill"));
72061da546Spatrick if (!m_interpreter.Confirm(message.GetString(), true)) {
73061da546Spatrick result.SetStatus(eReturnStatusFailed);
74061da546Spatrick return false;
75061da546Spatrick }
76061da546Spatrick }
77061da546Spatrick
78061da546Spatrick if (command.GetArgumentCount() > 1) {
79061da546Spatrick result.AppendError("Too many arguments for 'quit'. Only an optional exit "
80061da546Spatrick "code is allowed");
81061da546Spatrick return false;
82061da546Spatrick }
83061da546Spatrick
84061da546Spatrick // We parse the exit code argument if there is one.
85061da546Spatrick if (command.GetArgumentCount() == 1) {
86061da546Spatrick llvm::StringRef arg = command.GetArgumentAtIndex(0);
87061da546Spatrick int exit_code;
88061da546Spatrick if (arg.getAsInteger(/*autodetect radix*/ 0, exit_code)) {
89061da546Spatrick lldb_private::StreamString s;
90061da546Spatrick std::string arg_str = arg.str();
91061da546Spatrick s.Printf("Couldn't parse '%s' as integer for exit code.", arg_str.data());
92061da546Spatrick result.AppendError(s.GetString());
93061da546Spatrick return false;
94061da546Spatrick }
95061da546Spatrick if (!m_interpreter.SetQuitExitCode(exit_code)) {
96061da546Spatrick result.AppendError("The current driver doesn't allow custom exit codes"
97061da546Spatrick " for the quit command.");
98061da546Spatrick return false;
99061da546Spatrick }
100061da546Spatrick }
101061da546Spatrick
102061da546Spatrick const uint32_t event_type =
103061da546Spatrick CommandInterpreter::eBroadcastBitQuitCommandReceived;
104061da546Spatrick m_interpreter.BroadcastEvent(event_type);
105061da546Spatrick result.SetStatus(eReturnStatusQuit);
106be691f3bSpatrick
107061da546Spatrick return true;
108061da546Spatrick }
109