1"""Test that types defined in shared libraries work correctly."""
2
3
4
5import unittest2
6import lldb
7from lldbsuite.test.decorators import *
8from lldbsuite.test.lldbtest import *
9import lldbsuite.test.lldbutil as lldbutil
10
11
12class SharedLibTestCase(TestBase):
13
14    def common_test_expr(self, preload_symbols):
15        if "clang" in self.getCompiler() and "3.4" in self.getCompilerVersion():
16            self.skipTest(
17                "llvm.org/pr16214 -- clang emits partial DWARF for structures referenced via typedef")
18
19        self.build()
20        self.common_setup(preload_symbols)
21
22        # This should display correctly.
23        self.expect(
24            "expression --show-types -- *my_foo_ptr",
25            VARIABLES_DISPLAYED_CORRECTLY,
26            substrs=[
27                "(foo)",
28                "(sub_foo)",
29                "other_element = 3"])
30
31        self.expect(
32            "expression GetMeASubFoo(my_foo_ptr)",
33            startstr="(sub_foo *) $")
34
35    def test_expr(self):
36        """Test that types work when defined in a shared library and forward-declared in the main executable"""
37        self.common_test_expr(True)
38
39    def test_expr_no_preload(self):
40        """Test that types work when defined in a shared library and forward-declared in the main executable, but with preloading disabled"""
41        self.common_test_expr(False)
42
43    @expectedFailure("llvm.org/PR36712")
44    def test_frame_variable(self):
45        """Test that types work when defined in a shared library and forward-declared in the main executable"""
46        self.build()
47        self.common_setup()
48
49        # This should display correctly.
50        self.expect(
51            "frame variable --show-types -- *my_foo_ptr",
52            VARIABLES_DISPLAYED_CORRECTLY,
53            substrs=[
54                "(foo)",
55                "(sub_foo)",
56                "other_element = 3"])
57
58    def setUp(self):
59        # Call super's setUp().
60        TestBase.setUp(self)
61        # Find the line number to break inside main().
62        self.source = 'main.c'
63        self.line = line_number(self.source, '// Set breakpoint 0 here.')
64        self.shlib_names = ["foo"]
65
66    def common_setup(self, preload_symbols = True):
67        # Run in synchronous mode
68        self.dbg.SetAsync(False)
69
70        # Create a target by the debugger.
71        target = self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
72        self.assertTrue(target, VALID_TARGET)
73
74        self.runCmd("settings set target.preload-symbols " + str(preload_symbols).lower())
75
76        # Break inside the foo function which takes a bar_ptr argument.
77        lldbutil.run_break_set_by_file_and_line(
78            self, self.source, self.line, num_expected_locations=1, loc_exact=True)
79
80        # Register our shared libraries for remote targets so they get
81        # automatically uploaded
82        environment = self.registerSharedLibrariesWithTarget(
83            target, self.shlib_names)
84
85        # Now launch the process, and do not stop at entry point.
86        process = target.LaunchSimple(
87            None, environment, self.get_process_working_directory())
88        self.assertTrue(process, PROCESS_IS_VALID)
89
90        # The stop reason of the thread should be breakpoint.
91        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
92                    substrs=['stopped',
93                             'stop reason = breakpoint'])
94
95        # The breakpoint should have a hit count of 1.
96        lldbutil.check_breakpoint(self, bpno = 1, expected_hit_count = 1)
97