TestUniversal.py revision b170aee2daacc83e3d71c3e3acc9d56c89893a7b
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    @python_api_test
19    @unittest2.skipUnless(sys.platform.startswith("darwin") and os.uname()[4] in ['i386', 'x86_64'],
20                          "requires Darwin & i386")
21    def test_sbdebugger_create_target_with_file_and_target_triple(self):
22        """Test the SBDebugger.CreateTargetWithFileAndTargetTriple() API."""
23        # Invoke the default build rule.
24        self.buildDefault()
25
26        # Note that "testit" is a universal binary.
27        exe = os.path.join(os.getcwd(), "testit")
28
29        # Create a target by the debugger.
30        target = self.dbg.CreateTargetWithFileAndTargetTriple(exe, "i386-apple-macosx")
31        self.assertTrue(target, VALID_TARGET)
32
33        # Now launch the process, and do not stop at entry point.
34        process = target.LaunchSimple(None, None, os.getcwd())
35        self.assertTrue(process, PROCESS_IS_VALID)
36
37    # rdar://problem/8972204 AddressByteSize of 32-bit process should be 4, got 8 instead.
38    @unittest2.skipUnless(sys.platform.startswith("darwin") and os.uname()[4] in ['i386', 'x86_64'],
39                          "requires Darwin & i386")
40    def test_process_launch_for_universal(self):
41        """Test process launch of a universal binary."""
42        from lldbutil import print_registers
43
44        # Invoke the default build rule.
45        self.buildDefault()
46
47        # Note that "testit" is a universal binary.
48        exe = os.path.join(os.getcwd(), "testit")
49
50        # By default, x86_64 is assumed if no architecture is specified.
51        self.expect("file " + exe, CURRENT_EXECUTABLE_SET,
52            startstr = "Current executable set to ",
53            substrs = ["testit' (x86_64)."])
54
55        # Break inside the main.
56        self.expect("breakpoint set -f main.c -l %d" % self.line,
57                    BREAKPOINT_CREATED,
58            startstr = "Breakpoint created: 1: file ='main.c', line = %d, locations = 1" %
59                        self.line)
60
61        # We should be able to launch the x86_64 executable.
62        self.runCmd("run", RUN_SUCCEEDED)
63
64        # Check whether we have a 64-bit process launched.
65        target = self.dbg.GetSelectedTarget()
66        process = target.GetProcess()
67        self.assertTrue(target and process and
68                        self.invoke(process, 'GetAddressByteSize') == 8,
69                        "64-bit process launched")
70
71        frame = process.GetThreadAtIndex(0).GetFrameAtIndex(0)
72        registers = print_registers(frame, string_buffer=True)
73        self.expect(registers, exe=False,
74            substrs = ['Name: rax'])
75
76        self.runCmd("continue")
77
78        # Now specify i386 as the architecture for "testit".
79        self.expect("file " + exe + " -a i386", CURRENT_EXECUTABLE_SET,
80            startstr = "Current executable set to ",
81            substrs = ["testit' (i386)."])
82
83        # Break inside the main.
84        self.expect("breakpoint set -f main.c -l %d" % self.line,
85                    BREAKPOINT_CREATED,
86            startstr = "Breakpoint created: 1: file ='main.c', line = %d, locations = 1" %
87                        self.line)
88
89        # We should be able to launch the i386 executable as well.
90        self.runCmd("run", RUN_SUCCEEDED)
91
92        # Check whether we have a 32-bit process launched.
93        target = self.dbg.GetSelectedTarget()
94        process = target.GetProcess()
95        self.assertTrue(target and process,
96                        "32-bit process launched")
97
98        pointerSize = self.invoke(process, 'GetAddressByteSize')
99        self.assertTrue(pointerSize == 4,
100                        "AddressByteSize of 32-bit process should be 4, got %d instead." % pointerSize)
101
102        frame = process.GetThreadAtIndex(0).GetFrameAtIndex(0)
103        registers = print_registers(frame, string_buffer=True)
104        self.expect(registers, exe=False,
105            substrs = ['Name: eax'])
106
107        self.runCmd("continue")
108
109
110if __name__ == '__main__':
111    import atexit
112    lldb.SBDebugger.Initialize()
113    atexit.register(lambda: lldb.SBDebugger.Terminate())
114    unittest2.main()
115