blob: 071deba6c5a04efd61d626ac256344a3422983b9 [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"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019
20using namespace lldb;
21using namespace lldb_private;
22
23//-------------------------------------------------------------------------
24// CommandObjectQuit
25//-------------------------------------------------------------------------
26
Kate Stone7428a182016-07-14 22:03:10 +000027CommandObjectQuit::CommandObjectQuit(CommandInterpreter &interpreter)
Kate Stoneb9c1b512016-09-06 20:57:50 +000028 : CommandObjectParsed(interpreter, "quit", "Quit the LLDB debugger.",
29 "quit") {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030
Kate Stoneb9c1b512016-09-06 20:57:50 +000031CommandObjectQuit::~CommandObjectQuit() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032
Adrian Prantl05097242018-04-30 16:49:04 +000033// returns true if there is at least one alive process is_a_detach will be true
34// if all alive processes will be detached when you quit and false if at least
35// one process will be killed instead
Kate Stoneb9c1b512016-09-06 20:57:50 +000036bool CommandObjectQuit::ShouldAskForConfirmation(bool &is_a_detach) {
37 if (m_interpreter.GetPromptOnQuit() == false)
38 return false;
39 bool should_prompt = false;
40 is_a_detach = true;
41 for (uint32_t debugger_idx = 0; debugger_idx < Debugger::GetNumDebuggers();
42 debugger_idx++) {
43 DebuggerSP debugger_sp(Debugger::GetDebuggerAtIndex(debugger_idx));
44 if (!debugger_sp)
45 continue;
46 const TargetList &target_list(debugger_sp->GetTargetList());
47 for (uint32_t target_idx = 0;
48 target_idx < static_cast<uint32_t>(target_list.GetNumTargets());
49 target_idx++) {
50 TargetSP target_sp(target_list.GetTargetAtIndex(target_idx));
51 if (!target_sp)
52 continue;
53 ProcessSP process_sp(target_sp->GetProcessSP());
54 if (process_sp && process_sp->IsValid() && process_sp->IsAlive() &&
55 process_sp->WarnBeforeDetach()) {
56 should_prompt = true;
57 if (process_sp->GetShouldDetach() == false) {
58 // if we need to kill at least one process, just say so and return
59 is_a_detach = false;
60 return should_prompt;
Enrico Granatabcba2b22013-01-17 21:36:19 +000061 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 }
Enrico Granatabcba2b22013-01-17 21:36:19 +000063 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 }
65 return should_prompt;
Enrico Granatabcba2b22013-01-17 21:36:19 +000066}
67
Kate Stoneb9c1b512016-09-06 20:57:50 +000068bool CommandObjectQuit::DoExecute(Args &command, CommandReturnObject &result) {
69 bool is_a_detach = true;
70 if (ShouldAskForConfirmation(is_a_detach)) {
71 StreamString message;
72 message.Printf("Quitting LLDB will %s one or more processes. Do you really "
73 "want to proceed",
74 (is_a_detach ? "detach from" : "kill"));
Zachary Turnerc1564272016-11-16 21:15:24 +000075 if (!m_interpreter.Confirm(message.GetString(), true)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000076 result.SetStatus(eReturnStatusFailed);
77 return false;
Enrico Granatabcba2b22013-01-17 21:36:19 +000078 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000079 }
80 const uint32_t event_type =
81 CommandInterpreter::eBroadcastBitQuitCommandReceived;
82 m_interpreter.BroadcastEvent(event_type);
83 result.SetStatus(eReturnStatusQuit);
84 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085}