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}
26
28
29// returns true if there is at least one alive process is_a_detach will be true
30// if all alive processes will be detached when you quit and false if at least
31// one process will be killed instead
34 return false;
35 bool should_prompt = false;
36 is_a_detach = true;
37 for (uint32_t debugger_idx = 0; debugger_idx < Debugger::GetNumDebuggers();
38 debugger_idx++) {
39 DebuggerSP debugger_sp(Debugger::GetDebuggerAtIndex(debugger_idx));
40 if (!debugger_sp)
41 continue;
42 const TargetList &target_list(debugger_sp->GetTargetList());
43 for (uint32_t target_idx = 0;
44 target_idx < static_cast<uint32_t>(target_list.GetNumTargets());
45 target_idx++) {
46 TargetSP target_sp(target_list.GetTargetAtIndex(target_idx));
47 if (!target_sp)
48 continue;
49 ProcessSP process_sp(target_sp->GetProcessSP());
50 if (process_sp && process_sp->IsValid() && process_sp->IsAlive() &&
51 process_sp->WarnBeforeDetach()) {
52 should_prompt = true;
53 if (!process_sp->GetShouldDetach()) {
54 // if we need to kill at least one process, just say so and return
55 is_a_detach = false;
56 return should_prompt;
57 }
58 }
59 }
60 }
61 return should_prompt;
62}
63
65 bool is_a_detach = true;
66 if (ShouldAskForConfirmation(is_a_detach)) {
67 StreamString message;
68 message.Printf("Quitting LLDB will %s one or more processes. Do you really "
69 "want to proceed",
70 (is_a_detach ? "detach from" : "kill"));
71 if (!m_interpreter.Confirm(message.GetString(), true)) {
73 return;
74 }
75 }
76
77 if (command.GetArgumentCount() > 1) {
78 result.AppendError("Too many arguments for 'quit'. Only an optional exit "
79 "code is allowed");
80 return;
81 }
82
83 // We parse the exit code argument if there is one.
84 if (command.GetArgumentCount() == 1) {
85 llvm::StringRef arg = command.GetArgumentAtIndex(0);
86 int exit_code;
87 if (arg.getAsInteger(/*autodetect radix*/ 0, exit_code)) {
89 std::string arg_str = arg.str();
90 s.Printf("Couldn't parse '%s' as integer for exit code.", arg_str.data());
91 result.AppendError(s.GetString());
92 return;
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.");
97 return;
98 }
99 }
100
101 const uint32_t event_type =
103 m_interpreter.BroadcastEvent(event_type);
105}
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:263
void BroadcastEvent(lldb::EventSP &event_sp)
Broadcast an event which has no associated data.
Definition: Broadcaster.h:168
bool Confirm(llvm::StringRef message, bool default_answer)
bool SetQuitExitCode(int exit_code)
Sets the exit code for the quit command.
CommandObjectQuit(CommandInterpreter &interpreter)
void DoExecute(Args &args, CommandReturnObject &result) override
bool ShouldAskForConfirmation(bool &is_a_detach)
void AddSimpleArgumentList(lldb::CommandArgumentType arg_type, ArgumentRepetitionType repetition_type=eArgRepeatPlain)
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:1355
static size_t GetNumDebuggers()
Definition: Debugger.cpp:1347
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
lldb::TargetSP GetTargetAtIndex(uint32_t index) const
Definition: TargetList.cpp:499
size_t GetNumTargets() const
Definition: TargetList.cpp:494
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::Process > ProcessSP
Definition: lldb-forward.h:387
std::shared_ptr< lldb_private::Debugger > DebuggerSP
Definition: lldb-forward.h:337
@ eReturnStatusFailed
@ eReturnStatusQuit
@ eArgTypeUnsignedInteger
std::shared_ptr< lldb_private::Target > TargetSP
Definition: lldb-forward.h:444