LLDB  mainline
CommandObjectQuit.cpp
Go to the documentation of this file.
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 
13 #include "lldb/Target/Process.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 
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
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 
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)) {
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");
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)) {
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());
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.");
98  return false;
99  }
100  }
101 
102  const uint32_t event_type =
104  m_interpreter.BroadcastEvent(event_type);
106  return true;
107 }
A command line argument class.
Definition: Args.h:32
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
CommandInterpreter & m_interpreter
size_t GetArgumentCount() const
Gets the number of arguments left in this command object.
Definition: Args.cpp:254
static size_t GetNumDebuggers()
Definition: Debugger.cpp:1157
const char * GetArgumentAtIndex(size_t idx) const
Gets the NULL terminated C string argument pointer for the argument at index idx. ...
Definition: Args.cpp:256
void BroadcastEvent(lldb::EventSP &event_sp)
Broadcast an event which has no associated data.
Definition: Broadcaster.h:283
bool DoExecute(Args &args, CommandReturnObject &result) override
bool Confirm(llvm::StringRef message, bool default_answer)
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
void void AppendError(llvm::StringRef in_string)
Definition: SBAddress.h:15
static lldb::DebuggerSP GetDebuggerAtIndex(size_t index)
Definition: Debugger.cpp:1165
LLVM_NODISCARD bool SetQuitExitCode(int exit_code)
Sets the exit code for the quit command.
void SetStatus(lldb::ReturnStatus status)
bool ShouldAskForConfirmation(bool &is_a_detach)