LLDB  mainline
CommandObjectQuit.cpp
Go to the documentation of this file.
1 //===-- CommandObjectQuit.cpp ---------------------------------------------===//
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)) {
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");
78  return false;
79  }
80 
81  // We parse the exit code argument if there is one.
82  if (command.GetArgumentCount() == 1) {
83  llvm::StringRef arg = command.GetArgumentAtIndex(0);
84  int exit_code;
85  if (arg.getAsInteger(/*autodetect radix*/ 0, exit_code)) {
87  std::string arg_str = arg.str();
88  s.Printf("Couldn't parse '%s' as integer for exit code.", arg_str.data());
89  result.AppendError(s.GetString());
90  return false;
91  }
92  if (!m_interpreter.SetQuitExitCode(exit_code)) {
93  result.AppendError("The current driver doesn't allow custom exit codes"
94  " for the quit command.");
95  return false;
96  }
97  }
98 
99  const uint32_t event_type =
101  m_interpreter.BroadcastEvent(event_type);
103 
104  return true;
105 }
lldb_private::CommandInterpreter::GetPromptOnQuit
bool GetPromptOnQuit() const
Definition: CommandInterpreter.cpp:141
lldb_private::CommandObjectParsed
Definition: CommandObject.h:378
CommandObjectQuit.h
lldb_private::Debugger::GetDebuggerAtIndex
static lldb::DebuggerSP GetDebuggerAtIndex(size_t index)
Definition: Debugger.cpp:1077
lldb_private::TargetList
Definition: TargetList.h:21
lldb_private::Args
Definition: Args.h:33
CommandReturnObject.h
lldb_private::StreamString::GetString
llvm::StringRef GetString() const
Definition: StreamString.cpp:51
Process.h
lldb_private::CommandInterpreter::eBroadcastBitQuitCommandReceived
@ eBroadcastBitQuitCommandReceived
Definition: CommandInterpreter.h:221
lldb_private::CommandReturnObject::SetStatus
void SetStatus(lldb::ReturnStatus status)
Definition: CommandReturnObject.cpp:121
lldb_private::CommandInterpreter::SetQuitExitCode
LLVM_NODISCARD bool SetQuitExitCode(int exit_code)
Sets the exit code for the quit command.
Definition: CommandInterpreter.cpp:201
lldb_private::CommandInterpreter::Confirm
bool Confirm(llvm::StringRef message, bool default_answer)
StreamString.h
lldb_private::StreamString
Definition: StreamString.h:23
lldb_private::CommandInterpreter
Definition: CommandInterpreter.h:214
lldb_private::TargetList::GetNumTargets
int GetNumTargets() const
Definition: TargetList.cpp:530
lldb::eReturnStatusFailed
@ eReturnStatusFailed
Definition: lldb-enumerations.h:265
lldb_private::CommandObjectQuit::ShouldAskForConfirmation
bool ShouldAskForConfirmation(bool &is_a_detach)
Definition: CommandObjectQuit.cpp:30
lldb_private::CommandObject::m_interpreter
CommandInterpreter & m_interpreter
Definition: CommandObject.h:358
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:38
lldb_private::Broadcaster::BroadcastEvent
void BroadcastEvent(lldb::EventSP &event_sp)
Broadcast an event which has no associated data.
Definition: Broadcaster.h:262
lldb_private::Debugger::GetNumDebuggers
static size_t GetNumDebuggers()
Definition: Debugger.cpp:1069
lldb_private::Args::GetArgumentAtIndex
const char * GetArgumentAtIndex(size_t idx) const
Gets the NULL terminated C string argument pointer for the argument at index idx.
Definition: Args.cpp:259
lldb_private::CommandObjectQuit::DoExecute
bool DoExecute(Args &args, CommandReturnObject &result) override
Definition: CommandObjectQuit.cpp:62
lldb_private::CommandReturnObject
Definition: CommandReturnObject.h:25
message
message(FATAL_ERROR "invalid libipt include path provided") endif() include_directories($
Definition: Plugins/Trace/intel-pt/CMakeLists.txt:6
uint32_t
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::CommandObjectQuit::~CommandObjectQuit
~CommandObjectQuit() override
lldb_private::TargetList::GetTargetAtIndex
lldb::TargetSP GetTargetAtIndex(uint32_t index) const
Definition: TargetList.cpp:535
CommandInterpreter.h
lldb::eReturnStatusQuit
@ eReturnStatusQuit
Definition: lldb-enumerations.h:266
lldb_private::CommandReturnObject::AppendError
void void AppendError(llvm::StringRef in_string)
Definition: CommandReturnObject.cpp:100
lldb_private::Args::GetArgumentCount
size_t GetArgumentCount() const
Gets the number of arguments left in this command object.
Definition: Args.h:118
lldb
Definition: SBAddress.h:15