summaryrefslogtreecommitdiff
path: root/lldb/source/Host/common/TCPSocket.cpp
AgeCommit message (Collapse)Author
2025-06-23[lldb] Add Socket::CreatePair (#145015)Pavel Labath
It creates a pair of connected sockets using the simplest mechanism for the given platform (TCP on windows, socketpair(2) elsewhere). Main motivation is to remove the ugly platform-specific code in ProcessGDBRemote::LaunchAndConnectToDebugserver, but it can also be used in other places where we need to create a pair of connected sockets.
2025-05-20[lldb] Extend information for failed connection for gdb server (#139916)ita-sc
Before: ``` (lldb) r error: connect remote failed (Failed to connect port) error: Failed to connect port ``` After the patch: ``` (lldb) r error: connect remote failed (Failed to connect localhost:47140) error: Failed to connect localhost:47140 ```
2024-12-04[lldb] Correct an issue when using Socket to listen on `localhost:0` on ipv4 ↵John Harrison
and ipv6. (#118565) On systems supporting ting ipv4 and ipv6 the second socket to initialize will not update the listening address correctly after the call to `bind`. This results in the second address listed in `Socket::GetListeningConnectionURI` to have port `:0`, which is incorrect. To fix this, correct which address is used to detect the port and update the unit tests to cover this use case. Additionally, I updated the SocketTest's to only parameterize tests that can work on ipv4 or ipv6. This means tests like `SocketTest::DecodeHostAndPort` are only run once, instead of twice since they do not change behavior based on parameters. I also included a new unit test to cover listening on `localhost:0`, validating both sockets correctly list the updated port.
2024-12-03[lldb] For a host socket, add a method to print the listening address. (#118330)John Harrison
This is most useful if you are listening on an address like 'localhost:0' and want to know the resolved ip + port of the socket listener.
2024-11-28[lldb] Remove child_process_inherit from the socket classes (#117699)Pavel Labath
It's never set to true. Also, using inheritable FDs in a multithreaded process pretty much guarantees descriptor leaks. It's better to explicitly pass a specific FD to a specific subprocess, which we already mostly can do using the ProcessLaunchInfo FileActions.
2024-09-13[lldb] Add a MainLoop version of DomainSocket::Accept (#108188)Pavel Labath
To go along with the existing TCPSocket implementation.
2024-09-05[lldb] Make conversions from llvm::Error explicit with Status::FromEr… ↵Adrian Prantl
(#107163) …ror() [NFC]
2024-09-04[lldb][NFC] Move few static helpers to the class Socket (#106640)Dmitry Vasilyev
Fixed a typo in Socket::SetOption().
2024-09-03[lldb] Add a callback version of TCPSocket::Accept (#106955)Pavel Labath
The existing function already used the MainLoop class, which allows one to wait on multiple events at once. It needed to do this in order to wait for v4 and v6 connections simultaneously. However, since it was creating its own instance of MainLoop, this meant that it was impossible to multiplex these sockets with anything else. This patch simply adds a version of this function which uses an externally provided main loop instance, which allows the caller to add any events it deems necessary. The previous function becomes a very thin wrapper over the new one.
2024-08-27[lldb] Turn lldb_private::Status into a value type. (#106163)Adrian Prantl
This patch removes all of the Set.* methods from Status. This cleanup is part of a series of patches that make it harder use the anti-pattern of keeping a long-lives Status object around and updating it while dropping any errors it contains on the floor. This patch is largely NFC, the more interesting next steps this enables is to: 1. remove Status.Clear() 2. assert that Status::operator=() never overwrites an error 3. remove Status::operator=() Note that step (2) will bring 90% of the benefits for users, and step (3) will dramatically clean up the error handling code in various places. In the end my goal is to convert all APIs that are of the form ` ResultTy DoFoo(Status& error) ` to ` llvm::Expected<ResultTy> DoFoo() ` How to read this patch? The interesting changes are in Status.h and Status.cpp, all other changes are mostly ` perl -pi -e 's/\.SetErrorString/ = Status::FromErrorString/g' $(git grep -l SetErrorString lldb/source) ` plus the occasional manual cleanup.
2023-06-06[lldb] Remove __FUNCTION__ from log messages in lldbHost (NFC)Jonas Devlieghere
LLDB's logging infrastructure supports prepending log messages with the name of the file and function that generates the log (see help log enable). Therefore it's unnecessary to include the current __FUNCTION__ in the log message itself. This patch removes __FUNCTION__ from log messages in the Host library. Differential revision: https://reviews.llvm.org/D151762
2022-08-30[lldb] Use the NativeSock type instead of plain 'int'Martin Storsjö
This fixes a warning when building for Windows: ../tools/lldb/source/Host/common/TCPSocket.cpp:297:16: warning: comparison of integers of different signs: 'int' and 'const NativeSocket' (aka 'const unsigned long long') [-Wsign-compare] if (sock != kInvalidSocketValue) { ~~~~ ^ ~~~~~~~~~~~~~~~~~~~ Differential Revision: https://reviews.llvm.org/D132841
2022-08-11[LLDB][NFC] Fix the style issue in TCPSocketSlava Gurevich
Style fixes for the entire file Differential Revision: https://reviews.llvm.org/D131543
2022-08-06[LLDB][NFC] Reliability fixes to TCPSocket codeSlava Gurevich
Patch the following issues found by static code inspection: - Unchecked return values from lib calls - Passing potentially negative arg into a function that requires non-negative input - Possible socket double-close Differential Revision: https://reviews.llvm.org/D131294
2022-06-14[lldb] Fix TCPSocket::Connect when getaddrinfo returns multiple addrsPavel Labath
TCPSocket::Connect() calls SocketAddress::GetAddressInfo() and tries to connect any of them (in a for loop). This used to work before commit 4f6d3a376c9f("[LLDB] Fix setting of success in Socket::Close()") https://reviews.llvm.org/D116768. As a side effect of that commit, TCPSocket can only connect to the first address returned by SocketAddress::GetAddressInfo(). 1. If the attempt to connect to the first address fails, TCPSocket::Connect(), calls CLOSE_SOCKET(GetNativeSocket()), which closes the fd, but DOES NOT set m_socket to kInvalidSocketValue. 2. On the second attempt, TCPSocket::CreateSocket() calls Socket::Close(). 3. Socket::Close() proceeds, because IsValid() is true (m_socket was not reset on step 1). 4. Socket::Close() calls ::close(m_socket), which fails 5. Since commit 4f6d3a376c9f("[LLDB] Fix setting of success in Socket::Close()"), this is error is detected. Socket::Close() returns an error. 6. TCPSocket::CreateSocket() therefore returns an error. 7. TCPSocket::Connect() detects the error and continues, skipping the second (and the third, fourth...) address. This commit fixes the problem by changing step 1: by calling Socket::Close, instead of directly calling close(m_socket), m_socket is also se to kInvalidSocketValue. On step 3, Socket::Close() is going to return immediately and, on step 6, TCPSocket::CreateSocket() does not fail. How to reproduce this problem: On my system, getaddrinfo() resolves "localhost" to "::1" (first) and to "127.0.0.1" (second). Start a gdbserver that only listens on 127.0.0.1: ``` gdbserver 127.0.0.1:2159 /bin/cat Process /bin/cat created; pid = 2146709 Listening on port 2159 ``` Start lldb and make it connect to "localhost:2159" ``` ./bin/lldb (lldb) gdb-remote localhost:2159 ``` Before 4f6d3a376c9f("[LLDB] Fix setting of success in Socket::Close()"), this used to work. After that commit, it stopped working. This commit fixes the problem. Reviewed By: labath Differential Revision: https://reviews.llvm.org/D126702
2022-02-03[lldb] Rename Logging.h to LLDBLog.h and clean up includesPavel Labath
Most of our code was including Log.h even though that is not where the "lldb" log channel is defined (Log.h defines the generic logging infrastructure). This worked because Log.h included Logging.h, even though it should. After the recent refactor, it became impossible the two files include each other in this direction (the opposite inclusion is needed), so this patch removes the workaround that was put in place and cleans up all files to include the right thing. It also renames the file to LLDBLog to better reflect its purpose.
2022-02-02[lldb] Convert "LLDB" log channel to the new APIPavel Labath
2021-10-28[lldb] [Host/Socket] Make DecodeHostAndPort() return a dedicated structMichał Górny
Differential Revision: https://reviews.llvm.org/D112629
2021-10-05[lldb] Remove some anonymous namespacesPavel Labath
.. and reduce the scope of others. They don't follow llvm coding standards (which say they should be used only when the same effect cannot be achieved with the static keyword), and they set a bad example.
2021-09-24[lldb] [Host] Refactor Socket::DecodeHostAndPort() to use LLVM APIMichał Górny
Refactor Socket::DecodeHostAndPort() to use LLVM API over redundant LLDB API. In particular, this means llvm::Regex, llvm::Error return type and llvm::to_integer(). While at it, change the port type from int32_t to uint16_t. The method never returns any value outside this range, and using the correct type allows us to rely on getAsInteger()'s implicit overflow check. Differential Revision: https://reviews.llvm.org/D110391
2021-09-24Revert "[lldb] [Host] Refactor Socket::DecodeHostAndPort() to use LLVM API"Michał Górny
This reverts commit a6daf99228bc16fb7f2596d67a0d00fef327ace5. It causes buildbot regressions, I'll investigate.
2021-09-24[lldb] [Host] Refactor Socket::DecodeHostAndPort() to use LLVM APIMichał Górny
Refactor Socket::DecodeHostAndPort() to use LLVM API over redundant LLDB API. In particular, this means llvm::Regex, llvm::Error return type and llvm::to_integer(). While at it, change the port type from int32_t to uint16_t. The method never returns any value outside this range, and using the correct type allows us to rely on getAsInteger()'s implicit overflow check. Differential Revision: https://reviews.llvm.org/D110391
2020-08-13[lldb][NFC] Fix indentation in TCPSocket::CloseListenSocketsRaphael Isemann
2020-04-27Fix up a clang-tidy nit about using empty rather than size == 0.Eric Christopher
2020-04-27[lldb] Fix windows build break from 18e96a31Pavel Labath
2020-04-27[lldb/unittests] Skip IPv6 test on systems which don't have IPv6 configuredPavel Labath
Sadly IPv6 is still not present anywhere. The test was attempting to detect&skip such hosts, but the way it did that (essentially, by calling getaddrinfo) meant that it only detected hosts which have IPv6 support completely compiled out. It did not do anything about hosts which have it compiled in, but lack runtime configuration even for the ::1 loopback address. This patch changes the detection logic to use a new method. It does it by attempting to bind a socket to the appropriate loopback address. That should ensure the hosts loopback interface is fully set up. In an effort to avoid silently skipping the test on too many hosts, the test is fairly strict about the kind of error it expects in these cases -- it will only skip the test when receiving EADDRNOTAVAIL. If we find other error codes that can be reasonably returned in these situations, we can add more of them. The (small) change in TCPSocket.cpp is to ensure that the code correctly propagates the error received from the OS.
2020-01-28Make llvm::StringRef to std::string conversions explicit.Benjamin Kramer
This is how it should've been and brings it more in line with std::string_view. There should be no functional change here. This is mostly mechanical from a custom clang-tidy check, with a lot of manual fixups. It uncovers a lot of minor inefficiencies. This doesn't actually modify StringRef yet, I'll do that in a follow-up.
2020-01-24[lldb][NFC] Fix all formatting errors in .cpp file headersRaphael Isemann
Summary: A *.cpp file header in LLDB (and in LLDB) should like this: ``` //===-- TestUtilities.cpp -------------------------------------------------===// ``` However in LLDB most of our source files have arbitrary changes to this format and these changes are spreading through LLDB as folks usually just use the existing source files as templates for their new files (most notably the unnecessary editor language indicator `-*- C++ -*-` is spreading and in every review someone is pointing out that this is wrong, resulting in people pointing out that this is done in the same way in other files). This patch removes most of these inconsistencies including the editor language indicators, all the different missing/additional '-' characters, files that center the file name, missing trailing `===//` (mostly caused by clang-format breaking the line). Reviewers: aprantl, espindola, jfb, shafik, JDevlieghere Reviewed By: JDevlieghere Subscribers: dexonsmith, wuzish, emaste, sdardis, nemanjai, kbarton, MaskRay, atanasyan, arphaman, jfb, abidh, jsji, JDevlieghere, usaxena95, lldb-commits Tags: #lldb Differential Revision: https://reviews.llvm.org/D73258
2019-12-13[lldb/Host] Use cmakedefine01 for LLDB_ENABLE_POSIXJonas Devlieghere
Rename LLDB_DISABLE_POSIX to LLDB_ENABLE_POSIX and use cmakedefine01 for consistency.
2019-11-12[lldb] Fix more -Wdeprecated-copy warningsPavel Labath
This warning triggers when a class defines a copy constructor but not a copy-assignment operator (which then gets auto-generated by the compiler). Fix the warning by deleting the other operator too, as the default implementation works just fine.
2019-07-24[Logging] Replace Log::Printf with LLDB_LOG macro (NFC)Jonas Devlieghere
This patch replaces explicit calls to log::Printf with the new LLDB_LOGF macro. The macro is similar to LLDB_LOG but supports printf-style format strings, instead of formatv-style format strings. So instead of writing: if (log) log->Printf("%s\n", str); You'd write: LLDB_LOG(log, "%s\n", str); This change was done mechanically with the command below. I replaced the spurious if-checks with vim, since I know how to do multi-line replacements with it. find . -type f -name '*.cpp' -exec \ sed -i '' -E 's/log->Printf\(/LLDB_LOGF\(log, /g' "{}" + Differential revision: https://reviews.llvm.org/D65128 llvm-svn: 366936
2019-06-01Silence 'warning: extra ‘;’ [-Wpedantic]' with GCC 7.3Alexandre Ganea
llvm-svn: 362306
2019-05-30Make ConnectionFileDescription work with all socketsAntonio Afonso
Summary: My main goal here is to make lldb-server work with Android Studio. This is currently not the case because lldb-server is started in platform mode listening on a domain socket. When Android Studio connects to it lldb-server crashes because even though it's listening on a domain socket as soon as it gets a connection it asserts that it's a TCP connection, which will obviously fails for any non-tcp connection. To do this I came up with a new method called GetConnectURI() in Socket that returns the URI needed to connect to the connected portion of the socket. Reviewers: labath, clayborg, xiaobai Reviewed By: labath Subscribers: mgorny, jfb, lldb-commits Tags: #lldb Differential Revision: https://reviews.llvm.org/D62089 llvm-svn: 362173
2019-05-23[lldb] NFC modernize codebase with modernize-use-nullptrKonrad Kleine
Summary: NFC = [[ https://llvm.org/docs/Lexicon.html#nfc | Non functional change ]] This commit is the result of modernizing the LLDB codebase by using `nullptr` instread of `0` or `NULL`. See https://clang.llvm.org/extra/clang-tidy/checks/modernize-use-nullptr.html for more information. This is the command I ran and I to fix and format the code base: ``` run-clang-tidy.py \ -header-filter='.*' \ -checks='-*,modernize-use-nullptr' \ -fix ~/dev/llvm-project/lldb/.* \ -format \ -style LLVM \ -p ~/llvm-builds/debug-ninja-gcc ``` NOTE: There were also changes to `llvm/utils/unittest` but I did not include them because I felt that maybe this library shall be updated in isolation somehow. NOTE: I know this is a rather large commit but it is a nobrainer in most parts. Reviewers: martong, espindola, shafik, #lldb, JDevlieghere Reviewed By: JDevlieghere Subscribers: arsenm, jvesely, nhaehnle, hiraditya, JDevlieghere, teemperor, rnkovacs, emaste, kubamracek, nemanjai, ki.stfu, javed.absar, arichardson, kbarton, jrtc27, MaskRay, atanasyan, dexonsmith, arphaman, jfb, jsji, jdoerfert, lldb-commits, llvm-commits Tags: #lldb, #llvm Differential Revision: https://reviews.llvm.org/D61847 llvm-svn: 361484
2019-03-21[lldb] Add missing EINTR handlingMichal Gorny
Differential Revision: https://reviews.llvm.org/D59606 llvm-svn: 356703
2019-01-19Update the file headers across all of the LLVM projects in the monorepoChandler Carruth
to reflect the new license. We understand that people may be surprised that we're moving the header entirely to discuss the new license. We checked this carefully with the Foundation's lawyer and we believe this is the correct approach. Essentially, all code in the project is now made available by the LLVM project under our new license, so you will see that the license headers include that license only. Some of our contributors have contributed code under our old license, and accordingly, we have retained a copy of our old license notice in the top-level files in each project and repository. llvm-svn: 351636
2018-04-10s/LLVM_ON_WIN32/_WIN32/, lldbNico Weber
LLVM_ON_WIN32 is set exactly with MSVC and MinGW (but not Cygwin) in HandleLLVMOptions.cmake, which is where _WIN32 defined too. Just use the default macro instead of a reinvented one. See thread "Replacing LLVM_ON_WIN32 with just _WIN32" on llvm-dev and cfe-dev. No intended behavior change. llvm-svn: 329697
2017-08-29[IPv6] Fix a bug in the IPv6 listen behaviorChris Bieneman
The socket bind address should either be localhost or anyaddress. This bug in the listen behavior was preventing lldb-server from opening sockets for non-localhost connections. The added test verifies that opening an anyaddress socket works and has a non-zero port assignment. This should resolve PR34183. llvm-svn: 312008
2017-07-18Clean up lldb-types.hPavel Labath
Summary: It defined a couple of types (condition_t) which we don't use anymore, as we have c++11 goodies now. I remove these definitions. Also it unnecessarily included a couple of headers which weren't necessary for it's operation. I remove these, and place the includes in the relevant files (usually .cpp, usually in Host code) which use them. This allows us to reduce namespace pollution in most of the lldb files which don't need the OS-specific definitions. Reviewers: zturner, jingham Subscribers: ki.stfu, lldb-commits Differential Revision: https://reviews.llvm.org/D35113 llvm-svn: 308304
2017-05-12Rename Error -> Status.Zachary Turner
This renames the LLDB error class to Status, as discussed on the lldb-dev mailing list. A change of this magnitude cannot easily be done without find and replace, but that has potential to catch unwanted occurrences of common strings such as "Error". Every effort was made to find all the obvious things such as the word "Error" appearing in a string, etc, but it's possible there are still some lingering occurences left around. Hopefully nothing too serious. llvm-svn: 302872
2017-04-28Resurrect the standalone build of LLDBKamil Rytarowski
Switch includes "llvm/Config/config.h" to "llvm/Config/llvm-config.h". Tested on NetBSD 7.99.70 amd64 llvm-svn: 301603
2017-04-27TCPSocket: add back support for "*" addressPavel Labath
before r301492, we could specify "*:1234" as an address to lldb-server and it would interpret that as "any". I am not sure that's a good idea, but we have usages of that in the test suite, and without this the remote test suite fails. I'm adding that back, as it does not seem it was an intended side-effect of that change, but I am open to removing it in the future, after discussion and test suite fixup. llvm-svn: 301534
2017-04-27One more try at the whole compiling thing...Chris Bieneman
Need to actually use the right type in both parts of the cast. llvm-svn: 301506
2017-04-27One more attempt to fix the broken bots.Chris Bieneman
llvm-svn: 301504
2017-04-27Fix Windows bots broken by r301492Chris Bieneman
http://lab.llvm.org:8011/builders/lldb-x86-windows-msvc2015/builds/8644/ llvm-svn: 301502
2017-04-26Re-landing IPv6 support for LLDB HostChris Bieneman
This support was landed in r300579, and reverted in r300669 due to failures on the bots. The failures were caused by sockets not being properly closed, and this updated version of the patches should resolve that. Summary from the original change: This patch adds IPv6 support to LLDB/Host's TCP socket implementation. Supporting IPv6 involved a few significant changes to the implementation of the socket layers, and I have performed some significant code cleanup along the way. This patch changes the Socket constructors for all types of sockets to not create sockets until first use. This is required for IPv6 support because the socket type will vary based on the address you are connecting to. This also has the benefit of removing code that could have errors from the Socket subclass constructors (which seems like a win to me). The patch also slightly changes the API and behaviors of the Listen/Accept pattern. Previously both Listen and Accept calls took an address specified as a string. Now only listen does. This change was made because the Listen call can result in opening more than one socket. In order to support listening for both IPv4 and IPv6 connections we need to open one AF_INET socket and one AF_INET6 socket. During the listen call we construct a map of file descriptors to addrin structures which represent the allowable incoming connection address. This map removes the need for taking an address into the Accept call. This does have a change in functionality. Previously you could Listen for connections based on one address, and Accept connections from a different address. This is no longer supported. I could not find anywhere in LLDB where we actually used the APIs in that way. The new API does still support AnyAddr for allowing incoming connections from any address. The Listen implementation is implemented using kqueue on FreeBSD and Darwin, WSAPoll on Windows and poll(2) everywhere else. https://reviews.llvm.org/D31823 llvm-svn: 301492
2017-04-19Revert yesterdays IPv6 patchesPavel Labath
The break the linux bots (and probably any other machine which would run the test suite in a massively parallel way). The problem is that it can happen that we only successfully create an IPv6 listening socket (because the relevant IPv4 port is used by another process) and then the connecting side attempts to connect to the IPv4 port and fails. It's not very obvious how to fix this problem, so I am reverting this until we come up with a solution. llvm-svn: 300669
2017-04-18Update LLDB Host to support IPv6 over TCPChris Bieneman
Summary: This patch adds IPv6 support to LLDB/Host's TCP socket implementation. Supporting IPv6 involved a few significant changes to the implementation of the socket layers, and I have performed some significant code cleanup along the way. This patch changes the Socket constructors for all types of sockets to not create sockets until first use. This is required for IPv6 support because the socket type will vary based on the address you are connecting to. This also has the benefit of removing code that could have errors from the Socket subclass constructors (which seems like a win to me). The patch also slightly changes the API and behaviors of the Listen/Accept pattern. Previously both Listen and Accept calls took an address specified as a string. Now only listen does. This change was made because the Listen call can result in opening more than one socket. In order to support listening for both IPv4 and IPv6 connections we need to open one AF_INET socket and one AF_INET6 socket. During the listen call we construct a map of file descriptors to addrin structures which represent the allowable incoming connection address. This map removes the need for taking an address into the Accept call. This does have a change in functionality. Previously you could Listen for connections based on one address, and Accept connections from a different address. This is no longer supported. I could not find anywhere in LLDB where we actually used the APIs in that way. The new API does still support AnyAddr for allowing incoming connections from any address. The Listen implementation is implemented using kqueue on FreeBSD and Darwin, WSAPoll on Windows and poll(2) everywhere else. Reviewers: zturner, clayborg Subscribers: jasonmolenda, labath, lldb-commits, emaste Differential Revision: https://reviews.llvm.org/D31823 llvm-svn: 300579
2017-03-03Move Log from Core -> Utility.Zachary Turner
All references to Host and Core have been removed, so this class can now safely be lowered into Utility. Differential Revision: https://reviews.llvm.org/D30559 llvm-svn: 296909
2016-11-26[lldb] Fix typos in file headersAlexander Shaposhnikov
This diff fixes typos in file headers (incorrect file names). Test plan: Under llvm/tools/lldb/source: find ./* -type f | grep -e '\(cpp\|h\)$' | while read F; do B=$(basename $F); echo $F head -n 1 $F | grep -v $B | wc -l ; done Differential revision: https://reviews.llvm.org/D27115 llvm-svn: 287966