diff options
author | emaste <emaste@FreeBSD.org> | 2015-07-04 01:02:43 +0000 |
---|---|---|
committer | emaste <emaste@FreeBSD.org> | 2015-07-04 01:02:43 +0000 |
commit | cea4c167517a0678c7dbf92a0324088dcbac1035 (patch) | |
tree | 02de7f7c9d5a08ae1c4d3b4c98a565ff96cd52e6 /contrib/llvm/tools/lldb/source/Host/common/Socket.cpp | |
parent | 1756896fd2b99ede7ebeb8019d4004bdfeed3bbe (diff) | |
parent | 8037fa4ee916fa20b3c63cbf531f4ee7e1c76138 (diff) | |
download | FreeBSD-src-cea4c167517a0678c7dbf92a0324088dcbac1035.zip FreeBSD-src-cea4c167517a0678c7dbf92a0324088dcbac1035.tar.gz |
Update LLDB snapshot to upstream r241361
Notable upstream commits (upstream revision in parens):
- Add a JSON producer to LLDB (228636)
- Don't crash on bad DWARF expression (228729)
- Add support of DWARFv3 DW_OP_form_tls_address (231342)
- Assembly profiler for MIPS64 (232619)
- Handle FreeBSD/arm64 core files (233273)
- Read/Write register for MIPS64 (233685)
- Rework LLDB system initialization (233758)
- SysV ABI for aarch64 (236098)
- MIPS software single stepping (236696)
- FreeBSD/arm live debugging support (237303)
- Assembly profiler for mips32 (237420)
- Parse function name from DWARF DW_AT_abstract_origin (238307)
- Improve LLDB prompt handling (238313)
- Add real time signals support to FreeBSDSignals (238316)
- Fix race in IOHandlerProcessSTDIO (238423)
- MIPS64 Branch instruction emulation for SW single stepping (238820)
- Improve OSType initialization in elf object file's arch_spec (239148)
- Emulation of MIPS64 floating-point branch instructions (239996)
- ABI Plugin for MIPS32 (239997)
- ABI Plugin for MIPS64 (240123)
- MIPS32 branch emulation and single stepping (240373)
- Improve instruction emulation based stack unwinding on ARM (240533)
- Add branch emulation to aarch64 instruction emulator (240769)
Diffstat (limited to 'contrib/llvm/tools/lldb/source/Host/common/Socket.cpp')
-rw-r--r-- | contrib/llvm/tools/lldb/source/Host/common/Socket.cpp | 153 |
1 files changed, 106 insertions, 47 deletions
diff --git a/contrib/llvm/tools/lldb/source/Host/common/Socket.cpp b/contrib/llvm/tools/lldb/source/Host/common/Socket.cpp index a6118ee..f7e93c6 100644 --- a/contrib/llvm/tools/lldb/source/Host/common/Socket.cpp +++ b/contrib/llvm/tools/lldb/source/Host/common/Socket.cpp @@ -15,8 +15,8 @@ #include "lldb/Host/FileSystem.h" #include "lldb/Host/Host.h" #include "lldb/Host/SocketAddress.h" +#include "lldb/Host/StringConvert.h" #include "lldb/Host/TimeValue.h" -#include "lldb/Interpreter/Args.h" #ifdef __ANDROID_NDK__ #include <linux/tcp.h> @@ -80,6 +80,25 @@ NativeSocket Accept(NativeSocket sockfd, struct sockaddr *addr, socklen_t *addrl return ::accept (sockfd, addr, addrlen); #endif } + +void SetLastError(Error &error) +{ +#if defined(_WIN32) + error.SetError(::WSAGetLastError(), lldb::eErrorTypeWin32); +#else + error.SetErrorToErrno(); +#endif +} + +bool IsInterrupted() +{ +#if defined(_WIN32) + return ::WSAGetLastError() == WSAEINTR; +#else + return errno == EINTR; +#endif +} + } Socket::Socket(NativeSocket socket, SocketProtocol protocol, bool should_close) @@ -116,8 +135,7 @@ Error Socket::TcpConnect(llvm::StringRef host_and_port, bool child_processes_inh sock = CreateSocket (AF_INET, SOCK_STREAM, IPPROTO_TCP, child_processes_inherit); if (sock == kInvalidSocketValue) { - // TODO: On Windows, use WSAGetLastError(). - error.SetErrorToErrno(); + SetLastError (error); return error; } @@ -143,9 +161,8 @@ Error Socket::TcpConnect(llvm::StringRef host_and_port, bool child_processes_inh inet_pton_result = ::inet_pton (AF_INET, host_str.c_str(), &sa.sin_addr); if (inet_pton_result <= 0) { - // TODO: On Windows, use WSAGetLastError() if (inet_pton_result == -1) - error.SetErrorToErrno(); + SetLastError(error); else error.SetErrorStringWithFormat("invalid host string: '%s'", host_str.c_str()); @@ -155,8 +172,7 @@ Error Socket::TcpConnect(llvm::StringRef host_and_port, bool child_processes_inh if (-1 == ::connect (sock, (const struct sockaddr *)&sa, sizeof(sa))) { - // TODO: On Windows, use WSAGetLastError() - error.SetErrorToErrno(); + SetLastError (error); return error; } @@ -167,7 +183,12 @@ Error Socket::TcpConnect(llvm::StringRef host_and_port, bool child_processes_inh return error; } -Error Socket::TcpListen(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&socket, Predicate<uint16_t>* predicate) +Error Socket::TcpListen( + llvm::StringRef host_and_port, + bool child_processes_inherit, + Socket *&socket, + Predicate<uint16_t>* predicate, + int backlog) { std::unique_ptr<Socket> listen_socket; NativeSocket listen_sock = kInvalidSocketValue; @@ -179,7 +200,7 @@ Error Socket::TcpListen(llvm::StringRef host_and_port, bool child_processes_inhe listen_sock = ::CreateSocket (family, socktype, protocol, child_processes_inherit); if (listen_sock == kInvalidSocketValue) { - error.SetErrorToErrno(); + SetLastError (error); return error; } @@ -190,7 +211,7 @@ Error Socket::TcpListen(llvm::StringRef host_and_port, bool child_processes_inhe Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION)); if (log) - log->Printf ("ConnectionFileDescriptor::SocketListen (%s)", host_and_port.data()); + log->Printf ("Socket::TcpListen (%s)", host_and_port.data()); std::string host_str; std::string port_str; @@ -204,16 +225,14 @@ Error Socket::TcpListen(llvm::StringRef host_and_port, bool child_processes_inhe int err = ::bind (listen_sock, anyaddr, anyaddr.GetLength()); if (err == -1) { - // TODO: On Windows, use WSAGetLastError() - error.SetErrorToErrno(); + SetLastError (error); return error; } - err = ::listen (listen_sock, 1); + err = ::listen (listen_sock, backlog); if (err == -1) { - // TODO: On Windows, use WSAGetLastError() - error.SetErrorToErrno(); + SetLastError (error); return error; } @@ -222,7 +241,7 @@ Error Socket::TcpListen(llvm::StringRef host_and_port, bool child_processes_inhe // as port zero is a special code for "find an open port // for me". if (port == 0) - port = listen_socket->GetPortNumber(); + port = listen_socket->GetLocalPortNumber(); // Set the port predicate since when doing a listen://<host>:<port> // it often needs to accept the incoming connection which is a blocking @@ -230,7 +249,7 @@ Error Socket::TcpListen(llvm::StringRef host_and_port, bool child_processes_inhe // us to wait for the port predicate to be set to a non-zero value from // another thread in an efficient manor. if (predicate) - predicate->SetValue(port, eBroadcastAlways); + predicate->SetValue (port, eBroadcastAlways); socket = listen_socket.release(); } @@ -284,8 +303,7 @@ Error Socket::BlockingAccept(llvm::StringRef host_and_port, bool child_processes if (sock == kInvalidSocketValue) { - // TODO: On Windows, use WSAGetLastError() - error.SetErrorToErrno(); + SetLastError (error); break; } @@ -349,8 +367,7 @@ Error Socket::UdpConnect(llvm::StringRef host_and_port, bool child_processes_inh if (final_recv_fd == kInvalidSocketValue) { // Socket creation failed... - // TODO: On Windows, use WSAGetLastError(). - error.SetErrorToErrno(); + SetLastError (error); } else { @@ -363,8 +380,7 @@ Error Socket::UdpConnect(llvm::StringRef host_and_port, bool child_processes_inh if (::bind (final_recv_fd, addr, addr.GetLength()) == -1) { // Bind failed... - // TODO: On Windows use WSAGetLastError() - error.SetErrorToErrno(); + SetLastError (error); } } @@ -415,8 +431,7 @@ Error Socket::UdpConnect(llvm::StringRef host_and_port, bool child_processes_inh if (final_send_fd == kInvalidSocketValue) { - // TODO: On Windows, use WSAGetLastError(). - error.SetErrorToErrno(); + SetLastError (error); return error; } @@ -437,7 +452,7 @@ Error Socket::UnixDomainConnect(llvm::StringRef name, bool child_processes_inher int fd = ::CreateSocket (AF_UNIX, SOCK_STREAM, 0, child_processes_inherit); if (fd == kInvalidSocketValue) { - error.SetErrorToErrno(); + SetLastError (error); return error; } @@ -452,7 +467,7 @@ Error Socket::UnixDomainConnect(llvm::StringRef name, bool child_processes_inher if (::connect (fd, (struct sockaddr *)&saddr_un, SUN_LEN (&saddr_un)) < 0) { - error.SetErrorToErrno(); + SetLastError (error); return error; } @@ -476,7 +491,7 @@ Error Socket::UnixDomainAccept(llvm::StringRef name, bool child_processes_inheri listen_fd = ::CreateSocket (AF_UNIX, SOCK_STREAM, 0, child_processes_inherit); if (listen_fd == kInvalidSocketValue) { - error.SetErrorToErrno(); + SetLastError (error); return error; } @@ -489,7 +504,7 @@ Error Socket::UnixDomainAccept(llvm::StringRef name, bool child_processes_inheri saddr_un.sun_len = SUN_LEN (&saddr_un); #endif - FileSystem::Unlink(name.data()); + FileSystem::Unlink(FileSpec{name, true}); bool success = false; if (::bind (listen_fd, (struct sockaddr *)&saddr_un, SUN_LEN (&saddr_un)) == 0) { @@ -506,7 +521,7 @@ Error Socket::UnixDomainAccept(llvm::StringRef name, bool child_processes_inheri if (!success) { - error.SetErrorToErrno(); + SetLastError (error); return error; } // We are done with the listen port @@ -533,13 +548,18 @@ Socket::DecodeHostAndPort(llvm::StringRef host_and_port, if (regex_match.GetMatchAtIndex (host_and_port.data(), 1, host_str) && regex_match.GetMatchAtIndex (host_and_port.data(), 2, port_str)) { - port = Args::StringToSInt32 (port_str.c_str(), INT32_MIN); - if (port != INT32_MIN) + bool ok = false; + port = StringConvert::ToUInt32 (port_str.c_str(), UINT32_MAX, 10, &ok); + if (ok && port < UINT16_MAX) { if (error_ptr) error_ptr->Clear(); return true; } + // port is too large + if (error_ptr) + error_ptr->SetErrorStringWithFormat("invalid host:port specification: '%s'", host_and_port.data()); + return false; } } @@ -547,10 +567,13 @@ Socket::DecodeHostAndPort(llvm::StringRef host_and_port, // a port with an empty host. host_str.clear(); port_str.clear(); - port = Args::StringToSInt32(host_and_port.data(), INT32_MIN); - if (port != INT32_MIN) + bool ok = false; + port = StringConvert::ToUInt32 (host_and_port.data(), UINT32_MAX, 10, &ok); + if (ok && port < UINT16_MAX) { port_str = host_and_port; + if (error_ptr) + error_ptr->Clear(); return true; } @@ -572,12 +595,11 @@ Error Socket::Read (void *buf, size_t &num_bytes) do { bytes_received = ::recv (m_socket, static_cast<char *>(buf), num_bytes, 0); - // TODO: Use WSAGetLastError on windows. - } while (bytes_received < 0 && errno == EINTR); + } while (bytes_received < 0 && IsInterrupted ()); if (bytes_received < 0) { - error.SetErrorToErrno(); + SetLastError (error); num_bytes = 0; } else @@ -615,13 +637,11 @@ Error Socket::Write (const void *buf, size_t &num_bytes) } else bytes_sent = ::send (m_socket, static_cast<const char *>(buf), num_bytes, 0); - // TODO: Use WSAGetLastError on windows. - } while (bytes_sent < 0 && errno == EINTR); + } while (bytes_sent < 0 && IsInterrupted ()); if (bytes_sent < 0) { - // TODO: On Windows, use WSAGEtLastError. - error.SetErrorToErrno(); + SetLastError (error); num_bytes = 0; } else @@ -667,8 +687,7 @@ Error Socket::Close() m_socket = kInvalidSocketValue; if (!success) { - // TODO: On Windows, use WSAGetLastError(). - error.SetErrorToErrno(); + SetLastError (error); } return error; @@ -688,10 +707,10 @@ int Socket::SetOption(int level, int option_name, int option_value) return ::setsockopt(m_socket, level, option_name, option_value_p, sizeof(option_value)); } -uint16_t Socket::GetPortNumber(const NativeSocket& socket) +uint16_t Socket::GetLocalPortNumber(const NativeSocket& socket) { // We bound to port zero, so we need to figure out which port we actually bound to - if (socket >= 0) + if (socket != kInvalidSocketValue) { SocketAddress sock_addr; socklen_t sock_addr_len = sock_addr.GetMaxLength (); @@ -702,7 +721,47 @@ uint16_t Socket::GetPortNumber(const NativeSocket& socket) } // Return the port number that is being used by the socket. -uint16_t Socket::GetPortNumber() const +uint16_t Socket::GetLocalPortNumber() const +{ + return GetLocalPortNumber (m_socket); +} + +std::string Socket::GetLocalIPAddress () const +{ + // We bound to port zero, so we need to figure out which port we actually bound to + if (m_socket != kInvalidSocketValue) + { + SocketAddress sock_addr; + socklen_t sock_addr_len = sock_addr.GetMaxLength (); + if (::getsockname (m_socket, sock_addr, &sock_addr_len) == 0) + return sock_addr.GetIPAddress (); + } + return ""; +} + +uint16_t Socket::GetRemotePortNumber () const +{ + if (m_socket != kInvalidSocketValue) + { + SocketAddress sock_addr; + socklen_t sock_addr_len = sock_addr.GetMaxLength (); + if (::getpeername (m_socket, sock_addr, &sock_addr_len) == 0) + return sock_addr.GetPort (); + } + return 0; +} + +std::string Socket::GetRemoteIPAddress () const { - return GetPortNumber(m_socket); + // We bound to port zero, so we need to figure out which port we actually bound to + if (m_socket != kInvalidSocketValue) + { + SocketAddress sock_addr; + socklen_t sock_addr_len = sock_addr.GetMaxLength (); + if (::getpeername (m_socket, sock_addr, &sock_addr_len) == 0) + return sock_addr.GetIPAddress (); + } + return ""; } + + |