1"""Show global variables and check that they do indeed have global scopes."""
2
3import os, time
4import unittest2
5import lldb
6from lldbtest import *
7import lldbutil
8
9class GlobalVariablesTestCase(TestBase):
10
11    mydir = os.path.join("lang", "c", "global_variables")
12
13    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
14    @dsym_test
15    def test_with_dsym(self):
16        """Test 'frame variable --scope --no-args' which omits args and shows scopes."""
17        self.buildDsym()
18        self.global_variables()
19
20    @dwarf_test
21    def test_with_dwarf(self):
22        """Test 'frame variable --scope --no-args' which omits args and shows scopes."""
23        self.buildDwarf()
24        self.global_variables()
25
26    def setUp(self):
27        # Call super's setUp().
28        TestBase.setUp(self)
29        # Find the line number to break inside main().
30        self.line = line_number('main.c', '// Set break point at this line.')
31        if sys.platform.startswith("linux"):
32            # On Linux, LD_LIBRARY_PATH must be set so the shared libraries are found on startup
33            if "LD_LIBRARY_PATH" in os.environ:
34                self.runCmd("settings set target.env-vars " + self.dylibPath + "=" + os.environ["LD_LIBRARY_PATH"] + ":" + os.getcwd())
35            else:
36                self.runCmd("settings set target.env-vars " + self.dylibPath + "=" + os.getcwd())
37            self.addTearDownHook(lambda: self.runCmd("settings remove target.env-vars " + self.dylibPath))
38
39    def global_variables(self):
40        """Test 'frame variable --scope --no-args' which omits args and shows scopes."""
41        exe = os.path.join(os.getcwd(), "a.out")
42        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
43
44        # Break inside the main.
45        lldbutil.run_break_set_by_file_and_line (self, "main.c", self.line, num_expected_locations=1, loc_exact=True)
46
47        self.runCmd("run", RUN_SUCCEEDED)
48
49        self.runCmd("process status", "Get process status")
50
51        # The stop reason of the thread should be breakpoint.
52        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
53            substrs = ['stopped',
54                       'stop reason = breakpoint'])
55
56        # The breakpoint should have a hit count of 1.
57        self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
58            substrs = [' resolved, hit count = 1'])
59
60        # Check that GLOBAL scopes are indicated for the variables.
61        self.expect("frame variable --show-types --scope --show-globals --no-args", VARIABLES_DISPLAYED_CORRECTLY,
62            substrs = ['GLOBAL: (int) g_file_global_int = 42',
63                       'GLOBAL: (const char *) g_file_global_cstr',
64                       '"g_file_global_cstr"',
65                       'STATIC: (const char *) g_file_static_cstr',
66                       '"g_file_static_cstr"',
67                       'GLOBAL: (int) g_common_1 = 21'])
68
69        # 'frame variable' should support address-of operator.
70        self.runCmd("frame variable &g_file_global_int")
71
72        # Exercise the 'target variable' command to display globals in a.c file.
73        self.expect("target variable g_a", VARIABLES_DISPLAYED_CORRECTLY,
74                    substrs = ['g_a', '123'])
75        self.expect("target variable g_marked_spot.x", VARIABLES_DISPLAYED_CORRECTLY,
76                    substrs = ['g_marked_spot.x', '20'])
77
78        # rdar://problem/9747668
79        # runCmd: target variable g_marked_spot.y
80        # output: (int) g_marked_spot.y = <a.o[0x214] can't be resolved,  in not currently loaded.
81        #         >
82        self.expect("target variable g_marked_spot.y", VARIABLES_DISPLAYED_CORRECTLY,
83                    substrs = ['g_marked_spot.y', '21'])
84        self.expect("target variable g_marked_spot.y", VARIABLES_DISPLAYED_CORRECTLY, matching=False,
85                    substrs = ["can't be resolved"])
86
87
88if __name__ == '__main__':
89    import atexit
90    lldb.SBDebugger.Initialize()
91    atexit.register(lambda: lldb.SBDebugger.Terminate())
92    unittest2.main()
93