xref: /llvm-project/lldb/test/API/macosx/simulator/TestSimulatorPlatform.py (revision b505ed9d313653782b81bbc97979c98edb205558)
1import lldb
2from lldbsuite.test.lldbtest import *
3from lldbsuite.test.decorators import *
4import lldbsuite.test.lldbutil as lldbutil
5import json
6import unittest2
7
8
9class TestSimulatorPlatformLaunching(TestBase):
10
11    mydir = TestBase.compute_mydir(__file__)
12    NO_DEBUG_INFO_TESTCASE = True
13
14    def check_load_commands(self, expected_load_command):
15        """sanity check the built binary for the expected number of load commands"""
16        load_cmds = subprocess.check_output(
17            ['otool', '-l', self.getBuildArtifact()]
18        ).decode("utf-8")
19        found = 0
20        for line in load_cmds.split('\n'):
21            if expected_load_command in line:
22                found += 1
23        self.assertEquals(
24            found, 1, "wrong number of load commands for {}".format(
25                expected_load_command))
26
27
28    def check_debugserver(self, log, expected_platform, expected_version):
29        """scan the debugserver packet log"""
30        process_info = lldbutil.packetlog_get_process_info(log)
31        self.assertIn('ostype', process_info)
32        self.assertEquals(process_info['ostype'], expected_platform)
33        dylib_info = lldbutil.packetlog_get_dylib_info(log)
34        self.assertTrue(dylib_info)
35        aout_info = None
36        for image in dylib_info['images']:
37            if image['pathname'].endswith('a.out'):
38                aout_info = image
39        self.assertTrue(aout_info)
40        self.assertEquals(aout_info['min_version_os_name'], expected_platform)
41        if expected_version:
42            self.assertEquals(aout_info['min_version_os_sdk'], expected_version)
43
44    @skipIf(bugnumber="rdar://76995109")
45    def run_with(self, arch, os, vers, env, expected_load_command):
46        env_list = [env] if env else []
47        triple = '-'.join([arch, 'apple', os + vers] + env_list)
48        sdk = lldbutil.get_xcode_sdk(os, env)
49
50        version_min = ''
51        if not vers:
52            vers = lldbutil.get_xcode_sdk_version(sdk)
53        if env == 'simulator':
54            version_min = '-m{}-simulator-version-min={}'.format(os, vers)
55        elif os == 'macosx':
56            version_min = '-m{}-version-min={}'.format(os, vers)
57
58        sdk_root = lldbutil.get_xcode_sdk_root(sdk)
59        clang = lldbutil.get_xcode_clang(sdk)
60
61        self.build(
62            dictionary={
63                'ARCH': arch,
64                'CC': clang,
65                'ARCH_CFLAGS': '-target {} {}'.format(triple, version_min),
66                'SDKROOT': sdk_root
67            })
68
69        self.check_load_commands(expected_load_command)
70        log = self.getBuildArtifact('packets.log')
71        self.expect("log enable gdb-remote packets -f "+log)
72        lldbutil.run_to_source_breakpoint(self, "break here",
73                                          lldb.SBFileSpec("hello.c"))
74        triple_re = '-'.join([arch, 'apple', os + vers+'.*'] + env_list)
75        self.expect('image list -b -t', patterns=['a\.out '+triple_re])
76        self.check_debugserver(log, os+env, vers)
77
78    @skipIfAsan
79    @skipUnlessDarwin
80    @skipIfDarwinEmbedded
81    @apple_simulator_test('iphone')
82    @skipIfOutOfTreeDebugserver
83    def test_ios(self):
84        """Test running an iOS simulator binary"""
85        self.run_with(arch=self.getArchitecture(),
86                      os='ios', vers='', env='simulator',
87                      expected_load_command='LC_BUILD_VERSION')
88
89    @skipIfAsan
90    @skipUnlessDarwin
91    @skipIfDarwinEmbedded
92    @apple_simulator_test('appletv')
93    @skipIfOutOfTreeDebugserver
94    def test_tvos(self):
95        """Test running an tvOS simulator binary"""
96        self.run_with(arch=self.getArchitecture(),
97                      os='tvos', vers='', env='simulator',
98                      expected_load_command='LC_BUILD_VERSION')
99
100    @skipIfAsan
101    @skipUnlessDarwin
102    @skipIfDarwinEmbedded
103    @apple_simulator_test('watch')
104    @skipIfDarwin # rdar://problem/64552748
105    @skipIf(archs=['arm64','arm64e'])
106    @skipIfOutOfTreeDebugserver
107    def test_watchos_i386(self):
108        """Test running a 32-bit watchOS simulator binary"""
109        self.run_with(arch='i386',
110                      os='watchos', vers='', env='simulator',
111                      expected_load_command='LC_BUILD_VERSION')
112
113    @skipIfAsan
114    @skipUnlessDarwin
115    @skipIfDarwinEmbedded
116    @apple_simulator_test('watch')
117    @skipIfDarwin # rdar://problem/64552748
118    @skipIf(archs=['i386','x86_64'])
119    @skipIfOutOfTreeDebugserver
120    def test_watchos_armv7k(self):
121        """Test running a 32-bit watchOS simulator binary"""
122        self.run_with(arch='armv7k',
123                      os='watchos', vers='', env='simulator',
124                      expected_load_command='LC_BUILD_VERSION')
125
126
127    #
128    # Back-deployment tests.
129    #
130    # Older Mach-O versions used less expressive load commands, such
131    # as LC_VERSION_MIN_IPHONEOS that wouldn't distinguish between ios
132    # and ios-simulator.  When targeting a simulator on Apple Silicon
133    # macOS, however, these legacy load commands are never generated.
134    #
135
136    @skipUnlessDarwin
137    @skipIfDarwinEmbedded
138    @skipIfOutOfTreeDebugserver
139    def test_lc_version_min_macosx(self):
140        """Test running a back-deploying non-simulator MacOS X binary"""
141        self.run_with(arch=self.getArchitecture(),
142                      os='macosx', vers='10.9', env='',
143                      expected_load_command='LC_VERSION_MIN_MACOSX')
144    @skipIfAsan
145    @skipUnlessDarwin
146    @skipIfDarwinEmbedded
147    @apple_simulator_test('iphone')
148    @skipIf(archs=['arm64','arm64e'])
149    @skipIfOutOfTreeDebugserver
150    def test_lc_version_min_iphoneos(self):
151        """Test running a back-deploying iOS simulator binary
152           with a legacy iOS load command"""
153        self.run_with(arch=self.getArchitecture(),
154                      os='ios', vers='11.0', env='simulator',
155                      expected_load_command='LC_VERSION_MIN_IPHONEOS')
156
157    @skipIfAsan
158    @skipUnlessDarwin
159    @skipIfDarwinEmbedded
160    @apple_simulator_test('iphone')
161    @skipIf(archs=['arm64','arm64e'])
162    @skipIfOutOfTreeDebugserver
163    def test_ios_backdeploy_x86(self):
164        """Test running a back-deploying iOS simulator binary
165           with a legacy iOS load command"""
166        self.run_with(arch=self.getArchitecture(),
167                      os='ios', vers='13.0', env='simulator',
168                      expected_load_command='LC_BUILD_VERSION')
169
170    @skipIfAsan
171    @skipUnlessDarwin
172    @skipIfDarwinEmbedded
173    @apple_simulator_test('iphone')
174    @skipIf(archs=['i386','x86_64'])
175    @skipIfOutOfTreeDebugserver
176    def test_ios_backdeploy_apple_silicon(self):
177        """Test running a back-deploying iOS simulator binary"""
178        self.run_with(arch=self.getArchitecture(),
179                      os='ios', vers='11.0', env='simulator',
180                      expected_load_command='LC_BUILD_VERSION')
181
182    @skipIfAsan
183    @skipUnlessDarwin
184    @skipIfDarwinEmbedded
185    @apple_simulator_test('appletv')
186    @skipIf(archs=['arm64','arm64e'])
187    @skipIfOutOfTreeDebugserver
188    def test_lc_version_min_tvos(self):
189        """Test running a back-deploying tvOS simulator binary
190           with a legacy tvOS load command"""
191        self.run_with(arch=self.getArchitecture(),
192                      os='tvos', vers='11.0', env='simulator',
193                      expected_load_command='LC_VERSION_MIN_TVOS')
194
195    @skipIfAsan
196    @skipUnlessDarwin
197    @skipIfDarwinEmbedded
198    @apple_simulator_test('appletv')
199    @skipIf(archs=['i386','x86_64'])
200    @skipIfOutOfTreeDebugserver
201    def test_tvos_backdeploy_apple_silicon(self):
202        """Test running a back-deploying tvOS simulator binary"""
203        self.run_with(arch=self.getArchitecture(),
204                      os='tvos', vers='11.0', env='simulator',
205                      expected_load_command='LC_BUILD_VERSION')
206
207    @skipIfAsan
208    @skipUnlessDarwin
209    @skipIfDarwinEmbedded
210    @apple_simulator_test('watch')
211    @skipIf(archs=['arm64','arm64e'])
212    @skipIfDarwin # rdar://problem/64552748
213    @skipIfOutOfTreeDebugserver
214    def test_lc_version_min_watchos(self):
215        """Test running a back-deploying watchOS simulator binary
216           with a legacy watchOS load command"""
217        self.run_with(arch='i386',
218                      os='watchos', vers='4.0', env='simulator',
219                      expected_load_command='LC_VERSION_MIN_WATCHOS')
220
221    @skipIfAsan
222    @skipUnlessDarwin
223    @skipIfDarwinEmbedded
224    @apple_simulator_test('watch')
225    @skipIf(archs=['arm64','arm64e'])
226    @skipIfDarwin # rdar://problem/64552748
227    @skipIfOutOfTreeDebugserver
228    def test_watchos_backdeploy_apple_silicon(self):
229        """Test running a back-deploying watchOS simulator binary"""
230        self.run_with(arch='armv7k',
231                      os='watchos', vers='4.0', env='simulator',
232                      expected_load_command='LC_BUILD_VERSION')
233