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        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("target variable g_ptr", VARIABLES_DISPLAYED_CORRECTLY,
73                    substrs=['(int *)'])
74        self.expect("target variable *g_ptr", VARIABLES_DISPLAYED_CORRECTLY,
75                    substrs=['42'])
76
77        # Check that GLOBAL scopes are indicated for the variables.
78        self.expect(
79            "frame variable --show-types --scope --show-globals --no-args",
80            VARIABLES_DISPLAYED_CORRECTLY,
81            ordered=False,
82            substrs=[
83                'STATIC: (const char *) g_func_static_cstr',
84                '"g_func_static_cstr"',
85                'GLOBAL: (int *) g_ptr',
86                'STATIC: (const int) g_file_static_int = 2',
87                'GLOBAL: (int) g_common_1 = 21',
88                'GLOBAL: (int) g_file_global_int = 42',
89                'STATIC: (const char *) g_file_static_cstr',
90                '"g_file_static_cstr"',
91                'GLOBAL: (const char *) g_file_global_cstr',
92                '"g_file_global_cstr"',
93            ])
94
95        # 'frame variable' should support address-of operator.
96        self.runCmd("frame variable &g_file_global_int")
97
98        # Exercise the 'target variable' command to display globals in a.c
99        # file.
100        self.expect("target variable g_a", VARIABLES_DISPLAYED_CORRECTLY,
101                    substrs=['g_a', '123'])
102        self.expect(
103            "target variable g_marked_spot.x",
104            VARIABLES_DISPLAYED_CORRECTLY,
105            substrs=[
106                'g_marked_spot.x',
107                '20'])
108
109        self.expect(
110            "target variable g_marked_spot.y",
111            VARIABLES_DISPLAYED_CORRECTLY,
112            substrs=[
113                'g_marked_spot.y',
114                '21'])
115        self.expect(
116            "target variable g_marked_spot.y",
117            VARIABLES_DISPLAYED_CORRECTLY,
118            matching=False,
119            substrs=["can't be resolved"])
120
121