blob: 37ed12be358f2514292fb8aa57ba3c3709fb61a7 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
Eli Friedman59817b12010-06-09 07:57:51 +000016#include "lldb/Interpreter/CommandInterpreter.h"
17#include "lldb/Interpreter/CommandReturnObject.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000018#include "lldb/Target/Process.h"
Raphael Isemannc094d232018-07-11 17:18:01 +000019#include "lldb/Utility/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020
21using namespace lldb;
22using namespace lldb_private;
23
24//-------------------------------------------------------------------------
25// CommandObjectQuit
26//-------------------------------------------------------------------------
27
Kate Stone7428a182016-07-14 22:03:10 +000028CommandObjectQuit::CommandObjectQuit(CommandInterpreter &interpreter)
Kate Stoneb9c1b512016-09-06 20:57:50 +000029 : CommandObjectParsed(interpreter, "quit", "Quit the LLDB debugger.",
Raphael Isemannc094d232018-07-11 17:18:01 +000030 "quit [exit-code]") {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031
Kate Stoneb9c1b512016-09-06 20:57:50 +000032CommandObjectQuit::~CommandObjectQuit() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033
Adrian Prantl05097242018-04-30 16:49:04 +000034// 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
Kate Stoneb9c1b512016-09-06 20:57:50 +000037bool CommandObjectQuit::ShouldAskForConfirmation(bool &is_a_detach) {
38 if (m_interpreter.GetPromptOnQuit() == false)
39 return false;
40 bool should_prompt = false;
41 is_a_detach = true;
42 for (uint32_t debugger_idx = 0; debugger_idx < Debugger::GetNumDebuggers();
43 debugger_idx++) {
44 DebuggerSP debugger_sp(Debugger::GetDebuggerAtIndex(debugger_idx));
45 if (!debugger_sp)
46 continue;
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());
50 target_idx++) {
51 TargetSP target_sp(target_list.GetTargetAtIndex(target_idx));
52 if (!target_sp)
53 continue;
54 ProcessSP process_sp(target_sp->GetProcessSP());
55 if (process_sp && process_sp->IsValid() && process_sp->IsAlive() &&
56 process_sp->WarnBeforeDetach()) {
57 should_prompt = true;
58 if (process_sp->GetShouldDetach() == false) {
59 // if we need to kill at least one process, just say so and return
60 is_a_detach = false;
61 return should_prompt;
Enrico Granatabcba2b22013-01-17 21:36:19 +000062 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 }
Enrico Granatabcba2b22013-01-17 21:36:19 +000064 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000065 }
66 return should_prompt;
Enrico Granatabcba2b22013-01-17 21:36:19 +000067}
68
Kate Stoneb9c1b512016-09-06 20:57:50 +000069bool CommandObjectQuit::DoExecute(Args &command, CommandReturnObject &result) {
70 bool is_a_detach = true;
71 if (ShouldAskForConfirmation(is_a_detach)) {
72 StreamString message;
73 message.Printf("Quitting LLDB will %s one or more processes. Do you really "
74 "want to proceed",
75 (is_a_detach ? "detach from" : "kill"));
Zachary Turnerc1564272016-11-16 21:15:24 +000076 if (!m_interpreter.Confirm(message.GetString(), true)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000077 result.SetStatus(eReturnStatusFailed);
78 return false;
Enrico Granatabcba2b22013-01-17 21:36:19 +000079 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000080 }
Raphael Isemannc094d232018-07-11 17:18:01 +000081
82 if (command.GetArgumentCount() > 1) {
83 result.AppendError("Too many arguments for 'quit'. Only an optional exit "
84 "code is allowed");
85 result.SetStatus(eReturnStatusFailed);
86 return false;
87 }
88
89 if (command.GetArgumentCount() > 1) {
90 result.AppendError("Too many arguments for 'quit'. Only an optional exit "
91 "code is allowed");
92 result.SetStatus(eReturnStatusFailed);
93 return false;
94 }
95
96 // We parse the exit code argument if there is one.
97 if (command.GetArgumentCount() == 1) {
98 llvm::StringRef arg = command.GetArgumentAtIndex(0);
99 int exit_code;
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);
106 return false;
107 }
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);
112 return false;
113 }
114 }
115
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116 const uint32_t event_type =
117 CommandInterpreter::eBroadcastBitQuitCommandReceived;
118 m_interpreter.BroadcastEvent(event_type);
119 result.SetStatus(eReturnStatusQuit);
120 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000121}