LLDB mainline
SBProtocolServer.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
10#include "lldb/API/SBError.h"
12#include "lldb/Host/Socket.h"
16#include "llvm/ADT/StringExtras.h"
17#include "llvm/Support/FormatVariadic.h"
18
19#include <memory>
20
21using namespace lldb;
22using namespace lldb_private;
23
24/// PIMPL backing SBProtocolServer.
29
34
39
41 LLDB_INSTRUMENT_VA(this, rhs);
42
43 if (this != &rhs)
45 return *this;
46}
47
49
51 SBError &error) {
52 LLDB_INSTRUMENT_VA(protocol, error);
53
54 SBProtocolServer server;
55 error.Clear();
56
57 if (!protocol || !protocol[0]) {
58 error.SetErrorString("no protocol specified");
59 return server;
60 }
61
62 ProtocolServer *protocol_server = ProtocolServer::GetOrCreate(protocol);
63 if (!protocol_server) {
64 error.SetErrorStringWithFormat(
65 "unsupported protocol: %s. Supported protocols are: %s", protocol,
66 llvm::join(ProtocolServer::GetSupportedProtocols(), ", ").c_str());
67 return server;
68 }
69
70 server.m_opaque_up->server = protocol_server;
71 return server;
72}
73
74SBProtocolServer::operator bool() const {
76
77 return m_opaque_up->server != nullptr;
78}
79
82
83 return m_opaque_up->server != nullptr;
84}
85
86SBError SBProtocolServer::Start(const char *connection_uri) {
87 LLDB_INSTRUMENT_VA(this, connection_uri);
88
90 if (!m_opaque_up->server) {
91 error.SetErrorString("invalid protocol server");
92 return error;
93 }
94
95 const char *connection_error =
96 "unsupported connection specifier, expected 'accept:///path' or "
97 "'listen://[host]:port'";
98 std::optional<URI> uri = URI::Parse(connection_uri ? connection_uri : "");
99 if (!uri) {
100 error.SetErrorString(connection_error);
101 return error;
102 }
103
104 std::optional<Socket::ProtocolModePair> protocol_and_mode =
105 Socket::GetProtocolAndMode(uri->scheme);
106 if (!protocol_and_mode || protocol_and_mode->second != Socket::ModeAccept) {
107 error.SetErrorString(connection_error);
108 return error;
109 }
110
112 connection.protocol = protocol_and_mode->first;
114 connection.name = uri->path;
115 else
116 connection.name =
117 llvm::formatv("[{0}]:{1}",
118 uri->hostname.empty() ? "0.0.0.0" : uri->hostname,
119 uri->port.value_or(0))
120 .str();
121
122 if (llvm::Error err = m_opaque_up->server->Start(connection))
123 error.SetErrorString(llvm::toString(std::move(err)).c_str());
124
125 return error;
126}
127
129 LLDB_INSTRUMENT_VA(this);
130
132 if (!m_opaque_up->server) {
133 error.SetErrorString("invalid protocol server");
134 return error;
135 }
136
137 if (llvm::Error err = m_opaque_up->server->Stop())
138 error.SetErrorString(llvm::toString(std::move(err)).c_str());
139
140 return error;
141}
142
144 LLDB_INSTRUMENT_VA(this);
145
146 if (!m_opaque_up->server)
147 return nullptr;
148
149 Socket *socket = m_opaque_up->server->GetSocket();
150 if (!socket)
151 return nullptr;
152
153 std::vector<std::string> uris = socket->GetListeningConnectionURI();
154 if (uris.empty())
155 return nullptr;
156
157 // A listening socket may report several equivalent URIs (e.g. IPv6 and
158 // IPv4 loopback). Return the first, interned so the pointer stays valid.
159 return ConstString(uris.front()).GetCString();
160}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_INSTRUMENT_VA(...)
const char * GetConnectionURI()
Returns the URI clients can connect to, valid after a successful Start, or nullptr if the server is n...
SBProtocolServer & operator=(const SBProtocolServer &rhs)
std::unique_ptr< lldb_private::SBProtocolServerImpl > m_opaque_up
static SBProtocolServer Create(const char *protocol, lldb::SBError &error)
Returns the protocol server for protocol, creating it if this process does not already have one.
lldb::SBError Start(const char *connection_uri)
Starts listening for clients on connection_uri, which must be an accepting URI such as "listen://[hos...
A uniqued constant string class.
Definition ConstString.h:40
const char * GetCString() const
Get the string value as a C string.
static std::vector< llvm::StringRef > GetSupportedProtocols()
static ProtocolServer * GetOrCreate(llvm::StringRef name)
PIMPL backing SBProtocolServer.
virtual std::vector< std::string > GetListeningConnectionURI() const
Definition Socket.h:169
static std::optional< ProtocolModePair > GetProtocolAndMode(llvm::StringRef scheme)
Definition Socket.cpp:511
A class that represents a running process on the host machine.
static std::optional< URI > Parse(llvm::StringRef uri)
Definition UriParser.cpp:28