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