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"
17#include "llvm/ADT/StringExtras.h"
18#include "llvm/Support/FormatVariadic.h"
19
20#include <memory>
21
22using namespace lldb;
23using namespace lldb_private;
24
25/// PIMPL backing SBProtocolServer.
30
35
40
42 LLDB_INSTRUMENT_VA(this, rhs);
43
44 if (this != &rhs)
46 return *this;
47}
48
50
52 SBError &error) {
53 LLDB_INSTRUMENT_VA(protocol, error);
54
55 SBProtocolServer server;
56 error.Clear();
57
58 if (!protocol || !protocol[0]) {
59 error.SetErrorString("no protocol specified");
60 return server;
61 }
62
63 ProtocolServer *protocol_server = ProtocolServer::GetOrCreate(protocol);
64 if (!protocol_server) {
65 error.SetErrorStringWithFormat(
66 "unsupported protocol: %s. Supported protocols are: %s", protocol,
67 llvm::join(ProtocolServer::GetSupportedProtocols(), ", ").c_str());
68 return server;
69 }
70
71 server.m_opaque_up->server = protocol_server;
72 return server;
73}
74
75SBProtocolServer::operator bool() const {
77
78 return m_opaque_up->server != nullptr;
79}
80
83
84 return m_opaque_up->server != nullptr;
85}
86
87SBError SBProtocolServer::Start(const char *connection_uri) {
88 LLDB_INSTRUMENT_VA(this, connection_uri);
89
91 if (!m_opaque_up->server) {
92 error.SetErrorString("invalid protocol server");
93 return error;
94 }
95
96 const char *connection_error =
97 "unsupported connection specifier, expected 'accept:///path' or "
98 "'listen://[host]:port'";
99 std::optional<URI> uri = URI::Parse(connection_uri ? connection_uri : "");
100 if (!uri) {
101 error.SetErrorString(connection_error);
102 return error;
103 }
104
105 std::optional<Socket::ProtocolModePair> protocol_and_mode =
106 Socket::GetProtocolAndMode(uri->scheme);
107 if (!protocol_and_mode || protocol_and_mode->second != Socket::ModeAccept) {
108 error.SetErrorString(connection_error);
109 return error;
110 }
111
113 connection.protocol = protocol_and_mode->first;
115 connection.name = uri->path;
116 else
117 connection.name =
118 llvm::formatv("[{0}]:{1}",
119 uri->hostname.empty() ? "0.0.0.0" : uri->hostname,
120 uri->port.value_or(0))
121 .str();
122
123 if (llvm::Error err = m_opaque_up->server->Start(connection))
124 error.SetErrorString(llvm::toString(std::move(err)).c_str());
125
126 return error;
127}
128
130 LLDB_INSTRUMENT_VA(this);
131
133 if (!m_opaque_up->server) {
134 error.SetErrorString("invalid protocol server");
135 return error;
136 }
137
138 if (llvm::Error err = m_opaque_up->server->Stop())
139 error.SetErrorString(llvm::toString(std::move(err)).c_str());
140
141 return error;
142}
143
145 LLDB_INSTRUMENT_VA(this);
146
147 if (!m_opaque_up->server)
148 return nullptr;
149
150 Socket *socket = m_opaque_up->server->GetSocket();
151 if (!socket)
152 return nullptr;
153
154 std::vector<std::string> uris = socket->GetListeningConnectionURI();
155 if (uris.empty())
156 return nullptr;
157
158 // A listening socket may report several equivalent URIs (e.g. IPv6 and
159 // IPv4 loopback). Return the first, interned so the pointer stays valid.
160 return StringPool::GetSystemPool().Intern(uris.front());
161}
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...
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
const char * Intern(llvm::StringRef str) const
Definition StringPool.h:78
static StringPoolRef GetSystemPool()
The system pool. Only valid between Initialize and Terminate.
A class that represents a running process on the host machine.
static std::optional< URI > Parse(llvm::StringRef uri)
Definition UriParser.cpp:28