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