Path: blob/main/contrib/llvm-project/lldb/source/Commands/CommandObjectQuit.cpp
39587 views
//===-- CommandObjectQuit.cpp ---------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "CommandObjectQuit.h"910#include "lldb/Interpreter/CommandInterpreter.h"11#include "lldb/Interpreter/CommandReturnObject.h"12#include "lldb/Target/Process.h"13#include "lldb/Utility/StreamString.h"1415using namespace lldb;16using namespace lldb_private;1718// CommandObjectQuit1920CommandObjectQuit::CommandObjectQuit(CommandInterpreter &interpreter)21: CommandObjectParsed(interpreter, "quit", "Quit the LLDB debugger.",22"quit [exit-code]") {23AddSimpleArgumentList(eArgTypeUnsignedInteger);24}2526CommandObjectQuit::~CommandObjectQuit() = default;2728// returns true if there is at least one alive process is_a_detach will be true29// if all alive processes will be detached when you quit and false if at least30// one process will be killed instead31bool CommandObjectQuit::ShouldAskForConfirmation(bool &is_a_detach) {32if (!m_interpreter.GetPromptOnQuit())33return false;34bool should_prompt = false;35is_a_detach = true;36for (uint32_t debugger_idx = 0; debugger_idx < Debugger::GetNumDebuggers();37debugger_idx++) {38DebuggerSP debugger_sp(Debugger::GetDebuggerAtIndex(debugger_idx));39if (!debugger_sp)40continue;41const TargetList &target_list(debugger_sp->GetTargetList());42for (uint32_t target_idx = 0;43target_idx < static_cast<uint32_t>(target_list.GetNumTargets());44target_idx++) {45TargetSP target_sp(target_list.GetTargetAtIndex(target_idx));46if (!target_sp)47continue;48ProcessSP process_sp(target_sp->GetProcessSP());49if (process_sp && process_sp->IsValid() && process_sp->IsAlive() &&50process_sp->WarnBeforeDetach()) {51should_prompt = true;52if (!process_sp->GetShouldDetach()) {53// if we need to kill at least one process, just say so and return54is_a_detach = false;55return should_prompt;56}57}58}59}60return should_prompt;61}6263void CommandObjectQuit::DoExecute(Args &command, CommandReturnObject &result) {64bool is_a_detach = true;65if (ShouldAskForConfirmation(is_a_detach)) {66StreamString message;67message.Printf("Quitting LLDB will %s one or more processes. Do you really "68"want to proceed",69(is_a_detach ? "detach from" : "kill"));70if (!m_interpreter.Confirm(message.GetString(), true)) {71result.SetStatus(eReturnStatusFailed);72return;73}74}7576if (command.GetArgumentCount() > 1) {77result.AppendError("Too many arguments for 'quit'. Only an optional exit "78"code is allowed");79return;80}8182// We parse the exit code argument if there is one.83if (command.GetArgumentCount() == 1) {84llvm::StringRef arg = command.GetArgumentAtIndex(0);85int exit_code;86if (arg.getAsInteger(/*autodetect radix*/ 0, exit_code)) {87lldb_private::StreamString s;88std::string arg_str = arg.str();89s.Printf("Couldn't parse '%s' as integer for exit code.", arg_str.data());90result.AppendError(s.GetString());91return;92}93if (!m_interpreter.SetQuitExitCode(exit_code)) {94result.AppendError("The current driver doesn't allow custom exit codes"95" for the quit command.");96return;97}98}99100const uint32_t event_type =101CommandInterpreter::eBroadcastBitQuitCommandReceived;102m_interpreter.BroadcastEvent(event_type);103result.SetStatus(eReturnStatusQuit);104}105106107