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