xref: /llvm-project/lldb/test/API/lang/c/global_variables/TestGlobalVariables.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1"""Show global variables and check that they do indeed have global scopes."""
2
3
4from lldbsuite.test.decorators import *
5from lldbsuite.test.lldbtest import *
6from lldbsuite.test import lldbutil
7
8
9class GlobalVariablesTestCase(TestBase):
10    def setUp(self):
11        # Call super's setUp().
12        TestBase.setUp(self)
13        # Find the line number to break inside main().
14        self.source = "main.c"
15        self.line = line_number(self.source, "// Set break point at this line.")
16        self.shlib_names = ["a"]
17
18    @skipIfDarwin  # Chained Fixups
19    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24764")
20    def test_without_process(self):
21        """Test that static initialized variables can be inspected without
22        process."""
23        self.build()
24
25        # Create a target by the debugger.
26        target = self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
27
28        self.assertTrue(target, VALID_TARGET)
29        self.expect(
30            "target variable g_ptr", VARIABLES_DISPLAYED_CORRECTLY, substrs=["(int *)"]
31        )
32        self.expect(
33            "target variable *g_ptr", VARIABLES_DISPLAYED_CORRECTLY, substrs=["42"]
34        )
35
36    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24764")
37    def test_c_global_variables(self):
38        """Test 'frame variable --scope --no-args' which omits args and shows scopes."""
39        self.build()
40
41        # Create a target by the debugger.
42        target = self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
43        self.assertTrue(target, VALID_TARGET)
44
45        # Break inside the main.
46        lldbutil.run_break_set_by_file_and_line(
47            self, self.source, self.line, num_expected_locations=1, loc_exact=True
48        )
49
50        # Register our shared libraries for remote targets so they get
51        # automatically uploaded
52        environment = self.registerSharedLibrariesWithTarget(target, self.shlib_names)
53
54        # Now launch the process, and do not stop at entry point.
55        process = target.LaunchSimple(
56            None, environment, self.get_process_working_directory()
57        )
58        self.assertTrue(process, PROCESS_IS_VALID)
59
60        # The stop reason of the thread should be breakpoint.
61        self.expect(
62            "thread list",
63            STOPPED_DUE_TO_BREAKPOINT,
64            substrs=["stopped", "stop reason = breakpoint"],
65        )
66
67        # The breakpoint should have a hit count of 1.
68        lldbutil.check_breakpoint(self, bpno=1, expected_hit_count=1)
69
70        # Test that the statically initialized variable can also be
71        # inspected *with* a process.
72        self.expect(
73            "target variable g_ptr", VARIABLES_DISPLAYED_CORRECTLY, substrs=["(int *)"]
74        )
75        self.expect(
76            "target variable *g_ptr", VARIABLES_DISPLAYED_CORRECTLY, substrs=["42"]
77        )
78
79        # Check that GLOBAL scopes are indicated for the variables.
80        self.expect(
81            "frame variable --show-types --scope --show-globals --no-args",
82            VARIABLES_DISPLAYED_CORRECTLY,
83            ordered=False,
84            substrs=[
85                "STATIC: (const char *) g_func_static_cstr",
86                '"g_func_static_cstr"',
87                "GLOBAL: (int *) g_ptr",
88                "STATIC: (const int) g_file_static_int = 2",
89                "GLOBAL: (int) g_common_1 = 21",
90                "GLOBAL: (int) g_file_global_int = 42",
91                "STATIC: (const char *) g_file_static_cstr",
92                '"g_file_static_cstr"',
93                "GLOBAL: (const char *) g_file_global_cstr",
94                '"g_file_global_cstr"',
95            ],
96        )
97
98        # 'frame variable' should support address-of operator.
99        self.runCmd("frame variable &g_file_global_int")
100
101        # Exercise the 'target variable' command to display globals in a.c
102        # file.
103        self.expect(
104            "target variable g_a", VARIABLES_DISPLAYED_CORRECTLY, substrs=["g_a", "123"]
105        )
106        self.expect(
107            "target variable g_marked_spot.x",
108            VARIABLES_DISPLAYED_CORRECTLY,
109            substrs=["g_marked_spot.x", "20"],
110        )
111
112        self.expect(
113            "target variable g_marked_spot.y",
114            VARIABLES_DISPLAYED_CORRECTLY,
115            substrs=["g_marked_spot.y", "21"],
116        )
117        self.expect(
118            "target variable g_marked_spot.y",
119            VARIABLES_DISPLAYED_CORRECTLY,
120            matching=False,
121            substrs=["can't be resolved"],
122        )
123