|
| 1 | +//===-- CommandObjectProtocolServer.cpp |
| 2 | +//----------------------------------------------===// |
| 3 | +// |
| 4 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +// See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | + |
| 10 | +#include "CommandObjectProtocolServer.h" |
| 11 | +#include "lldb/Core/PluginManager.h" |
| 12 | +#include "lldb/Core/ProtocolServer.h" |
| 13 | +#include "lldb/Host/Socket.h" |
| 14 | +#include "lldb/Interpreter/CommandInterpreter.h" |
| 15 | +#include "lldb/Interpreter/CommandReturnObject.h" |
| 16 | +#include "lldb/Utility/UriParser.h" |
| 17 | +#include "llvm/ADT/STLExtras.h" |
| 18 | +#include "llvm/Support/FormatAdapters.h" |
| 19 | + |
| 20 | +using namespace llvm; |
| 21 | +using namespace lldb; |
| 22 | +using namespace lldb_private; |
| 23 | + |
| 24 | +#define LLDB_OPTIONS_mcp |
| 25 | +#include "CommandOptions.inc" |
| 26 | + |
| 27 | +static std::vector<llvm::StringRef> GetSupportedProtocols() { |
| 28 | + std::vector<llvm::StringRef> supported_protocols; |
| 29 | + size_t i = 0; |
| 30 | + |
| 31 | + for (llvm::StringRef protocol_name = |
| 32 | + PluginManager::GetProtocolServerPluginNameAtIndex(i++); |
| 33 | + !protocol_name.empty(); |
| 34 | + protocol_name = PluginManager::GetProtocolServerPluginNameAtIndex(i++)) { |
| 35 | + supported_protocols.push_back(protocol_name); |
| 36 | + } |
| 37 | + |
| 38 | + return supported_protocols; |
| 39 | +} |
| 40 | + |
| 41 | +class CommandObjectProtocolServerStart : public CommandObjectParsed { |
| 42 | +public: |
| 43 | + CommandObjectProtocolServerStart(CommandInterpreter &interpreter) |
| 44 | + : CommandObjectParsed(interpreter, "protocol-server start", |
| 45 | + "start protocol server", |
| 46 | + "protocol-server start <protocol> <connection>") { |
| 47 | + AddSimpleArgumentList(lldb::eArgTypeProtocol, eArgRepeatPlain); |
| 48 | + AddSimpleArgumentList(lldb::eArgTypeConnectURL, eArgRepeatPlain); |
| 49 | + } |
| 50 | + |
| 51 | + ~CommandObjectProtocolServerStart() override = default; |
| 52 | + |
| 53 | +protected: |
| 54 | + void DoExecute(Args &args, CommandReturnObject &result) override { |
| 55 | + if (args.GetArgumentCount() < 1) { |
| 56 | + result.AppendError("no protocol specified"); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + llvm::StringRef protocol = args.GetArgumentAtIndex(0); |
| 61 | + std::vector<llvm::StringRef> supported_protocols = GetSupportedProtocols(); |
| 62 | + if (llvm::find(supported_protocols, protocol) == |
| 63 | + supported_protocols.end()) { |
| 64 | + result.AppendErrorWithFormatv( |
| 65 | + "unsupported protocol: {0}. Supported protocols are: {1}", protocol, |
| 66 | + llvm::join(GetSupportedProtocols(), ", ")); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + if (args.GetArgumentCount() < 2) { |
| 71 | + result.AppendError("no connection specified"); |
| 72 | + return; |
| 73 | + } |
| 74 | + llvm::StringRef connection_uri = args.GetArgumentAtIndex(1); |
| 75 | + |
| 76 | + ProtocolServerSP server_sp = GetDebugger().GetProtocolServer(protocol); |
| 77 | + if (!server_sp) |
| 78 | + server_sp = ProtocolServer::Create(protocol, GetDebugger()); |
| 79 | + |
| 80 | + const char *connection_error = |
| 81 | + "unsupported connection specifier, expected 'accept:///path' or " |
| 82 | + "'listen://[host]:port', got '{0}'."; |
| 83 | + auto uri = lldb_private::URI::Parse(connection_uri); |
| 84 | + if (!uri) { |
| 85 | + result.AppendErrorWithFormatv(connection_error, connection_uri); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + std::optional<Socket::ProtocolModePair> protocol_and_mode = |
| 90 | + Socket::GetProtocolAndMode(uri->scheme); |
| 91 | + if (!protocol_and_mode || protocol_and_mode->second != Socket::ModeAccept) { |
| 92 | + result.AppendErrorWithFormatv(connection_error, connection_uri); |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + ProtocolServer::Connection connection; |
| 97 | + connection.protocol = protocol_and_mode->first; |
| 98 | + connection.name = |
| 99 | + formatv("[{0}]:{1}", uri->hostname.empty() ? "0.0.0.0" : uri->hostname, |
| 100 | + uri->port.value_or(0)); |
| 101 | + |
| 102 | + if (llvm::Error error = server_sp->Start(connection)) { |
| 103 | + result.AppendErrorWithFormatv("{0}", llvm::fmt_consume(std::move(error))); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + GetDebugger().AddProtocolServer(server_sp); |
| 108 | + |
| 109 | + if (Socket *socket = server_sp->GetSocket()) { |
| 110 | + std::string address = |
| 111 | + llvm::join(socket->GetListeningConnectionURI(), ", "); |
| 112 | + result.AppendMessageWithFormatv( |
| 113 | + "{0} server started with connection listeners: {1}", protocol, |
| 114 | + address); |
| 115 | + } |
| 116 | + } |
| 117 | +}; |
| 118 | + |
| 119 | +class CommandObjectProtocolServerStop : public CommandObjectParsed { |
| 120 | +public: |
| 121 | + CommandObjectProtocolServerStop(CommandInterpreter &interpreter) |
| 122 | + : CommandObjectParsed(interpreter, "protocol-server stop", |
| 123 | + "stop protocol server", |
| 124 | + "protocol-server stop <protocol>") { |
| 125 | + AddSimpleArgumentList(lldb::eArgTypeProtocol, eArgRepeatPlain); |
| 126 | + } |
| 127 | + |
| 128 | + ~CommandObjectProtocolServerStop() override = default; |
| 129 | + |
| 130 | +protected: |
| 131 | + void DoExecute(Args &args, CommandReturnObject &result) override { |
| 132 | + if (args.GetArgumentCount() < 1) { |
| 133 | + result.AppendError("no protocol specified"); |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + llvm::StringRef protocol = args.GetArgumentAtIndex(0); |
| 138 | + std::vector<llvm::StringRef> supported_protocols = GetSupportedProtocols(); |
| 139 | + if (llvm::find(supported_protocols, protocol) == |
| 140 | + supported_protocols.end()) { |
| 141 | + result.AppendErrorWithFormatv( |
| 142 | + "unsupported protocol: {0}. Supported protocols are: {1}", protocol, |
| 143 | + llvm::join(GetSupportedProtocols(), ", ")); |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + Debugger &debugger = GetDebugger(); |
| 148 | + |
| 149 | + ProtocolServerSP server_sp = debugger.GetProtocolServer(protocol); |
| 150 | + if (!server_sp) { |
| 151 | + result.AppendError( |
| 152 | + llvm::formatv("no {0} protocol server running", protocol).str()); |
| 153 | + return; |
| 154 | + } |
| 155 | + |
| 156 | + if (llvm::Error error = server_sp->Stop()) { |
| 157 | + result.AppendErrorWithFormatv("{0}", llvm::fmt_consume(std::move(error))); |
| 158 | + return; |
| 159 | + } |
| 160 | + |
| 161 | + debugger.RemoveProtocolServer(server_sp); |
| 162 | + } |
| 163 | +}; |
| 164 | + |
| 165 | +CommandObjectProtocolServer::CommandObjectProtocolServer( |
| 166 | + CommandInterpreter &interpreter) |
| 167 | + : CommandObjectMultiword(interpreter, "protocol-server", |
| 168 | + "Start and stop a protocol server.", |
| 169 | + "protocol-server") { |
| 170 | + LoadSubCommand("start", CommandObjectSP(new CommandObjectProtocolServerStart( |
| 171 | + interpreter))); |
| 172 | + LoadSubCommand("stop", CommandObjectSP( |
| 173 | + new CommandObjectProtocolServerStop(interpreter))); |
| 174 | +} |
| 175 | + |
| 176 | +CommandObjectProtocolServer::~CommandObjectProtocolServer() = default; |
0 commit comments