1 //===-- CommandObjectQuit.cpp -----------------------------------*- C++ -*-===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "CommandObjectQuit.h" 10 11 #include "lldb/Interpreter/CommandInterpreter.h" 12 #include "lldb/Interpreter/CommandReturnObject.h" 13 #include "lldb/Target/Process.h" 14 #include "lldb/Utility/StreamString.h" 15 16 using namespace lldb; 17 using namespace lldb_private; 18 19 // CommandObjectQuit 20 21 CommandObjectQuit::CommandObjectQuit(CommandInterpreter &interpreter) 22 : CommandObjectParsed(interpreter, "quit", "Quit the LLDB debugger.", 23 "quit [exit-code]") {} 24 25 CommandObjectQuit::~CommandObjectQuit() {} 26 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()) 32 return false; 33 bool should_prompt = false; 34 is_a_detach = true; 35 for (uint32_t debugger_idx = 0; debugger_idx < Debugger::GetNumDebuggers(); 36 debugger_idx++) { 37 DebuggerSP debugger_sp(Debugger::GetDebuggerAtIndex(debugger_idx)); 38 if (!debugger_sp) 39 continue; 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()); 43 target_idx++) { 44 TargetSP target_sp(target_list.GetTargetAtIndex(target_idx)); 45 if (!target_sp) 46 continue; 47 ProcessSP process_sp(target_sp->GetProcessSP()); 48 if (process_sp && process_sp->IsValid() && process_sp->IsAlive() && 49 process_sp->WarnBeforeDetach()) { 50 should_prompt = true; 51 if (!process_sp->GetShouldDetach()) { 52 // if we need to kill at least one process, just say so and return 53 is_a_detach = false; 54 return should_prompt; 55 } 56 } 57 } 58 } 59 return should_prompt; 60 } 61 62 bool CommandObjectQuit::DoExecute(Args &command, CommandReturnObject &result) { 63 bool is_a_detach = true; 64 if (ShouldAskForConfirmation(is_a_detach)) { 65 StreamString message; 66 message.Printf("Quitting LLDB will %s one or more processes. Do you really " 67 "want to proceed", 68 (is_a_detach ? "detach from" : "kill")); 69 if (!m_interpreter.Confirm(message.GetString(), true)) { 70 result.SetStatus(eReturnStatusFailed); 71 return false; 72 } 73 } 74 75 if (command.GetArgumentCount() > 1) { 76 result.AppendError("Too many arguments for 'quit'. Only an optional exit " 77 "code is allowed"); 78 result.SetStatus(eReturnStatusFailed); 79 return false; 80 } 81 82 // We parse the exit code argument if there is one. 83 if (command.GetArgumentCount() == 1) { 84 llvm::StringRef arg = command.GetArgumentAtIndex(0); 85 int exit_code; 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); 92 return false; 93 } 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); 98 return false; 99 } 100 } 101 102 const uint32_t event_type = 103 CommandInterpreter::eBroadcastBitQuitCommandReceived; 104 m_interpreter.BroadcastEvent(event_type); 105 result.SetStatus(eReturnStatusQuit); 106 return true; 107 } 108