Path: blob/main/contrib/llvm-project/lldb/source/Commands/CommandObjectProtocolServer.cpp
213764 views
//===-- CommandObjectProtocolServer.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 "CommandObjectProtocolServer.h"9#include "lldb/Core/PluginManager.h"10#include "lldb/Core/ProtocolServer.h"11#include "lldb/Host/Socket.h"12#include "lldb/Interpreter/CommandInterpreter.h"13#include "lldb/Interpreter/CommandReturnObject.h"14#include "lldb/Utility/UriParser.h"15#include "llvm/ADT/STLExtras.h"16#include "llvm/Support/FormatAdapters.h"1718using namespace llvm;19using namespace lldb;20using namespace lldb_private;2122#define LLDB_OPTIONS_mcp23#include "CommandOptions.inc"2425class CommandObjectProtocolServerStart : public CommandObjectParsed {26public:27CommandObjectProtocolServerStart(CommandInterpreter &interpreter)28: CommandObjectParsed(interpreter, "protocol-server start",29"start protocol server",30"protocol-server start <protocol> <connection>") {31AddSimpleArgumentList(lldb::eArgTypeProtocol, eArgRepeatPlain);32AddSimpleArgumentList(lldb::eArgTypeConnectURL, eArgRepeatPlain);33}3435~CommandObjectProtocolServerStart() override = default;3637protected:38void DoExecute(Args &args, CommandReturnObject &result) override {39if (args.GetArgumentCount() < 1) {40result.AppendError("no protocol specified");41return;42}4344llvm::StringRef protocol = args.GetArgumentAtIndex(0);45ProtocolServer *server = ProtocolServer::GetOrCreate(protocol);46if (!server) {47result.AppendErrorWithFormatv(48"unsupported protocol: {0}. Supported protocols are: {1}", protocol,49llvm::join(ProtocolServer::GetSupportedProtocols(), ", "));50return;51}5253if (args.GetArgumentCount() < 2) {54result.AppendError("no connection specified");55return;56}57llvm::StringRef connection_uri = args.GetArgumentAtIndex(1);5859const char *connection_error =60"unsupported connection specifier, expected 'accept:///path' or "61"'listen://[host]:port', got '{0}'.";62auto uri = lldb_private::URI::Parse(connection_uri);63if (!uri) {64result.AppendErrorWithFormatv(connection_error, connection_uri);65return;66}6768std::optional<Socket::ProtocolModePair> protocol_and_mode =69Socket::GetProtocolAndMode(uri->scheme);70if (!protocol_and_mode || protocol_and_mode->second != Socket::ModeAccept) {71result.AppendErrorWithFormatv(connection_error, connection_uri);72return;73}7475ProtocolServer::Connection connection;76connection.protocol = protocol_and_mode->first;77if (connection.protocol == Socket::SocketProtocol::ProtocolUnixDomain)78connection.name = uri->path;79else80connection.name = formatv(81"[{0}]:{1}", uri->hostname.empty() ? "0.0.0.0" : uri->hostname,82uri->port.value_or(0));8384if (llvm::Error error = server->Start(connection)) {85result.AppendErrorWithFormatv("{0}", llvm::fmt_consume(std::move(error)));86return;87}8889if (Socket *socket = server->GetSocket()) {90std::string address =91llvm::join(socket->GetListeningConnectionURI(), ", ");92result.AppendMessageWithFormatv(93"{0} server started with connection listeners: {1}", protocol,94address);95result.SetStatus(eReturnStatusSuccessFinishNoResult);96}97}98};99100class CommandObjectProtocolServerStop : public CommandObjectParsed {101public:102CommandObjectProtocolServerStop(CommandInterpreter &interpreter)103: CommandObjectParsed(interpreter, "protocol-server stop",104"stop protocol server",105"protocol-server stop <protocol>") {106AddSimpleArgumentList(lldb::eArgTypeProtocol, eArgRepeatPlain);107}108109~CommandObjectProtocolServerStop() override = default;110111protected:112void DoExecute(Args &args, CommandReturnObject &result) override {113if (args.GetArgumentCount() < 1) {114result.AppendError("no protocol specified");115return;116}117118llvm::StringRef protocol = args.GetArgumentAtIndex(0);119ProtocolServer *server = ProtocolServer::GetOrCreate(protocol);120if (!server) {121result.AppendErrorWithFormatv(122"unsupported protocol: {0}. Supported protocols are: {1}", protocol,123llvm::join(ProtocolServer::GetSupportedProtocols(), ", "));124return;125}126127if (llvm::Error error = server->Stop()) {128result.AppendErrorWithFormatv("{0}", llvm::fmt_consume(std::move(error)));129return;130}131}132};133134CommandObjectProtocolServer::CommandObjectProtocolServer(135CommandInterpreter &interpreter)136: CommandObjectMultiword(interpreter, "protocol-server",137"Start and stop a protocol server.",138"protocol-server") {139LoadSubCommand("start", CommandObjectSP(new CommandObjectProtocolServerStart(140interpreter)));141LoadSubCommand("stop", CommandObjectSP(142new CommandObjectProtocolServerStop(interpreter)));143}144145CommandObjectProtocolServer::~CommandObjectProtocolServer() = default;146147148