1 //===-- CommandObjectQuit.cpp -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "CommandObjectQuit.h"
14 // Other libraries and framework includes
16 #include "lldb/Interpreter/CommandInterpreter.h"
17 #include "lldb/Interpreter/CommandReturnObject.h"
18 #include "lldb/Target/Process.h"
19 #include "lldb/Utility/StreamString.h"
22 using namespace lldb_private;
24 //-------------------------------------------------------------------------
26 //-------------------------------------------------------------------------
28 CommandObjectQuit::CommandObjectQuit(CommandInterpreter &interpreter)
29 : CommandObjectParsed(interpreter, "quit", "Quit the LLDB debugger.",
30 "quit [exit-code]") {}
32 CommandObjectQuit::~CommandObjectQuit() {}
34 // returns true if there is at least one alive process is_a_detach will be true
35 // if all alive processes will be detached when you quit and false if at least
36 // one process will be killed instead
37 bool CommandObjectQuit::ShouldAskForConfirmation(bool &is_a_detach) {
38 if (m_interpreter.GetPromptOnQuit() == false)
40 bool should_prompt = false;
42 for (uint32_t debugger_idx = 0; debugger_idx < Debugger::GetNumDebuggers();
44 DebuggerSP debugger_sp(Debugger::GetDebuggerAtIndex(debugger_idx));
47 const TargetList &target_list(debugger_sp->GetTargetList());
48 for (uint32_t target_idx = 0;
49 target_idx < static_cast<uint32_t>(target_list.GetNumTargets());
51 TargetSP target_sp(target_list.GetTargetAtIndex(target_idx));
54 ProcessSP process_sp(target_sp->GetProcessSP());
55 if (process_sp && process_sp->IsValid() && process_sp->IsAlive() &&
56 process_sp->WarnBeforeDetach()) {
58 if (process_sp->GetShouldDetach() == false) {
59 // if we need to kill at least one process, just say so and return
69 bool CommandObjectQuit::DoExecute(Args &command, CommandReturnObject &result) {
70 bool is_a_detach = true;
71 if (ShouldAskForConfirmation(is_a_detach)) {
73 message.Printf("Quitting LLDB will %s one or more processes. Do you really "
75 (is_a_detach ? "detach from" : "kill"));
76 if (!m_interpreter.Confirm(message.GetString(), true)) {
77 result.SetStatus(eReturnStatusFailed);
82 if (command.GetArgumentCount() > 1) {
83 result.AppendError("Too many arguments for 'quit'. Only an optional exit "
85 result.SetStatus(eReturnStatusFailed);
89 if (command.GetArgumentCount() > 1) {
90 result.AppendError("Too many arguments for 'quit'. Only an optional exit "
92 result.SetStatus(eReturnStatusFailed);
96 // We parse the exit code argument if there is one.
97 if (command.GetArgumentCount() == 1) {
98 llvm::StringRef arg = command.GetArgumentAtIndex(0);
100 if (arg.getAsInteger(/*autodetect radix*/ 0, exit_code)) {
101 lldb_private::StreamString s;
102 std::string arg_str = arg.str();
103 s.Printf("Couldn't parse '%s' as integer for exit code.", arg_str.data());
104 result.AppendError(s.GetString());
105 result.SetStatus(eReturnStatusFailed);
108 if (!m_interpreter.SetQuitExitCode(exit_code)) {
109 result.AppendError("The current driver doesn't allow custom exit codes"
110 " for the quit command.");
111 result.SetStatus(eReturnStatusFailed);
116 const uint32_t event_type =
117 CommandInterpreter::eBroadcastBitQuitCommandReceived;
118 m_interpreter.BroadcastEvent(event_type);
119 result.SetStatus(eReturnStatusQuit);