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