TestUniversal.py revision 185e2c103c2af449262046495b3f8d1640794543
1"""Test aspects of lldb commands on universal binaries."""
2
3import os, time
4import unittest2
5import lldb
6from lldbtest import *
7
8class UniversalTestCase(TestBase):
9
10    mydir = os.path.join("macosx", "universal")
11
12    def setUp(self):
13        # Call super's setUp().
14        TestBase.setUp(self)
15        # Find the line number to break inside main().
16        self.line = line_number('main.c', '// Set break point at this line.')
17
18    # rdar://problem/8972204 AddressByteSize of 32-bit process should be 4, got 8 instead.
19    @unittest2.skipUnless(sys.platform.startswith("darwin") and os.uname()[4]=='i386',
20                          "requires Darwin & i386")
21    def test_process_launch_for_universal(self):
22        """Test process launch of a universal binary."""
23        from lldbutil import print_registers
24
25        # Invoke the default build rule.
26        self.buildDefault()
27
28        # Note that "testit" is a universal binary.
29        exe = os.path.join(os.getcwd(), "testit")
30
31        # By default, x86_64 is assumed if no architecture is specified.
32        self.expect("file " + exe, CURRENT_EXECUTABLE_SET,
33            startstr = "Current executable set to ",
34            substrs = ["testit' (x86_64)."])
35
36        # Break inside the main.
37        self.expect("breakpoint set -f main.c -l %d" % self.line,
38                    BREAKPOINT_CREATED,
39            startstr = "Breakpoint created: 1: file ='main.c', line = %d, locations = 1" %
40                        self.line)
41
42        # We should be able to launch the x86_64 executable.
43        self.runCmd("run", RUN_SUCCEEDED)
44
45        # Check whether we have a 64-bit process launched.
46        target = self.dbg.GetSelectedTarget()
47        process = target.GetProcess()
48        self.assertTrue(target.IsValid() and process.IsValid() and
49                        self.invoke(process, 'GetAddressByteSize') == 8,
50                        "64-bit process launched")
51
52        frame = process.GetThreadAtIndex(0).GetFrameAtIndex(0)
53        registers = print_registers(frame, string_buffer=True)
54        self.expect(registers, exe=False,
55            substrs = ['Name: rax'])
56
57        self.runCmd("continue")
58
59        # Now specify i386 as the architecture for "testit".
60        self.expect("file " + exe + " -a i386", CURRENT_EXECUTABLE_SET,
61            startstr = "Current executable set to ",
62            substrs = ["testit' (i386)."])
63
64        # Break inside the main.
65        self.expect("breakpoint set -f main.c -l %d" % self.line,
66                    BREAKPOINT_CREATED,
67            startstr = "Breakpoint created: 1: file ='main.c', line = %d, locations = 1" %
68                        self.line)
69
70        # We should be able to launch the i386 executable as well.
71        self.runCmd("run", RUN_SUCCEEDED)
72
73        # Check whether we have a 32-bit process launched.
74        target = self.dbg.GetSelectedTarget()
75        process = target.GetProcess()
76        self.assertTrue(target.IsValid() and process.IsValid(),
77                        "32-bit process launched")
78
79        pointerSize = self.invoke(process, 'GetAddressByteSize')
80        self.assertTrue(pointerSize == 4,
81                        "AddressByteSize of 32-bit process should be 4, got %d instead." % pointerSize)
82
83        frame = process.GetThreadAtIndex(0).GetFrameAtIndex(0)
84        registers = print_registers(frame, string_buffer=True)
85        self.expect(registers, exe=False,
86            substrs = ['Name: eax'])
87
88        self.runCmd("continue")
89
90
91if __name__ == '__main__':
92    import atexit
93    lldb.SBDebugger.Initialize()
94    atexit.register(lambda: lldb.SBDebugger.Terminate())
95    unittest2.main()
96