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