1import unittest2
2import os
3import lldb
4from lldbsuite.test.decorators import *
5from lldbsuite.test.lldbtest import *
6from lldbsuite.test import lldbutil
7
8def haswellOrLater():
9    features = subprocess.check_output(["sysctl", "machdep.cpu"])
10    return "AVX2" in features.split()
11
12class UniversalTestCase(TestBase):
13    """Test aspects of lldb commands on universal binaries."""
14
15    NO_DEBUG_INFO_TESTCASE = True
16
17    def setUp(self):
18        # Call super's setUp().
19        TestBase.setUp(self)
20        # Find the line number to break inside main().
21        self.line = line_number('main.c', '// Set break point at this line.')
22
23    @add_test_categories(['pyapi'])
24    @skipUnlessDarwin
25    @unittest2.skipUnless(hasattr(os, "uname") and os.uname()[4] in
26                          ['x86_64'], "requires x86_64")
27    @skipIfDarwinEmbedded # this test file assumes we're targetting an x86 system
28    @skipIf(compiler="clang", compiler_version=['<', '7.0'])
29    def test_sbdebugger_create_target_with_file_and_target_triple(self):
30        """Test the SBDebugger.CreateTargetWithFileAndTargetTriple() API."""
31        # Invoke the default build rule.
32        self.build()
33
34        # Note that "testit" is a universal binary.
35        exe = self.getBuildArtifact("testit")
36
37        # Create a target by the debugger.
38        target = self.dbg.CreateTargetWithFileAndTargetTriple(
39            exe, "x86_64-apple-macosx10.10")
40        self.assertTrue(target, VALID_TARGET)
41        self.expect("image list -t -b", substrs=["x86_64-apple-macosx10.9.0 testit"])
42        self.expect("target list", substrs=["testit", "arch=x86_64-apple-macosx10.10"])
43
44        # Now launch the process, and do not stop at entry point.
45        process = target.LaunchSimple(
46            None, None, self.get_process_working_directory())
47        self.assertTrue(process, PROCESS_IS_VALID)
48
49    @skipUnlessDarwin
50    @unittest2.skipUnless(hasattr(os, "uname") and os.uname()[4] in
51                          ['x86_64'], "requires x86_64")
52    @skipIfDarwinEmbedded # this test file assumes we're targetting an x86 system
53    @skipIf(compiler="clang", compiler_version=['<', '7.0'])
54    def test_process_launch_for_universal(self):
55        """Test process launch of a universal binary."""
56        from lldbsuite.test.lldbutil import print_registers
57
58        if not haswellOrLater():
59            return
60
61        # Invoke the default build rule.
62        self.build()
63
64        # Note that "testit" is a universal binary.
65        exe = self.getBuildArtifact("testit")
66
67        # By default, x86_64 is assumed if no architecture is specified.
68        self.expect("file " + exe, CURRENT_EXECUTABLE_SET,
69                    startstr="Current executable set to ",
70                    substrs=["testit' (x86_64h)."])
71
72        # Break inside the main.
73        lldbutil.run_break_set_by_file_and_line(
74            self, "main.c", self.line, num_expected_locations=1, loc_exact=True)
75
76        # We should be able to launch the x86_64h executable.
77        self.runCmd("run", RUN_SUCCEEDED)
78
79        # Check whether we have a x86_64h process launched.
80        target = self.dbg.GetSelectedTarget()
81        process = target.GetProcess()
82        self.expect("image list -A -b", substrs=["x86_64h testit"])
83        self.runCmd("continue")
84
85        # Now specify x86_64 as the architecture for "testit".
86        self.expect("file -a x86_64 " + exe, CURRENT_EXECUTABLE_SET,
87                    startstr="Current executable set to ",
88                    substrs=["testit' (x86_64)."])
89
90        # Break inside the main.
91        lldbutil.run_break_set_by_file_and_line(
92            self, "main.c", self.line, num_expected_locations=1, loc_exact=True)
93
94        # We should be able to launch the x86_64 executable as well.
95        self.runCmd("run", RUN_SUCCEEDED)
96
97        # Check whether we have a x86_64 process launched.
98
99        # FIXME: This wrong. We are expecting x86_64, but spawning a
100        # new process currently doesn't allow specifying a *sub*-architecture.
101        # <rdar://problem/46101466>
102        self.expect("image list -A -b", substrs=["x86_64h testit"])
103        self.runCmd("continue")
104
105    @skipUnlessDarwin
106    @unittest2.skipUnless(hasattr(os, "uname") and os.uname()[4] in
107                          ['x86_64'], "requires x86_64")
108    @skipIfDarwinEmbedded # this test file assumes we're targetting an x86 system
109    def test_process_attach_with_wrong_arch(self):
110        """Test that when we attach to a binary from the wrong fork of
111            a universal binary, we fix up the ABI correctly."""
112        if not haswellOrLater():
113            return
114
115        # Now keep the architecture at x86_64, but switch the binary
116        # we launch to x86_64h, and make sure on attach we switch to
117        # the correct architecture.
118
119        # Invoke the default build rule.
120        self.build()
121
122        # Note that "testit" is a universal binary.
123        exe = self.getBuildArtifact("testit")
124
125        # Create a target by the debugger.
126        target = self.dbg.CreateTargetWithFileAndTargetTriple(
127            exe, "x86_64-apple-macosx")
128        self.assertTrue(target, VALID_TARGET)
129        self.expect("image list -A -b", substrs=["x86_64 testit"])
130
131        bkpt = target.BreakpointCreateBySourceRegex(
132            "sleep", lldb.SBFileSpec("main.c"))
133        self.assertTrue(bkpt.IsValid(), "Valid breakpoint")
134        self.assertTrue(
135            bkpt.GetNumLocations() >= 1,
136            "Our main breakpoint has locations.")
137
138        popen = self.spawnSubprocess(exe, ["keep_waiting"])
139
140        error = lldb.SBError()
141        empty_listener = lldb.SBListener()
142        process = target.AttachToProcessWithID(
143            empty_listener, popen.pid, error)
144        self.assertSuccess(error, "Attached to process.")
145
146        self.expect("image list -A -b", substrs=["x86_64h testit"])
147
148        # It may seem odd to check the number of frames, but the bug
149        # that motivated this test was that we eventually fixed the
150        # architecture, but we left the ABI set to the original value.
151        # In that case, if you asked the process for its architecture,
152        # it would look right, but since the ABI was wrong,
153        # backtracing failed.
154
155        threads = lldbutil.continue_to_breakpoint(process, bkpt)
156        self.assertEquals(len(threads), 1)
157        thread = threads[0]
158        self.assertTrue(
159            thread.GetNumFrames() > 1,
160            "We were able to backtrace.")
161