1"""
2Test lldb data formatter subsystem.
3"""
4
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class PrintArrayTestCase(TestBase):
14
15    def test_print_array(self):
16        """Test that expr -Z works"""
17        self.build()
18        self.printarray_data_formatter_commands()
19
20    def setUp(self):
21        # Call super's setUp().
22        TestBase.setUp(self)
23        # Find the line number to break at.
24        self.line = line_number('main.cpp', 'break here')
25
26    def printarray_data_formatter_commands(self):
27        """Test that expr -Z works"""
28        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
29
30        lldbutil.run_break_set_by_file_and_line(
31            self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True)
32
33        self.runCmd("run", RUN_SUCCEEDED)
34
35        # The stop reason of the thread should be breakpoint.
36        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
37                    substrs=['stopped',
38                             'stop reason = breakpoint'])
39
40        # This is the function to remove the custom formats in order to have a
41        # clean slate for the next test case.
42        def cleanup():
43            self.runCmd('type format clear', check=False)
44            self.runCmd('type summary clear', check=False)
45            self.runCmd('type synth clear', check=False)
46
47        # Execute the cleanup function during test case tear down.
48        self.addTearDownHook(cleanup)
49
50        self.expect(
51            'expr --element-count 3 -- data',
52            substrs=[
53                '[0] = 1',
54                '[1] = 3',
55                '[2] = 5'])
56        self.expect('expr data', substrs=['int *', '$', '0x'])
57        self.expect(
58            'expr -f binary --element-count 0 -- data',
59            substrs=[
60                'int *',
61                '$',
62                '0b'])
63        self.expect(
64            'expr -f hex --element-count 3 -- data',
65            substrs=[
66                '[0] = 0x',
67                '1',
68                '[1] = 0x',
69                '3',
70                '[2] = 0x',
71                '5'])
72        self.expect(
73            'expr -f binary --element-count 2 -- data',
74            substrs=[
75                'int *',
76                '$',
77                '0x',
78                '[0] = 0b',
79                '1',
80                '[1] = 0b',
81                '11'])
82        self.expect('parray 3 data', substrs=['[0] = 1', '[1] = 3', '[2] = 5'])
83        self.expect(
84            'parray `1 + 1 + 1` data',
85            substrs=[
86                '[0] = 1',
87                '[1] = 3',
88                '[2] = 5'])
89        self.expect(
90            'parray `data[1]` data',
91            substrs=[
92                '[0] = 1',
93                '[1] = 3',
94                '[2] = 5'])
95        self.expect(
96            'parray/x 3 data',
97            substrs=[
98                '[0] = 0x',
99                '1',
100                '[1] = 0x',
101                '3',
102                '[2] = 0x',
103                '5'])
104        self.expect(
105            'parray/x `data[1]` data',
106            substrs=[
107                '[0] = 0x',
108                '1',
109                '[1] = 0x',
110                '3',
111                '[2] = 0x',
112                '5'])
113
114        # check error conditions
115        self.expect(
116            'expr --element-count 10 -- 123',
117            error=True,
118            substrs=['expression cannot be used with --element-count as it does not refer to a pointer'])
119        self.expect(
120            'expr --element-count 10 -- (void*)123',
121            error=True,
122            substrs=['expression cannot be used with --element-count as it refers to a pointer to void'])
123        self.expect('parray data', error=True, substrs=[
124                    "invalid element count 'data'"])
125        self.expect(
126            'parray data data',
127            error=True,
128            substrs=["invalid element count 'data'"])
129        self.expect('parray', error=True, substrs=[
130                    'Not enough arguments provided'])
131