1"""
2Test mpx-table command.
3"""
4
5from __future__ import print_function
6
7
8import os
9import time
10import re
11import lldb
12from lldbsuite.test.decorators import *
13from lldbsuite.test.lldbtest import *
14from lldbsuite.test import lldbutil
15
16
17class TestMPXTable(TestBase):
18
19    mydir = TestBase.compute_mydir(__file__)
20
21    def setUp(self):
22        TestBase.setUp(self)
23
24    @skipIf(compiler="clang")
25    @skipIf(oslist=no_match(['linux']))
26    @skipIf(archs=no_match(['i386', 'x86_64']))
27    @skipIf(compiler="gcc", compiler_version=["<", "5"]) #GCC version >= 5 supports
28    #Intel(R) Memory Protection Extensions (Intel(R) MPX).
29    def test_show_command(self):
30        """Test 'mpx-table show' command"""
31        self.build()
32
33        plugin_file = os.path.join(configuration.lldb_libs_dir, "liblldbIntelFeatures.so")
34        if not os.path.isfile(plugin_file):
35            self.skipTest("features plugin missing.")
36        plugin_command = " "
37        seq = ("plugin", "load", plugin_file)
38        plugin_command = plugin_command.join(seq)
39        self.runCmd(plugin_command)
40
41        exe = os.path.join(os.getcwd(), "a.out")
42        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
43
44        self.b1 = line_number('main.cpp', '// Break 1.')
45        self.b2 = line_number('main.cpp', '// Break 2.')
46        self.b3 = line_number('main.cpp', '// Break 3.')
47        self.b4 = line_number('main.cpp', '// Break 4.')
48        lldbutil.run_break_set_by_file_and_line(self, "main.cpp", self.b1, num_expected_locations=1)
49        lldbutil.run_break_set_by_file_and_line(self, "main.cpp", self.b2, num_expected_locations=1)
50        lldbutil.run_break_set_by_file_and_line(self, "main.cpp", self.b3, num_expected_locations=1)
51        lldbutil.run_break_set_by_file_and_line(self, "main.cpp", self.b4, num_expected_locations=1)
52        self.runCmd("run", RUN_SUCCEEDED)
53
54        target = self.dbg.GetSelectedTarget()
55        process = target.GetProcess()
56
57        if (process.GetState() == lldb.eStateExited):
58            self.skipTest("Intel(R) MPX is not supported.")
59        else:
60            self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT,
61                        substrs = ["stop reason = breakpoint 1."])
62
63        self.expect("mpx-table show a",
64                     substrs = ['lbound = 0x',
65                                ', ubound = 0x',
66                                '(pointer value = 0x',
67                                ', metadata = 0x',
68                                ')'],
69                     error = False)
70
71        self.expect("continue", STOPPED_DUE_TO_BREAKPOINT,
72                     substrs = ["stop reason = breakpoint 2."])
73
74        # Check that out of scope pointer cannot be reached.
75        #
76        self.expect("mpx-table show a",
77                     substrs = ['Invalid pointer.'],
78                     error = True)
79
80        self.expect("mpx-table show tmp",
81                     substrs = ['lbound = 0x',
82                                ', ubound = 0x',
83                                '(pointer value = 0x',
84                                ', metadata = 0x',
85                                ')'],
86                     error = False)
87
88        self.expect("continue", STOPPED_DUE_TO_BREAKPOINT,
89                     substrs = ["stop reason = breakpoint 3."])
90
91        # Check that the pointer value is correctly updated.
92        #
93        self.expect("mpx-table show tmp",
94                     substrs = ['lbound = 0x',
95                                ', ubound = 0x',
96                                '(pointer value = 0x2',
97                                ', metadata = 0x',
98                                ')'],
99                     error = False)
100
101        self.expect("continue", STOPPED_DUE_TO_BREAKPOINT,
102                     substrs = ["stop reason = breakpoint 4."])
103
104        # After going back to main(), check that out of scope pointer cannot be
105        # reached.
106        #
107        self.expect("mpx-table show tmp",
108                     substrs = ['Invalid pointer.'],
109                     error = True)
110
111        self.expect("mpx-table show a",
112                     substrs = ['lbound = 0x',
113                                ', ubound = 0x',
114                                '(pointer value = 0x',
115                                ', metadata = 0x',
116                                ')'],
117                     error = False)
118
119    def test_set_command(self):
120        """Test 'mpx-table set' command"""
121        self.build()
122
123        plugin_file = os.path.join(configuration.lldb_libs_dir, "liblldbIntelFeatures.so")
124        if not os.path.isfile(plugin_file):
125            self.skipTest("features plugin missing.")
126        plugin_command = " "
127        seq = ("plugin", "load", plugin_file)
128        plugin_command = plugin_command.join(seq)
129        self.runCmd(plugin_command)
130
131        exe = os.path.join(os.getcwd(), "a.out")
132        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
133
134        self.b1 = line_number('main.cpp', '// Break 1.')
135        lldbutil.run_break_set_by_file_and_line(self, "main.cpp", self.b1, num_expected_locations=1)
136        self.runCmd("run", RUN_SUCCEEDED)
137
138        target = self.dbg.GetSelectedTarget()
139        process = target.GetProcess()
140
141        if (process.GetState() == lldb.eStateExited):
142            self.skipTest("Intel(R) MPX is not supported.")
143        else:
144            self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT,
145                        substrs = ["stop reason = breakpoint 1."])
146
147        # Check that the BT Entry doesn't already contain the test values.
148        #
149        self.expect("mpx-table show a", matching=False,
150                     substrs = ['lbound = 0xcafecafe',
151                                ', ubound = 0xbeefbeef'])
152
153        # Set the test values.
154        #
155        self.expect("mpx-table set a 0xcafecafe 0xbeefbeef", error = False)
156
157        # Verify that the test values have been correctly written in the BT
158        # entry.
159        #
160        self.expect("mpx-table show a",
161                     substrs = ['lbound = 0xcafecafe',
162                                ', ubound = 0xbeefbeef'],
163                     error = False)
164
165
166