Skip to content

Commit 23df946

Browse files
committed
[lldb][NFC] Used shared_fd_t
Replaced `int connection_fd = -1` with `shared_fd_t connection_fd = SharedSocket::kInvalidFD`. This is prerequisite for #104238.
1 parent 691e3c6 commit 23df946

File tree

8 files changed

+44
-28
lines changed

8 files changed

+44
-28
lines changed

lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -962,16 +962,19 @@ Status GDBRemoteCommunication::StartDebugserverProcess(
962962
#endif
963963

964964
// If a url is supplied then use it
965-
if (url)
965+
if (url && url[0])
966966
debugserver_args.AppendArgument(llvm::StringRef(url));
967967

968-
if (pass_comm_fd >= 0) {
968+
if (pass_comm_fd != SharedSocket::kInvalidFD) {
969969
StreamString fd_arg;
970-
fd_arg.Printf("--fd=%i", pass_comm_fd);
970+
fd_arg.Printf("--fd=%" PRIi64, (int64_t)pass_comm_fd);
971971
debugserver_args.AppendArgument(fd_arg.GetString());
972972
// Send "pass_comm_fd" down to the inferior so it can use it to
973-
// communicate back with this process
974-
launch_info.AppendDuplicateFileAction(pass_comm_fd, pass_comm_fd);
973+
// communicate back with this process. Ignored on Windows.
974+
#ifndef _WIN32
975+
launch_info.AppendDuplicateFileAction((int)pass_comm_fd,
976+
(int)pass_comm_fd);
977+
#endif
975978
}
976979

977980
// use native registers, not the GDB registers
@@ -991,7 +994,7 @@ Status GDBRemoteCommunication::StartDebugserverProcess(
991994
// port is null when debug server should listen on domain socket - we're
992995
// not interested in port value but rather waiting for debug server to
993996
// become available.
994-
if (pass_comm_fd == -1) {
997+
if (pass_comm_fd == SharedSocket::kInvalidFD) {
995998
if (url) {
996999
// Create a temporary file to get the stdout/stderr and redirect the output of
9971000
// the command into this file. We will later read this file if all goes well
@@ -1137,7 +1140,7 @@ Status GDBRemoteCommunication::StartDebugserverProcess(
11371140

11381141
if (error.Success() &&
11391142
(launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) &&
1140-
pass_comm_fd == -1) {
1143+
pass_comm_fd == SharedSocket::kInvalidFD) {
11411144
if (named_pipe_path.size() > 0) {
11421145
error = socket_pipe.OpenAsReader(named_pipe_path, false);
11431146
if (error.Fail())

lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "lldb/Core/Communication.h"
2222
#include "lldb/Host/Config.h"
2323
#include "lldb/Host/HostThread.h"
24+
#include "lldb/Host/Socket.h"
2425
#include "lldb/Utility/Args.h"
2526
#include "lldb/Utility/Listener.h"
2627
#include "lldb/Utility/Predicate.h"
@@ -156,8 +157,8 @@ class GDBRemoteCommunication : public Communication {
156157
Platform *platform, // If non nullptr, then check with the platform for
157158
// the GDB server binary if it can't be located
158159
ProcessLaunchInfo &launch_info, uint16_t *port, const Args *inferior_args,
159-
int pass_comm_fd); // Communication file descriptor to pass during
160-
// fork/exec to avoid having to connect/accept
160+
shared_fd_t pass_comm_fd); // Communication file descriptor to pass during
161+
// fork/exec to avoid having to connect/accept
161162

162163
void DumpHistory(Stream &strm);
163164

lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,9 @@ Status GDBRemoteCommunicationServerPlatform::LaunchGDBServer(
208208
port_ptr = nullptr;
209209
}
210210

211-
Status error = StartDebugserverProcess(
212-
url.str().c_str(), nullptr, debugserver_launch_info, port_ptr, &args, -1);
211+
Status error = StartDebugserverProcess(url.str().c_str(), nullptr,
212+
debugserver_launch_info, port_ptr,
213+
&args, SharedSocket::kInvalidFD);
213214

214215
pid = debugserver_launch_info.GetProcessID();
215216
if (pid != LLDB_INVALID_PROCESS_ID) {

lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3447,7 +3447,7 @@ Status ProcessGDBRemote::LaunchAndConnectToDebugserver(
34473447
}
34483448
#endif
34493449

3450-
int communication_fd = -1;
3450+
shared_fd_t communication_fd = SharedSocket::kInvalidFD;
34513451
#ifdef USE_SOCKETPAIR_FOR_LOCAL_CONNECTION
34523452
// Use a socketpair on non-Windows systems for security and performance
34533453
// reasons.

lldb/tools/lldb-server/lldb-gdbserver.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
#include "lldb/Host/ConnectionFileDescriptor.h"
2525
#include "lldb/Host/FileSystem.h"
2626
#include "lldb/Host/Pipe.h"
27-
#include "lldb/Host/Socket.h"
2827
#include "lldb/Host/common/NativeProcessProtocol.h"
28+
#include "lldb/Host/common/TCPSocket.h"
2929
#include "lldb/Target/Process.h"
3030
#include "lldb/Utility/LLDBLog.h"
3131
#include "lldb/Utility/Status.h"
@@ -195,18 +195,31 @@ void ConnectToRemote(MainLoop &mainloop,
195195
bool reverse_connect, llvm::StringRef host_and_port,
196196
const char *const progname, const char *const subcommand,
197197
const char *const named_pipe_path, pipe_t unnamed_pipe,
198-
int connection_fd) {
198+
shared_fd_t connection_fd) {
199199
Status error;
200200

201201
std::unique_ptr<Connection> connection_up;
202202
std::string url;
203203

204-
if (connection_fd != -1) {
204+
if (connection_fd != SharedSocket::kInvalidFD) {
205+
#ifdef _WIN32
206+
NativeSocket sockfd;
207+
error = SharedSocket::GetNativeSocket(connection_fd, sockfd);
208+
if (error.Fail()) {
209+
llvm::errs() << llvm::formatv("error: GetNativeSocket failed: {0}\n",
210+
error.AsCString());
211+
exit(-1);
212+
}
213+
connection_up =
214+
std::unique_ptr<Connection>(new ConnectionFileDescriptor(new TCPSocket(
215+
sockfd, /*should_close=*/true, /*child_processes_inherit=*/false)));
216+
#else
205217
url = llvm::formatv("fd://{0}", connection_fd).str();
206218

207219
// Create the connection.
208-
#if LLDB_ENABLE_POSIX && !defined _WIN32
220+
#if LLDB_ENABLE_POSIX
209221
::fcntl(connection_fd, F_SETFD, FD_CLOEXEC);
222+
#endif
210223
#endif
211224
} else if (!host_and_port.empty()) {
212225
llvm::Expected<std::string> url_exp =
@@ -338,7 +351,7 @@ int main_gdbserver(int argc, char *argv[]) {
338351
log_channels; // e.g. "lldb process threads:gdb-remote default:linux all"
339352
lldb::pipe_t unnamed_pipe = LLDB_INVALID_PIPE;
340353
bool reverse_connect = false;
341-
int connection_fd = -1;
354+
shared_fd_t connection_fd = SharedSocket::kInvalidFD;
342355

343356
// ProcessLaunchInfo launch_info;
344357
ProcessAttachInfo attach_info;
@@ -404,10 +417,12 @@ int main_gdbserver(int argc, char *argv[]) {
404417
unnamed_pipe = (pipe_t)Arg;
405418
}
406419
if (Args.hasArg(OPT_fd)) {
407-
if (!llvm::to_integer(Args.getLastArgValue(OPT_fd), connection_fd)) {
420+
int64_t fd;
421+
if (!llvm::to_integer(Args.getLastArgValue(OPT_fd), fd)) {
408422
WithColor::error() << "invalid '--fd' argument\n" << HelpText;
409423
return 1;
410424
}
425+
connection_fd = (shared_fd_t)fd;
411426
}
412427

413428
if (!LLDBServerUtilities::SetupLogging(
@@ -423,7 +438,7 @@ int main_gdbserver(int argc, char *argv[]) {
423438
for (const char *Val : Arg->getValues())
424439
Inputs.push_back(Val);
425440
}
426-
if (Inputs.empty() && connection_fd == -1) {
441+
if (Inputs.empty() && connection_fd == SharedSocket::kInvalidFD) {
427442
WithColor::error() << "no connection arguments\n" << HelpText;
428443
return 1;
429444
}
@@ -432,7 +447,7 @@ int main_gdbserver(int argc, char *argv[]) {
432447
GDBRemoteCommunicationServerLLGS gdb_server(mainloop, manager);
433448

434449
llvm::StringRef host_and_port;
435-
if (!Inputs.empty()) {
450+
if (!Inputs.empty() && connection_fd == SharedSocket::kInvalidFD) {
436451
host_and_port = Inputs.front();
437452
Inputs.erase(Inputs.begin());
438453
}

lldb/unittests/tools/lldb-server/tests/LLGSTest.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ using namespace llgs_tests;
1414
using namespace lldb_private;
1515
using namespace llvm;
1616

17-
#ifdef SendMessage
18-
#undef SendMessage
19-
#endif
20-
2117
// Disable this test on Windows as it appears to have a race condition
2218
// that causes lldb-server not to exit after the inferior hangs up.
2319
#if !defined(_WIN32)

lldb/unittests/tools/lldb-server/tests/TestClient.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ using namespace lldb_private;
2626
using namespace llvm;
2727
using namespace llgs_tests;
2828

29-
#ifdef SendMessage
30-
#undef SendMessage
31-
#endif
32-
3329
TestClient::TestClient(std::unique_ptr<Connection> Conn) {
3430
SetConnection(std::move(Conn));
3531
SetPacketTimeout(std::chrono::seconds(10));

lldb/unittests/tools/lldb-server/tests/TestClient.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
#include <optional>
2121
#include <string>
2222

23+
#ifdef SendMessage
24+
#undef SendMessage
25+
#endif
26+
2327
#if LLDB_SERVER_IS_DEBUGSERVER
2428
#define LLGS_TEST(x) DISABLED_ ## x
2529
#define DS_TEST(x) x

0 commit comments

Comments
 (0)