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