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