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