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