1"""
2Test target commands: target.auto-install-main-executable.
3"""
4
5import time
6import gdbremote_testcase
7
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class TestAutoInstallMainExecutable(gdbremote_testcase.GdbRemoteTestCaseBase):
14    mydir = TestBase.compute_mydir(__file__)
15
16    @llgs_test
17    @no_debug_info_test
18    @skipIf(remote=False)
19    @expectedFailureAll(hostoslist=["windows"], triple='.*-android')
20    def test_target_auto_install_main_executable(self):
21        self.build()
22
23        # Manually install the modified binary.
24        working_dir = lldb.remote_platform.GetWorkingDirectory()
25        src_device = lldb.SBFileSpec(self.getBuildArtifact("a.device.out"))
26        dest = lldb.SBFileSpec(os.path.join(working_dir, "a.out"))
27        err = lldb.remote_platform.Put(src_device, dest)
28        if err.Fail():
29            raise RuntimeError(
30                "Unable copy '%s' to '%s'.\n>>> %s" %
31                (src_device.GetFilename(), working_dir, err.GetCString()))
32
33        m = re.search("^(.*)://([^/]*):(.*)$", configuration.lldb_platform_url)
34        protocol = m.group(1)
35        hostname = m.group(2)
36        hostport = int(m.group(3))
37        listen_url = "*:"+str(hostport+1)
38
39        commandline_args = [
40            "platform",
41            "--listen",
42            listen_url,
43            "--server"
44            ]
45
46        self.spawnSubprocess(
47            self.debug_monitor_exe,
48            commandline_args,
49            install_remote=False)
50
51        # Wait for the new process gets ready.
52        time.sleep(0.1)
53
54        self.dbg.SetAsync(False)
55
56        new_platform = lldb.SBPlatform(lldb.remote_platform.GetName())
57        self.dbg.SetSelectedPlatform(new_platform)
58
59        connect_url = "%s://%s:%s" % (protocol, hostname, str(hostport+1))
60
61        # Test the default setting.
62        self.expect("settings show target.auto-install-main-executable",
63                substrs=["target.auto-install-main-executable (boolean) = true"],
64                msg="Default settings for target.auto-install-main-executable failed.")
65
66        # Disable the auto install.
67        self.runCmd("settings set target.auto-install-main-executable false")
68        self.expect("settings show target.auto-install-main-executable",
69            substrs=["target.auto-install-main-executable (boolean) = false"])
70
71        self.runCmd("platform select %s"%configuration.lldb_platform_name)
72        self.runCmd("platform connect %s" % (connect_url))
73
74        # Create the target with the original file.
75        self.runCmd("target create --remote-file %s %s "%
76                                        (os.path.join(working_dir,dest.GetFilename()),
77                                            self.getBuildArtifact("a.out")))
78
79        target = self.dbg.GetSelectedTarget()
80        breakpoint = target.BreakpointCreateByName("main")
81
82        launch_info = target.GetLaunchInfo()
83        error = lldb.SBError()
84        process = target.Launch(launch_info, error)
85        self.assertTrue(process, PROCESS_IS_VALID)
86
87        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
88        self.assertTrue(
89            thread.IsValid(),
90            "There should be a thread stopped due to breakpoint")
91
92        frame = thread.GetFrameAtIndex(0)
93        self.assertEqual(frame.GetFunction().GetName(), "main")
94
95        self.expect("target variable build", substrs=['"device"'],
96                msg="Magic in the binary is wrong")
97
98        process.Continue()
99