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
16using namespace lldb;
17using namespace lldb_private;
18
19// CommandObjectQuit
20
22 : CommandObjectParsed(interpreter, "quit", "Quit the LLDB debugger.",
23 "quit [exit-code]") {
25 m_arguments.push_back({exit_code_arg});
26}
27
29
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
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()) {
55 // if we need to kill at least one process, just say so and return
56 is_a_detach = false;
57 return should_prompt;
58 }
59 }
60 }
61 }
62 return should_prompt;
63}
64
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"));
72 if (!m_interpreter.Confirm(message.GetString(), true)) {
74 return false;
75 }
76 }
77
78 if (command.GetArgumentCount() > 1) {
79 result.AppendError("Too many arguments for 'quit'. Only an optional exit "
80 "code is allowed");
81 return false;
82 }
83
84 // We parse the exit code argument if there is one.
85 if (command.GetArgumentCount() == 1) {
86 llvm::StringRef arg = command.GetArgumentAtIndex(0);
87 int exit_code;
88 if (arg.getAsInteger(/*autodetect radix*/ 0, exit_code)) {
90 std::string arg_str = arg.str();
91 s.Printf("Couldn't parse '%s' as integer for exit code.", arg_str.data());
92 result.AppendError(s.GetString());
93 return false;
94 }
95 if (!m_interpreter.SetQuitExitCode(exit_code)) {
96 result.AppendError("The current driver doesn't allow custom exit codes"
97 " for the quit command.");
98 return false;
99 }
100 }
101
102 const uint32_t event_type =
104 m_interpreter.BroadcastEvent(event_type);
106
107 return true;
108}
A command line argument class.
Definition: Args.h:33
size_t GetArgumentCount() const
Gets the number of arguments left in this command object.
Definition: Args.h:116
const char * GetArgumentAtIndex(size_t idx) const
Gets the NULL terminated C string argument pointer for the argument at index idx.
Definition: Args.cpp:264
void BroadcastEvent(lldb::EventSP &event_sp)
Broadcast an event which has no associated data.
Definition: Broadcaster.h:262
bool Confirm(llvm::StringRef message, bool default_answer)
bool SetQuitExitCode(int exit_code)
Sets the exit code for the quit command.
CommandObjectQuit(CommandInterpreter &interpreter)
bool ShouldAskForConfirmation(bool &is_a_detach)
bool DoExecute(Args &args, CommandReturnObject &result) override
std::vector< CommandArgumentEntry > m_arguments
CommandInterpreter & m_interpreter
void void AppendError(llvm::StringRef in_string)
void SetStatus(lldb::ReturnStatus status)
static lldb::DebuggerSP GetDebuggerAtIndex(size_t index)
Definition: Debugger.cpp:1209
static size_t GetNumDebuggers()
Definition: Debugger.cpp:1201
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb::TargetSP GetTargetAtIndex(uint32_t index) const
Definition: TargetList.cpp:497
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
@ eReturnStatusFailed
@ eReturnStatusQuit
@ eArgTypeUnsignedInteger
Used to build individual command argument lists.
Definition: CommandObject.h:93