1"""
2Test lldb data formatter subsystem.
3"""
4
5import os, time
6import unittest2
7import lldb
8from lldbtest import *
9import lldbutil
10
11class AdvDataFormatterTestCase(TestBase):
12
13    mydir = os.path.join("functionalities", "data-formatter", "data-formatter-advanced")
14
15    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
16    @dsym_test
17    def test_with_dsym_and_run_command(self):
18        """Test data formatter commands."""
19        self.buildDsym()
20        self.data_formatter_commands()
21
22    @dwarf_test
23    def test_with_dwarf_and_run_command(self):
24        """Test data formatter commands."""
25        self.buildDwarf()
26        self.data_formatter_commands()
27
28    def setUp(self):
29        # Call super's setUp().
30        TestBase.setUp(self)
31        # Find the line number to break at.
32        self.line = line_number('main.cpp', '// Set break point at this line.')
33
34    def data_formatter_commands(self):
35        """Test that that file and class static variables display correctly."""
36        self.runCmd("file a.out", CURRENT_EXECUTABLE_SET)
37
38        lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True)
39
40        self.runCmd("run", RUN_SUCCEEDED)
41
42        # The stop reason of the thread should be breakpoint.
43        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
44            substrs = ['stopped',
45                       'stop reason = breakpoint'])
46
47        # This is the function to remove the custom formats in order to have a
48        # clean slate for the next test case.
49        def cleanup():
50            self.runCmd('type format clear', check=False)
51            self.runCmd('type summary clear', check=False)
52            self.runCmd("settings set target.max-children-count 256", check=False)
53
54
55        # Execute the cleanup function during test case tear down.
56        self.addTearDownHook(cleanup)
57
58        self.runCmd("type summary add --summary-string \"pippo\" \"i_am_cool\"")
59
60        self.runCmd("type summary add --summary-string \"pluto\" -x \"i_am_cool[a-z]*\"")
61
62        self.expect("frame variable cool_boy",
63            substrs = ['pippo'])
64
65        self.expect("frame variable cooler_boy",
66            substrs = ['pluto'])
67
68        self.runCmd("type summary delete i_am_cool")
69
70        self.expect("frame variable cool_boy",
71            substrs = ['pluto'])
72
73        self.runCmd("type summary clear")
74
75        self.runCmd("type summary add --summary-string \"${var[]}\" -x \"int \\[[0-9]\\]")
76
77        self.expect("frame variable int_array",
78            substrs = ['1,2,3,4,5'])
79
80        # this will fail if we don't do [] as regex correctly
81        self.runCmd('type summary add --summary-string "${var[].integer}" "i_am_cool[]')
82
83        self.expect("frame variable cool_array",
84            substrs = ['1,1,1,1,6'])
85
86        self.runCmd("type summary clear")
87
88        self.runCmd("type summary add --summary-string \"${var[1-0]%x}\" \"int\"")
89
90        self.expect("frame variable iAmInt",
91            substrs = ['01'])
92
93        self.runCmd("type summary add --summary-string \"${var[0-1]%x}\" \"int\"")
94
95        self.expect("frame variable iAmInt",
96            substrs = ['01'])
97
98        self.runCmd("type summary clear")
99
100        self.runCmd("type summary add --summary-string \"${var[0-1]%x}\" int")
101        self.runCmd("type summary add --summary-string \"${var[0-31]%x}\" float")
102
103        self.expect("frame variable *pointer",
104            substrs = ['0x',
105                       '2'])
106
107        # check fix for <rdar://problem/11338654> LLDB crashes when using a "type summary" that uses bitfields with no format
108        self.runCmd("type summary add --summary-string \"${var[0-1]}\" int")
109        self.expect("frame variable iAmInt",
110            substrs = ['9 1'])
111
112        self.expect("frame variable cool_array[3].floating",
113            substrs = ['0x'])
114
115        self.runCmd("type summary add --summary-string \"low bits are ${*var[0-1]} tgt is ${*var}\" \"int *\"")
116
117        self.expect("frame variable pointer",
118            substrs = ['low bits are',
119                       'tgt is 6'])
120
121        self.expect("frame variable int_array --summary-string \"${*var[0-1]}\"",
122            substrs = ['3'])
123
124        self.runCmd("type summary clear")
125
126        self.runCmd('type summary add --summary-string \"${var[0-1]}\" -x \"int \[[0-9]\]\"')
127
128        self.expect("frame variable int_array",
129            substrs = ['1,2'])
130
131        self.runCmd('type summary add --summary-string \"${var[0-1]}\" "int []"')
132
133        self.expect("frame variable int_array",
134            substrs = ['1,2'])
135
136        self.runCmd("type summary clear")
137
138        self.runCmd("type summary add -c -x \"i_am_cool \[[0-9]\]\"")
139        self.runCmd("type summary add -c i_am_cool")
140
141        self.expect("frame variable cool_array",
142            substrs = ['[0]',
143                       '[1]',
144                       '[2]',
145                       '[3]',
146                       '[4]',
147                       'integer',
148                       'character',
149                       'floating'])
150
151        self.runCmd("type summary add --summary-string \"int = ${*var.int_pointer}, float = ${*var.float_pointer}\" IWrapPointers")
152
153        self.expect("frame variable wrapper",
154            substrs = ['int = 4',
155                       'float = 1.1'])
156
157        self.runCmd("type summary add --summary-string \"low bits = ${*var.int_pointer[2]}\" IWrapPointers -p")
158
159        self.expect("frame variable wrapper",
160            substrs = ['low bits = 1'])
161
162        self.expect("frame variable *wrap_pointer",
163            substrs = ['low bits = 1'])
164
165        self.runCmd("type summary clear")
166
167        self.expect("frame variable int_array --summary-string \"${var[0][0-2]%hex}\"",
168            substrs = ['0x',
169                       '7'])
170
171        self.runCmd("type summary clear")
172
173        self.runCmd("type summary add --summary-string \"${*var[].x[0-3]%hex} is a bitfield on a set of integers\" -x \"SimpleWithPointers \[[0-9]\]\"")
174
175        self.expect("frame variable couple --summary-string \"${*var.sp.x[0-2]} are low bits of integer ${*var.sp.x}. If I pretend it is an array I get ${var.sp.x[0-5]}\"",
176            substrs = ['1 are low bits of integer 9.',
177                       'If I pretend it is an array I get [9,'])
178
179        # if the summary has an error, we still display the value
180        self.expect("frame variable couple --summary-string \"${*var.sp.foo[0-2]\"",
181            substrs = ['(Couple) couple =  {','sp = {','z =','"X"'])
182
183
184        self.runCmd("type summary add --summary-string \"${*var.sp.x[0-2]} are low bits of integer ${*var.sp.x}. If I pretend it is an array I get ${var.sp.x[0-5]}\" Couple")
185
186        self.expect("frame variable sparray",
187            substrs = ['[0x0000000f,0x0000000c,0x00000009]'])
188
189        # check that we can format a variable in a summary even if a format is defined for its datatype
190        self.runCmd("type format add -f hex int")
191        self.runCmd("type summary add --summary-string \"x=${var.x%d}\" Simple")
192
193        self.expect("frame variable a_simple_object",
194            substrs = ['x=3'])
195
196        self.expect("frame variable a_simple_object", matching=False,
197                    substrs = ['0x0'])
198
199        # now check that the default is applied if we do not hand out a format
200        self.runCmd("type summary add --summary-string \"x=${var.x}\" Simple")
201
202        self.expect("frame variable a_simple_object", matching=False,
203                    substrs = ['x=3'])
204
205        self.expect("frame variable a_simple_object", matching=True,
206                    substrs = ['x=0x00000003'])
207
208        # check that we can correctly cap the number of children shown
209        self.runCmd("settings set target.max-children-count 5")
210
211        self.expect('frame variable a_long_guy', matching=True,
212            substrs = ['a_1',
213                       'b_1',
214                       'c_1',
215                       'd_1',
216                       'e_1',
217                       '...'])
218
219        # check that no further stuff is printed (not ALL values are checked!)
220        self.expect('frame variable a_long_guy', matching=False,
221                    substrs = ['f_1',
222                               'g_1',
223                               'h_1',
224                               'i_1',
225                               'j_1',
226                               'q_1',
227                               'a_2',
228                               'f_2',
229                               't_2',
230                               'w_2'])
231
232        self.runCmd("settings set target.max-children-count 1")
233        self.expect('frame variable a_long_guy', matching=True,
234                    substrs = ['a_1',
235                               '...'])
236        self.expect('frame variable a_long_guy', matching=False,
237                    substrs = ['b_1',
238                               'c_1',
239                               'd_1',
240                               'e_1'])
241        self.expect('frame variable a_long_guy', matching=False,
242                    substrs = ['f_1',
243                               'g_1',
244                               'h_1',
245                               'i_1',
246                               'j_1',
247                               'q_1',
248                               'a_2',
249                               'f_2',
250                               't_2',
251                               'w_2'])
252
253        self.runCmd("settings set target.max-children-count 30")
254        self.expect('frame variable a_long_guy', matching=True,
255                    substrs = ['a_1',
256                               'b_1',
257                               'c_1',
258                               'd_1',
259                               'e_1',
260                               'z_1',
261                               'a_2',
262                               'b_2',
263                               'c_2',
264                               'd_2',
265                               '...'])
266        self.expect('frame variable a_long_guy', matching=False,
267                    substrs = ['e_2',
268                               'n_2',
269                               'r_2',
270                               'i_2',
271                               'k_2',
272                               'o_2'])
273
274        # override the cap
275        self.expect('frame variable a_long_guy --show-all-children', matching=True,
276                    substrs = ['a_1',
277                               'b_1',
278                               'c_1',
279                               'd_1',
280                               'e_1',
281                               'z_1',
282                               'a_2',
283                               'b_2',
284                               'c_2',
285                               'd_2'])
286        self.expect('frame variable a_long_guy --show-all-children', matching=True,
287                    substrs = ['e_2',
288                               'n_2',
289                               'r_2',
290                               'i_2',
291                               'k_2',
292                               'o_2'])
293        self.expect('frame variable a_long_guy --show-all-children', matching=False,
294                    substrs = ['...'])
295
296
297if __name__ == '__main__':
298    import atexit
299    lldb.SBDebugger.Initialize()
300    atexit.register(lambda: lldb.SBDebugger.Terminate())
301    unittest2.main()
302