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