Index: include/lldb/Host/PipeBase.h =================================================================== --- include/lldb/Host/PipeBase.h +++ include/lldb/Host/PipeBase.h @@ -25,6 +25,7 @@ virtual Error CreateNew(bool child_process_inherit) = 0; virtual Error CreateNew(llvm::StringRef name, bool child_process_inherit) = 0; + virtual Error CreateWithUniqueName(llvm::StringRef prefix, bool child_process_inherit) = 0; virtual Error OpenAsReader(llvm::StringRef name, bool child_process_inherit) = 0; @@ -33,6 +34,7 @@ virtual bool CanRead() const = 0; virtual bool CanWrite() const = 0; + virtual llvm::StringRef GetName() const = 0; virtual int GetReadFileDescriptor() const = 0; virtual int GetWriteFileDescriptor() const = 0; Index: include/lldb/Host/posix/PipePosix.h =================================================================== --- include/lldb/Host/posix/PipePosix.h +++ include/lldb/Host/posix/PipePosix.h @@ -13,6 +13,8 @@ #include "lldb/Host/PipeBase.h" +#include + namespace lldb_private { //---------------------------------------------------------------------- @@ -36,6 +38,8 @@ Error CreateNew(llvm::StringRef name, bool child_process_inherit) override; Error + CreateWithUniqueName(llvm::StringRef prefix, bool child_process_inherit) override; + Error OpenAsReader(llvm::StringRef name, bool child_process_inherit) override; Error OpenAsWriterWithTimeout(llvm::StringRef name, bool child_process_inherit, const std::chrono::microseconds &timeout) override; @@ -44,6 +48,8 @@ CanRead() const override; bool CanWrite() const override; + llvm::StringRef + GetName() const override; int GetReadFileDescriptor() const override; @@ -73,6 +79,7 @@ CloseWriteFileDescriptor(); int m_fds[2]; + std::string m_name; }; } // namespace lldb_private Index: include/lldb/Host/windows/PipeWindows.h =================================================================== --- include/lldb/Host/windows/PipeWindows.h +++ include/lldb/Host/windows/PipeWindows.h @@ -13,6 +13,8 @@ #include "lldb/Host/PipeBase.h" #include "lldb/Host/windows/windows.h" +#include + namespace lldb_private { @@ -31,11 +33,13 @@ Error CreateNew(bool child_process_inherit) override; Error CreateNew(llvm::StringRef name, bool child_process_inherit) override; + Error CreateWithUniqueName(llvm::StringRef prefix, bool child_process_inherit) override; Error OpenAsReader(llvm::StringRef name, bool child_process_inherit) override; Error OpenAsWriterWithTimeout(llvm::StringRef name, bool child_process_inherit, const std::chrono::microseconds &timeout) override; bool CanRead() const override; bool CanWrite() const override; + llvm::StringRef GetName() const override; int GetReadFileDescriptor() const override; int GetWriteFileDescriptor() const override; @@ -64,6 +68,7 @@ int m_read_fd; int m_write_fd; + std::string m_name; OVERLAPPED m_read_overlapped; OVERLAPPED m_write_overlapped; Index: source/Host/posix/PipePosix.cpp =================================================================== --- source/Host/posix/PipePosix.cpp +++ source/Host/posix/PipePosix.cpp @@ -9,12 +9,16 @@ #include "lldb/Host/posix/PipePosix.h" #include "lldb/Host/FileSystem.h" +#include "lldb/Host/HostInfo.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/Support/FileSystem.h" #include #include #include #include +#include #include #include #include @@ -179,6 +183,39 @@ if (::mkfifo(name.data(), 0660) != 0) error.SetErrorToErrno(); + if (error.Success()) + m_name = name; + + return error; +} + +Error +PipePosix::CreateWithUniqueName(llvm::StringRef prefix, bool child_process_inherit) +{ + llvm::SmallString named_pipe_path; + llvm::SmallString pipe_spec; + pipe_spec = prefix; + pipe_spec += ".%%%%%%"; + FileSpec tmpdir_file_spec; + tmpdir_file_spec.Clear(); + if (HostInfo::GetLLDBPath(ePathTypeLLDBTempSystemDir, tmpdir_file_spec)) + { + tmpdir_file_spec.AppendPathComponent(pipe_spec.c_str()); + } + else + { + tmpdir_file_spec.AppendPathComponent("/tmp"); + tmpdir_file_spec.AppendPathComponent(pipe_spec.c_str()); + } + + // It's possible that another process creates the target path after we've + // verified it's available but before we create it, in which case we + // should try again. + Error error; + do { + llvm::sys::fs::createUniqueFile(tmpdir_file_spec.GetPath().c_str(), named_pipe_path); + error = CreateNew(named_pipe_path, child_process_inherit); + } while (error.GetError() == EEXIST); return error; } @@ -195,9 +232,14 @@ Error error; int fd = ::open(name.data(), flags); if (fd != -1) + { + m_name = name; m_fds[READ] = fd; + } else + { error.SetErrorToErrno(); + } return error; } @@ -297,6 +339,12 @@ return m_fds[WRITE] != PipePosix::kInvalidDescriptor; } +llvm::StringRef +PipePosix::GetName() const +{ + return m_name; +} + void PipePosix::CloseReadFileDescriptor() { Index: source/Host/windows/PipeWindows.cpp =================================================================== --- source/Host/windows/PipeWindows.cpp +++ source/Host/windows/PipeWindows.cpp @@ -9,8 +9,11 @@ #include "lldb/Host/windows/PipeWindows.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/Support/Process.h" #include "llvm/Support/raw_ostream.h" +#include #include #include @@ -90,6 +93,30 @@ } Error +PipeWindows::CreateWithUniqueName(llvm::StringRef prefix, bool child_process_inherit) +{ + ::UUID uuid; + + llvm::SmallString<128> name; + Error error; + do { + name = prefix; + name += "-"; + ZeroMemory(&uuid, sizeof(::UUID)); + ::UuidCreate(&uuid); + char* uuid_str = NULL; + if (::UuidToString(&uuid, (RPC_CSTR*)&uuid_str) != RPC_S_OK) + return Error(::GetLastError(), eErrorTypeWin32); + name += uuid_str; + RpcStringFree((RPC_CSTR*)&uuid_str); + uuid_str = NULL; + + Error error = CreateNew(name, child_process_inherit); + } while (error.GetError() == ERROR_ALREADY_EXISTS); + return error; +} + +Error PipeWindows::OpenAsReader(llvm::StringRef name, bool child_process_inherit) { if (CanRead() || CanWrite()) @@ -143,6 +170,7 @@ ZeroMemory(&m_write_overlapped, sizeof(m_write_overlapped)); } + m_name = name; return Error(); } @@ -235,6 +263,12 @@ return (m_write != INVALID_HANDLE_VALUE); } +llvm::StringRef +PipeWindows::GetName() const +{ + return m_name; +} + HANDLE PipeWindows::GetReadNativeHandle() { Index: source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp =================================================================== --- source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp +++ source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp @@ -764,8 +764,6 @@ debugserver_args.AppendArgument("--setsid"); } - char named_pipe_path[PATH_MAX]; - named_pipe_path[0] = '\0'; Pipe port_named_pipe; bool listen = false; @@ -779,25 +777,11 @@ { // Binding to port zero, we need to figure out what port it ends up // using using a named pipe... - FileSpec tmpdir_file_spec; - if (HostInfo::GetLLDBPath(ePathTypeLLDBTempSystemDir, tmpdir_file_spec)) - { - tmpdir_file_spec.AppendPathComponent("debugserver-named-pipe.XXXXXX"); - strncpy(named_pipe_path, tmpdir_file_spec.GetPath().c_str(), sizeof(named_pipe_path)); - } - else - { - strncpy(named_pipe_path, "/tmp/debugserver-named-pipe.XXXXXX", sizeof(named_pipe_path)); - } - - if (::mktemp (named_pipe_path)) - { - error = port_named_pipe.CreateNew(named_pipe_path, false); - if (error.Fail()) - return error; - debugserver_args.AppendArgument("--named-pipe"); - debugserver_args.AppendArgument(named_pipe_path); - } + error = port_named_pipe.CreateWithUniqueName("debugserver-named-pipe", false); + if (error.Fail()) + return error; + debugserver_args.AppendArgument("--named-pipe"); + debugserver_args.AppendArgument(port_named_pipe.GetName().data()); } else { @@ -877,9 +861,9 @@ if (error.Success() && launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) { - if (named_pipe_path[0]) + if (!port_named_pipe.GetName().empty()) { - error = port_named_pipe.OpenAsReader(named_pipe_path, false); + error = port_named_pipe.OpenAsReader(port_named_pipe.GetName(), false); if (error.Success()) { char port_cstr[256]; @@ -897,7 +881,7 @@ else { if (log) - log->Printf("GDBRemoteCommunication::%s() failed to read a port value from named pipe %s: %s", __FUNCTION__, named_pipe_path, error.AsCString()); + log->Printf("GDBRemoteCommunication::%s() failed to read a port value from named pipe %s: %s", __FUNCTION__, port_named_pipe.GetName().data(), error.AsCString()); } port_named_pipe.Close(); @@ -905,13 +889,13 @@ else { if (log) - log->Printf("GDBRemoteCommunication::%s() failed to open named pipe %s for reading: %s", __FUNCTION__, named_pipe_path, error.AsCString()); + log->Printf("GDBRemoteCommunication::%s() failed to open named pipe %s for reading: %s", __FUNCTION__, port_named_pipe.GetName().data(), error.AsCString()); } - const auto err = port_named_pipe.Delete(named_pipe_path); + const auto err = port_named_pipe.Delete(port_named_pipe.GetName()); if (err.Fail()) { if (log) - log->Printf ("GDBRemoteCommunication::%s failed to delete pipe %s: %s", __FUNCTION__, named_pipe_path, err.AsCString()); + log->Printf ("GDBRemoteCommunication::%s failed to delete pipe %s: %s", __FUNCTION__, port_named_pipe.GetName().data(), err.AsCString()); } } else if (listen)