xref: /llvm-project/lldb/test/API/commands/platform/launchgdbserver/TestPlatformLaunchGDBServer.py (revision 2e89312419c5f7875c947fcf79ea0446cf65a287)
1""" Check that errors while handling qLaunchGDBServer are reported to the user.
2    Though this isn't a platform command in itself, the best way to test it is
3    from Python because we can juggle multiple processes more easily.
4"""
5
6import os
7import socket
8import shutil
9import lldbgdbserverutils
10from lldbsuite.test.decorators import *
11from lldbsuite.test.lldbtest import *
12from lldbsuite.test import lldbutil
13
14
15class TestPlatformProcessLaunchGDBServer(TestBase):
16    NO_DEBUG_INFO_TESTCASE = True
17
18    @skipIfRemote
19    # Windows cannot delete the executable while it is running.
20    # On Darwin we may be using debugserver.
21    @skipUnlessPlatform(["linux"])
22    @add_test_categories(["lldb-server"])
23    def test_platform_process_launch_gdb_server(self):
24        self.build()
25
26        hostname = socket.getaddrinfo("localhost", 0, proto=socket.IPPROTO_TCP)[0][4][0]
27        listen_url = "[%s]:0" % hostname
28
29        port_file = self.getBuildArtifact("port")
30        commandline_args = [
31            "platform",
32            "--listen",
33            listen_url,
34            "--socket-file",
35            port_file,
36            "--",
37            self.getBuildArtifact("a.out"),
38            "foo",
39        ]
40
41        # Run lldb-server from a new location.
42        new_lldb_server = self.getBuildArtifact("lldb-server")
43        shutil.copy(lldbgdbserverutils.get_lldb_server_exe(), new_lldb_server)
44
45        self.spawnSubprocess(new_lldb_server, commandline_args)
46        socket_id = lldbutil.wait_for_file_on_target(self, port_file)
47
48        new_platform = lldb.SBPlatform("remote-" + self.getPlatform())
49        self.dbg.SetSelectedPlatform(new_platform)
50
51        connect_url = "connect://[%s]:%s" % (hostname, socket_id)
52        self.runCmd("platform connect %s" % connect_url)
53
54        # First connect to lldb-server which spawn a process to handle the connection.
55        # Then remove our new lldb-server so that when it tries to invoke itself as a
56        # gdbserver, it fails.
57        os.remove(new_lldb_server)
58
59        self.runCmd("target create {}".format(self.getBuildArtifact("a.out")))
60        self.expect("run", substrs=["unable to launch a GDB server on"], error=True)
61