Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- CommandObjectQuit.cpp -----------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "CommandObjectQuit.h" |
| 11 | |
Eli Friedman | 59817b1 | 2010-06-09 07:57:51 +0000 | [diff] [blame] | 12 | #include "lldb/Interpreter/CommandInterpreter.h" |
| 13 | #include "lldb/Interpreter/CommandReturnObject.h" |
Zachary Turner | a78bd7f | 2015-03-03 23:11:11 +0000 | [diff] [blame] | 14 | #include "lldb/Target/Process.h" |
Raphael Isemann | c094d23 | 2018-07-11 17:18:01 +0000 | [diff] [blame] | 15 | #include "lldb/Utility/StreamString.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | |
| 17 | using namespace lldb; |
| 18 | using namespace lldb_private; |
| 19 | |
| 20 | //------------------------------------------------------------------------- |
| 21 | // CommandObjectQuit |
| 22 | //------------------------------------------------------------------------- |
| 23 | |
Kate Stone | 7428a18 | 2016-07-14 22:03:10 +0000 | [diff] [blame] | 24 | CommandObjectQuit::CommandObjectQuit(CommandInterpreter &interpreter) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 25 | : CommandObjectParsed(interpreter, "quit", "Quit the LLDB debugger.", |
Raphael Isemann | c094d23 | 2018-07-11 17:18:01 +0000 | [diff] [blame] | 26 | "quit [exit-code]") {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 28 | CommandObjectQuit::~CommandObjectQuit() {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 30 | // returns true if there is at least one alive process is_a_detach will be true |
| 31 | // if all alive processes will be detached when you quit and false if at least |
| 32 | // one process will be killed instead |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 33 | bool CommandObjectQuit::ShouldAskForConfirmation(bool &is_a_detach) { |
| 34 | if (m_interpreter.GetPromptOnQuit() == false) |
| 35 | return false; |
| 36 | bool should_prompt = false; |
| 37 | is_a_detach = true; |
| 38 | for (uint32_t debugger_idx = 0; debugger_idx < Debugger::GetNumDebuggers(); |
| 39 | debugger_idx++) { |
| 40 | DebuggerSP debugger_sp(Debugger::GetDebuggerAtIndex(debugger_idx)); |
| 41 | if (!debugger_sp) |
| 42 | continue; |
| 43 | const TargetList &target_list(debugger_sp->GetTargetList()); |
| 44 | for (uint32_t target_idx = 0; |
| 45 | target_idx < static_cast<uint32_t>(target_list.GetNumTargets()); |
| 46 | target_idx++) { |
| 47 | TargetSP target_sp(target_list.GetTargetAtIndex(target_idx)); |
| 48 | if (!target_sp) |
| 49 | continue; |
| 50 | ProcessSP process_sp(target_sp->GetProcessSP()); |
| 51 | if (process_sp && process_sp->IsValid() && process_sp->IsAlive() && |
| 52 | process_sp->WarnBeforeDetach()) { |
| 53 | should_prompt = true; |
| 54 | if (process_sp->GetShouldDetach() == false) { |
| 55 | // if we need to kill at least one process, just say so and return |
| 56 | is_a_detach = false; |
| 57 | return should_prompt; |
Enrico Granata | bcba2b2 | 2013-01-17 21:36:19 +0000 | [diff] [blame] | 58 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 59 | } |
Enrico Granata | bcba2b2 | 2013-01-17 21:36:19 +0000 | [diff] [blame] | 60 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 61 | } |
| 62 | return should_prompt; |
Enrico Granata | bcba2b2 | 2013-01-17 21:36:19 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 65 | bool CommandObjectQuit::DoExecute(Args &command, CommandReturnObject &result) { |
| 66 | bool is_a_detach = true; |
| 67 | if (ShouldAskForConfirmation(is_a_detach)) { |
| 68 | StreamString message; |
| 69 | message.Printf("Quitting LLDB will %s one or more processes. Do you really " |
| 70 | "want to proceed", |
| 71 | (is_a_detach ? "detach from" : "kill")); |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 72 | if (!m_interpreter.Confirm(message.GetString(), true)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 73 | result.SetStatus(eReturnStatusFailed); |
| 74 | return false; |
Enrico Granata | bcba2b2 | 2013-01-17 21:36:19 +0000 | [diff] [blame] | 75 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 76 | } |
Raphael Isemann | c094d23 | 2018-07-11 17:18:01 +0000 | [diff] [blame] | 77 | |
| 78 | if (command.GetArgumentCount() > 1) { |
| 79 | result.AppendError("Too many arguments for 'quit'. Only an optional exit " |
| 80 | "code is allowed"); |
| 81 | result.SetStatus(eReturnStatusFailed); |
| 82 | return false; |
| 83 | } |
| 84 | |
Raphael Isemann | c094d23 | 2018-07-11 17:18:01 +0000 | [diff] [blame] | 85 | // We parse the exit code argument if there is one. |
| 86 | if (command.GetArgumentCount() == 1) { |
| 87 | llvm::StringRef arg = command.GetArgumentAtIndex(0); |
| 88 | int exit_code; |
| 89 | if (arg.getAsInteger(/*autodetect radix*/ 0, exit_code)) { |
| 90 | lldb_private::StreamString s; |
| 91 | std::string arg_str = arg.str(); |
| 92 | s.Printf("Couldn't parse '%s' as integer for exit code.", arg_str.data()); |
| 93 | result.AppendError(s.GetString()); |
| 94 | result.SetStatus(eReturnStatusFailed); |
| 95 | return false; |
| 96 | } |
| 97 | if (!m_interpreter.SetQuitExitCode(exit_code)) { |
| 98 | result.AppendError("The current driver doesn't allow custom exit codes" |
| 99 | " for the quit command."); |
| 100 | result.SetStatus(eReturnStatusFailed); |
| 101 | return false; |
| 102 | } |
| 103 | } |
| 104 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 105 | const uint32_t event_type = |
| 106 | CommandInterpreter::eBroadcastBitQuitCommandReceived; |
| 107 | m_interpreter.BroadcastEvent(event_type); |
| 108 | result.SetStatus(eReturnStatusQuit); |
| 109 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 110 | } |