import unittest
import os
import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil
def haswellOrLater():
features = subprocess.check_output(["sysctl", "machdep.cpu"])
return "AVX2" in features.split()
class UniversalTestCase(TestBase):
"""Test aspects of lldb commands on universal binaries."""
NO_DEBUG_INFO_TESTCASE = True
def setUp(self):
TestBase.setUp(self)
self.line = line_number("main.c", "// Set break point at this line.")
@add_test_categories(["pyapi"])
@skipUnlessDarwin
@unittest.skipUnless(
hasattr(os, "uname") and os.uname()[4] in ["x86_64"], "requires x86_64"
)
@skipIfDarwinEmbedded
@skipIf(compiler="clang", compiler_version=["<", "7.0"])
def test_sbdebugger_create_target_with_file_and_target_triple(self):
"""Test the SBDebugger.CreateTargetWithFileAndTargetTriple() API."""
self.build()
exe = self.getBuildArtifact("testit")
target = self.dbg.CreateTargetWithFileAndTargetTriple(
exe, "x86_64-apple-macosx10.10"
)
self.assertTrue(target, VALID_TARGET)
self.expect("image list -t -b", substrs=["x86_64-apple-macosx10.9.0 testit"])
self.expect("target list", substrs=["testit", "arch=x86_64-apple-macosx10.10"])
process = target.LaunchSimple(None, None, self.get_process_working_directory())
self.assertTrue(process, PROCESS_IS_VALID)
@skipUnlessDarwin
@unittest.skipUnless(
hasattr(os, "uname") and os.uname()[4] in ["x86_64"], "requires x86_64"
)
@skipIfDarwinEmbedded
@skipIf(compiler="clang", compiler_version=["<", "7.0"])
def test_process_launch_for_universal(self):
"""Test process launch of a universal binary."""
from lldbsuite.test.lldbutil import print_registers
if not haswellOrLater():
return
self.build()
exe = self.getBuildArtifact("testit")
self.expect(
"file " + exe,
CURRENT_EXECUTABLE_SET,
startstr="Current executable set to ",
substrs=["testit' (x86_64h)."],
)
lldbutil.run_break_set_by_file_and_line(
self, "main.c", self.line, num_expected_locations=1, loc_exact=True
)
self.runCmd("run", RUN_SUCCEEDED)
target = self.dbg.GetSelectedTarget()
process = target.GetProcess()
self.expect("image list -A -b", substrs=["x86_64h testit"])
self.runCmd("continue")
self.expect(
"file -a x86_64 " + exe,
CURRENT_EXECUTABLE_SET,
startstr="Current executable set to ",
substrs=["testit' (x86_64)."],
)
lldbutil.run_break_set_by_file_and_line(
self, "main.c", self.line, num_expected_locations=1, loc_exact=True
)
self.runCmd("run", RUN_SUCCEEDED)
self.expect("image list -A -b", substrs=["x86_64h testit"])
self.runCmd("continue")
@skipUnlessDarwin
@unittest.skipUnless(
hasattr(os, "uname") and os.uname()[4] in ["x86_64"], "requires x86_64"
)
@skipIfDarwinEmbedded
def test_process_attach_with_wrong_arch(self):
"""Test that when we attach to a binary from the wrong fork of
a universal binary, we fix up the ABI correctly."""
if not haswellOrLater():
return
self.build()
exe = self.getBuildArtifact("testit")
target = self.dbg.CreateTargetWithFileAndTargetTriple(
exe, "x86_64-apple-macosx"
)
self.assertTrue(target, VALID_TARGET)
self.expect("image list -A -b", substrs=["x86_64 testit"])
bkpt = target.BreakpointCreateBySourceRegex("sleep", lldb.SBFileSpec("main.c"))
self.assertTrue(bkpt.IsValid(), "Valid breakpoint")
self.assertGreaterEqual(
bkpt.GetNumLocations(), 1, "Our main breakpoint has locations."
)
popen = self.spawnSubprocess(exe, ["keep_waiting"])
error = lldb.SBError()
empty_listener = lldb.SBListener()
process = target.AttachToProcessWithID(empty_listener, popen.pid, error)
self.assertSuccess(error, "Attached to process.")
self.expect("image list -A -b", substrs=["x86_64h testit"])
threads = lldbutil.continue_to_breakpoint(process, bkpt)
self.assertEqual(len(threads), 1)
thread = threads[0]
self.assertGreater(thread.GetNumFrames(), 1, "We were able to backtrace.")