1"""
2Test target commands: target.auto-install-main-executable.
3"""
4
5import socket
6import time
7import lldbgdbserverutils
8
9from lldbsuite.test.decorators import *
10from lldbsuite.test.lldbtest import *
11from lldbsuite.test import lldbutil
12
13
14class TestAutoInstallMainExecutable(TestBase):
15    mydir = TestBase.compute_mydir(__file__)
16    NO_DEBUG_INFO_TESTCASE = True
17
18    @skipIfRemote
19    @expectedFailureAll(oslist=["windows"]) # process modules not loaded
20    def test_target_auto_install_main_executable(self):
21        self.build()
22
23        hostname = socket.getaddrinfo("localhost", 0, proto=socket.IPPROTO_TCP)[0][4][0]
24        listen_url = "[%s]:0"%hostname
25
26        port_file = self.getBuildArtifact("port")
27        commandline_args = [
28            "platform",
29            "--listen",
30            listen_url,
31            "--socket-file",
32            port_file]
33        self.spawnSubprocess(
34            lldbgdbserverutils.get_lldb_server_exe(),
35            commandline_args)
36
37        socket_id = lldbutil.wait_for_file_on_target(self, port_file)
38
39        new_platform = lldb.SBPlatform("remote-" + self.getPlatform())
40        self.dbg.SetSelectedPlatform(new_platform)
41
42        connect_url = "connect://[%s]:%s" % (hostname, socket_id)
43        connect_opts = lldb.SBPlatformConnectOptions(connect_url)
44        self.assertSuccess(new_platform.ConnectRemote(connect_opts))
45
46        wd = self.getBuildArtifact("wd")
47        os.mkdir(wd)
48        new_platform.SetWorkingDirectory(wd)
49
50
51        # Manually install the modified binary.
52        src_device = lldb.SBFileSpec(self.getBuildArtifact("a.device.out"))
53        dest = lldb.SBFileSpec(os.path.join(wd, "a.out"))
54        self.assertSuccess(new_platform.Put(src_device, dest))
55
56        # Test the default setting.
57        self.expect("settings show target.auto-install-main-executable",
58                substrs=["target.auto-install-main-executable (boolean) = true"],
59                msg="Default settings for target.auto-install-main-executable failed.")
60
61        # Disable the auto install.
62        self.runCmd("settings set target.auto-install-main-executable false")
63        self.expect("settings show target.auto-install-main-executable",
64            substrs=["target.auto-install-main-executable (boolean) = false"])
65
66        # Create the target with the original file.
67        self.runCmd("target create --remote-file %s %s "%
68                                        (dest.fullpath,
69                                            self.getBuildArtifact("a.out")))
70
71        target = self.dbg.GetSelectedTarget()
72        breakpoint = target.BreakpointCreateByName("main")
73
74        launch_info = target.GetLaunchInfo()
75        error = lldb.SBError()
76        process = target.Launch(launch_info, error)
77        self.assertTrue(process, PROCESS_IS_VALID)
78
79        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
80        self.assertTrue(
81            thread.IsValid(),
82            "There should be a thread stopped due to breakpoint")
83
84        frame = thread.GetFrameAtIndex(0)
85        self.assertEqual(frame.GetFunction().GetName(), "main")
86
87        self.expect("target variable build", substrs=['"device"'],
88                msg="Magic in the binary is wrong")
89
90        process.Continue()
91