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        self.init_llgs_test(False)
23
24        # Manually install the modified binary.
25        working_dir = lldb.remote_platform.GetWorkingDirectory()
26        src_device = lldb.SBFileSpec(self.getBuildArtifact("a.device.out"))
27        dest = lldb.SBFileSpec(os.path.join(working_dir, "a.out"))
28        err = lldb.remote_platform.Put(src_device, dest)
29        if err.Fail():
30            raise RuntimeError(
31                "Unable copy '%s' to '%s'.\n>>> %s" %
32                (src_device.GetFilename(), working_dir, err.GetCString()))
33
34        m = re.search("^(.*)://([^/]*):(.*)$", configuration.lldb_platform_url)
35        protocol = m.group(1)
36        hostname = m.group(2)
37        hostport = int(m.group(3))
38        listen_url = "*:"+str(hostport+1)
39
40        commandline_args = [
41            "platform",
42            "--listen",
43            listen_url,
44            "--server"
45            ]
46
47        self.spawnSubprocess(
48            self.debug_monitor_exe,
49            commandline_args,
50            install_remote=False)
51        self.addTearDownHook(self.cleanupSubprocesses)
52
53        # Wait for the new process gets ready.
54        time.sleep(0.1)
55
56        self.dbg.SetAsync(False)
57
58        new_platform = lldb.SBPlatform(lldb.remote_platform.GetName())
59        self.dbg.SetSelectedPlatform(new_platform)
60        interpreter = self.dbg.GetCommandInterpreter()
61
62        connect_url = "%s://%s:%s" % (protocol, hostname, str(hostport+1))
63
64        command = "platform connect %s" % (connect_url)
65
66        result = lldb.SBCommandReturnObject()
67
68        # Test the default setting.
69        interpreter.HandleCommand("settings show target.auto-install-main-executable", result)
70        self.assertTrue(
71            result.Succeeded() and
72            "target.auto-install-main-executable (boolean) = true" in result.GetOutput(),
73            "Default settings for target.auto-install-main-executable failed.: %s - %s" %
74            (result.GetOutput(), result.GetError()))
75
76        # Disable the auto install.
77        interpreter.HandleCommand("settings set target.auto-install-main-executable false", result)
78        interpreter.HandleCommand("settings show target.auto-install-main-executable", result)
79        self.assertTrue(
80            result.Succeeded() and
81            "target.auto-install-main-executable (boolean) = false" in result.GetOutput(),
82            "Default settings for target.auto-install-main-executable failed.: %s - %s" %
83            (result.GetOutput(), result.GetError()))
84
85        interpreter.HandleCommand("platform select %s"%configuration.lldb_platform_name, result)
86        interpreter.HandleCommand(command, result)
87
88        self.assertTrue(
89            result.Succeeded(),
90            "platform process connect failed: %s - %s" %
91            (result.GetOutput(),result.GetError()))
92
93        # Create the target with the original file.
94        interpreter.HandleCommand("target create --remote-file %s %s "%
95                                        (os.path.join(working_dir,dest.GetFilename()), self.getBuildArtifact("a.out")),
96                                      result)
97        self.assertTrue(
98            result.Succeeded(),
99            "platform create failed: %s - %s" %
100            (result.GetOutput(),result.GetError()))
101
102        target = new_debugger.GetSelectedTarget()
103        breakpoint = target.BreakpointCreateByName("main")
104
105        launch_info = lldb.SBLaunchInfo(None)
106        error = lldb.SBError()
107        process = target.Launch(launch_info, error)
108        self.assertTrue(process, PROCESS_IS_VALID)
109
110        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
111        self.assertTrue(
112            thread.IsValid(),
113            "There should be a thread stopped due to breakpoint")
114
115        frame = thread.GetFrameAtIndex(0)
116        self.assertEqual(frame.GetFunction().GetName(), "main")
117
118        interpreter.HandleCommand("target variable build", result)
119        self.assertTrue(
120            result.Succeeded() and
121            '"device"' in result.GetOutput(),
122            "Magic in the binary is wrong: %s " % result.GetOutput())
123
124        process.Continue()
125