1"""Test that types defined in shared libraries work correctly."""
2
3import os, time
4import unittest2
5import lldb
6from lldbtest import *
7import lldbutil
8
9class SharedLibTestCase(TestBase):
10
11    mydir = os.path.join("lang", "c", "shared_lib")
12
13    @dsym_test
14    def test_expr_with_dsym(self):
15        """Test that types work when defined in a shared library and forward-declared in the main executable"""
16        self.buildDsym()
17        self.expr()
18
19    @dwarf_test
20    def test_expr_with_dwarf(self):
21        """Test that types work when defined in a shared library and forward-declared in the main executable"""
22        self.buildDwarf()
23        self.expr()
24
25    @dsym_test
26    def test_frame_variable_with_dsym(self):
27        """Test that types work when defined in a shared library and forward-declared in the main executable"""
28        self.buildDsym()
29        self.frame_var()
30
31    @dwarf_test
32    def test_frame_variable_with_dwarf(self):
33        """Test that types work when defined in a shared library and forward-declared in the main executable"""
34        self.buildDwarf()
35        self.frame_var()
36
37    def setUp(self):
38        # Call super's setUp().
39        TestBase.setUp(self)
40        # Find the line number to break inside main().
41        self.line = line_number('main.c', '// Set breakpoint 0 here.')
42        if sys.platform.startswith("linux"):
43            if "LD_LIBRARY_PATH" in os.environ:
44                self.runCmd("settings set target.env-vars " + self.dylibPath + "=" + os.environ["LD_LIBRARY_PATH"] + ":" + os.getcwd())
45            else:
46                self.runCmd("settings set target.env-vars " + self.dylibPath + "=" + os.getcwd())
47            self.addTearDownHook(lambda: self.runCmd("settings remove target.env-vars " + self.dylibPath))
48
49    def common_setup(self):
50        exe = os.path.join(os.getcwd(), "a.out")
51        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
52
53        # Break inside the foo function which takes a bar_ptr argument.
54        lldbutil.run_break_set_by_file_and_line (self, "main.c", self.line, num_expected_locations=1, loc_exact=True)
55
56        self.runCmd("run", RUN_SUCCEEDED)
57
58        # The stop reason of the thread should be breakpoint.
59        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
60            substrs = ['stopped',
61                       'stop reason = breakpoint'])
62
63        # The breakpoint should have a hit count of 1.
64        self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
65            substrs = [' resolved, hit count = 1'])
66
67    def expr(self):
68        """Test that types work when defined in a shared library and forward-declared in the main executable"""
69
70        if "clang" in self.getCompiler() and "3.4" in self.getCompilerVersion():
71            self.skipTest("llvm.org/pr16214 -- clang emits partial DWARF for structures referenced via typedef")
72
73	self.common_setup()
74
75        # This should display correctly.
76        self.expect("expression --show-types -- *my_foo_ptr", VARIABLES_DISPLAYED_CORRECTLY,
77            substrs = ["(foo)", "(sub_foo)", "other_element = 3"])
78
79    @unittest2.expectedFailure
80    # rdar://problem/10381325
81    def frame_var(self):
82        """Test that types work when defined in a shared library and forward-declared in the main executable"""
83	self.common_setup()
84
85        # This should display correctly.
86        self.expect("frame variable --show-types -- *my_foo_ptr", VARIABLES_DISPLAYED_CORRECTLY,
87            substrs = ["(foo)", "(sub_foo)", "other_element = 3"])
88
89if __name__ == '__main__':
90    import atexit
91    lldb.SBDebugger.Initialize()
92    atexit.register(lambda: lldb.SBDebugger.Terminate())
93    unittest2.main()
94