1"""
2Test lldb data formatter subsystem.
3"""
4
5import os
6import lldb
7from lldbsuite.test.decorators import *
8from lldbsuite.test.lldbtest import *
9from lldbsuite.test import lldbutil
10
11
12class SkipSummaryDataFormatterTestCase(TestBase):
13
14    mydir = TestBase.compute_mydir(__file__)
15
16    @expectedFailureAll(
17        oslist=["windows"],
18        bugnumber="llvm.org/pr24462, Data formatters have problems on Windows")
19    def test_with_run_command(self):
20        """Test data formatter commands."""
21        self.build()
22        self.data_formatter_commands()
23
24    def setUp(self):
25        # Call super's setUp().
26        TestBase.setUp(self)
27        # Find the line number to break at.
28        self.line = line_number('main.cpp', '// Set break point at this line.')
29
30    def data_formatter_commands(self):
31        """Test that that file and class static variables display correctly."""
32        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
33
34        #import lldbsuite.test.lldbutil as lldbutil
35        lldbutil.run_break_set_by_file_and_line(
36            self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True)
37
38        self.runCmd("run", RUN_SUCCEEDED)
39
40        # The stop reason of the thread should be breakpoint.
41        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
42                    substrs=['stopped',
43                             'stop reason = breakpoint'])
44
45        # This is the function to remove the custom formats in order to have a
46        # clean slate for the next test case.
47        def cleanup():
48            self.runCmd('type format clear', check=False)
49            self.runCmd('type summary clear', check=False)
50
51        # Execute the cleanup function during test case tear down.
52        self.addTearDownHook(cleanup)
53
54        # Setup the summaries for this scenario
55        #self.runCmd("type summary add --summary-string \"${var._M_dataplus._M_p}\" std::string")
56        self.runCmd(
57            "type summary add --summary-string \"Level 1\" \"DeepData_1\"")
58        self.runCmd(
59            "type summary add --summary-string \"Level 2\" \"DeepData_2\" -e")
60        self.runCmd(
61            "type summary add --summary-string \"Level 3\" \"DeepData_3\"")
62        self.runCmd(
63            "type summary add --summary-string \"Level 4\" \"DeepData_4\"")
64        self.runCmd(
65            "type summary add --summary-string \"Level 5\" \"DeepData_5\"")
66
67        # Default case, just print out summaries
68        self.expect('frame variable',
69                    substrs=['(DeepData_1) data1 = Level 1',
70                             '(DeepData_2) data2 = Level 2 {',
71                             'm_child1 = Level 3',
72                             'm_child2 = Level 3',
73                             'm_child3 = Level 3',
74                             'm_child4 = Level 3',
75                             '}'])
76
77        # Skip the default (should be 1) levels of summaries
78        self.expect('frame variable --no-summary-depth',
79                    substrs=['(DeepData_1) data1 = {',
80                             'm_child1 = 0x',
81                             '}',
82                             '(DeepData_2) data2 = {',
83                             'm_child1 = Level 3',
84                             'm_child2 = Level 3',
85                             'm_child3 = Level 3',
86                             'm_child4 = Level 3',
87                             '}'])
88
89        # Now skip 2 levels of summaries
90        self.expect('frame variable --no-summary-depth=2',
91                    substrs=['(DeepData_1) data1 = {',
92                             'm_child1 = 0x',
93                             '}',
94                             '(DeepData_2) data2 = {',
95                             'm_child1 = {',
96                             'm_child1 = 0x',
97                             'Level 4',
98                             'm_child2 = {',
99                             'm_child3 = {',
100                             '}'])
101
102        # Check that no "Level 3" comes out
103        self.expect(
104            'frame variable data1.m_child1 --no-summary-depth=2',
105            matching=False,
106            substrs=['Level 3'])
107
108        # Now expand a pointer with 2 level of skipped summaries
109        self.expect('frame variable data1.m_child1 --no-summary-depth=2',
110                    substrs=['(DeepData_2 *) data1.m_child1 = 0x'])
111
112        # Deref and expand said pointer
113        self.expect('frame variable *data1.m_child1 --no-summary-depth=2',
114                    substrs=['(DeepData_2) *data1.m_child1 = {',
115                             'm_child2 = {',
116                             'm_child1 = 0x',
117                             'Level 4',
118                             '}'])
119
120        # Expand an expression, skipping 2 layers of summaries
121        self.expect(
122            'frame variable data1.m_child1->m_child2 --no-summary-depth=2',
123            substrs=[
124                '(DeepData_3) data1.m_child1->m_child2 = {',
125                'm_child2 = {',
126                'm_child1 = Level 5',
127                'm_child2 = Level 5',
128                'm_child3 = Level 5',
129                '}'])
130
131        # Expand same expression, skipping only 1 layer of summaries
132        self.expect(
133            'frame variable data1.m_child1->m_child2 --no-summary-depth=1',
134            substrs=[
135                '(DeepData_3) data1.m_child1->m_child2 = {',
136                'm_child1 = 0x',
137                'Level 4',
138                'm_child2 = Level 4',
139                '}'])
140
141        # Bad debugging info on SnowLeopard gcc (Apple Inc. build 5666).
142        # Skip the following tests if the condition is met.
143        if self.getCompiler().endswith('gcc') and not self.getCompiler().endswith('llvm-gcc'):
144            import re
145            gcc_version_output = system(
146                [[lldbutil.which(self.getCompiler()), "-v"]])
147            self.trace("my output:", gcc_version_output)
148            for line in gcc_version_output.split(os.linesep):
149                m = re.search('\(Apple Inc\. build ([0-9]+)\)', line)
150                self.trace("line:", line)
151                if m:
152                    gcc_build = int(m.group(1))
153                    self.trace("gcc build:", gcc_build)
154                    if gcc_build >= 5666:
155                        # rdar://problem/9804600"
156                        self.skipTest(
157                            "rdar://problem/9804600 wrong namespace for std::string in debug info")
158
159        # Expand same expression, skipping 3 layers of summaries
160        self.expect(
161            'frame variable data1.m_child1->m_child2 --show-types --no-summary-depth=3',
162            substrs=[
163                '(DeepData_3) data1.m_child1->m_child2 = {',
164                'm_some_text = "Just a test"',
165                'm_child2 = {',
166                'm_some_text = "Just a test"'])
167
168        # Change summary and expand, first without --no-summary-depth then with
169        # --no-summary-depth
170        self.runCmd(
171            "type summary add --summary-string \"${var.m_some_text}\" DeepData_5")
172
173        self.expect('fr var data2.m_child4.m_child2.m_child2', substrs=[
174                    '(DeepData_5) data2.m_child4.m_child2.m_child2 = "Just a test"'])
175
176        self.expect(
177            'fr var data2.m_child4.m_child2.m_child2 --no-summary-depth',
178            substrs=[
179                '(DeepData_5) data2.m_child4.m_child2.m_child2 = {',
180                'm_some_text = "Just a test"',
181                '}'])
182