1"""
2Test lldb data formatter subsystem.
3"""
4
5import os, time
6import unittest2
7import lldb
8from lldbtest import *
9import lldbutil
10
11class SmartArrayDataFormatterTestCase(TestBase):
12
13    mydir = os.path.join("functionalities", "data-formatter", "data-formatter-smart-array")
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
53        # Execute the cleanup function during test case tear down.
54        self.addTearDownHook(cleanup)
55
56# check that we are not looping here
57        self.runCmd("type summary add --summary-string \"${var%V}\" SomeData")
58
59        self.expect("frame variable data",
60            substrs = ['invalid use of aggregate type'])
61# ${var%s}
62        self.runCmd("type summary add --summary-string \"ptr = ${var%s}\" \"char *\"")
63
64        self.expect("frame variable strptr",
65            substrs = ['ptr = \"',
66                       'Hello world!'])
67
68        self.expect("frame variable other.strptr",
69            substrs = ['ptr = \"',
70                        'Nested Hello world!'])
71
72        self.runCmd("type summary add --summary-string \"arr = ${var%s}\" -x \"char \\[[0-9]+\\]\"")
73
74        self.expect("frame variable strarr",
75                    substrs = ['arr = \"',
76                               'Hello world!'])
77
78        self.expect("frame variable other.strarr",
79                    substrs = ['arr = \"',
80                               'Nested Hello world!'])
81
82        self.expect("p strarr",
83                    substrs = ['arr = \"',
84                               'Hello world!'])
85
86        self.expect("p other.strarr",
87                    substrs = ['arr = \"',
88                               'Nested Hello world!'])
89
90# ${var%c}
91        self.runCmd("type summary add --summary-string \"ptr = ${var%c}\" \"char *\"")
92
93        self.expect("frame variable strptr",
94                substrs = ['ptr = \"',
95                           'Hello world!'])
96
97        self.expect("frame variable other.strptr",
98                substrs = ['ptr = \"',
99                           'Nested Hello world!'])
100
101        self.expect("p strptr",
102                    substrs = ['ptr = \"',
103                               'Hello world!'])
104
105        self.expect("p other.strptr",
106                    substrs = ['ptr = \"',
107                               'Nested Hello world!'])
108
109        self.runCmd("type summary add --summary-string \"arr = ${var%c}\" -x \"char \\[[0-9]+\\]\"")
110
111        self.expect("frame variable strarr",
112                    substrs = ['arr = \"',
113                               'Hello world!'])
114
115        self.expect("frame variable other.strarr",
116                    substrs = ['arr = \"',
117                               'Nested Hello world!'])
118
119        self.expect("p strarr",
120                    substrs = ['arr = \"',
121                               'Hello world!'])
122
123        self.expect("p other.strarr",
124                    substrs = ['arr = \"',
125                               'Nested Hello world!'])
126
127# ${var%char[]}
128        self.runCmd("type summary add --summary-string \"arr = ${var%char[]}\" -x \"char \\[[0-9]+\\]\"")
129
130        self.expect("frame variable strarr",
131                    substrs = ['arr = \"',
132                               'Hello world!'])
133
134        self.expect("frame variable other.strarr",
135                    substrs = ['arr = ',
136                               'Nested Hello world!'])
137
138        self.expect("p strarr",
139                    substrs = ['arr = \"',
140                               'Hello world!'])
141
142        self.expect("p other.strarr",
143                    substrs = ['arr = ',
144                               'Nested Hello world!'])
145
146        self.runCmd("type summary add --summary-string \"ptr = ${var%char[]}\" \"char *\"")
147
148        self.expect("frame variable strptr",
149            substrs = ['ptr = \"',
150            'Hello world!'])
151
152        self.expect("frame variable other.strptr",
153            substrs = ['ptr = \"',
154            'Nested Hello world!'])
155
156        self.expect("p strptr",
157                    substrs = ['ptr = \"',
158                               'Hello world!'])
159
160        self.expect("p other.strptr",
161                    substrs = ['ptr = \"',
162                               'Nested Hello world!'])
163
164# ${var%a}
165        self.runCmd("type summary add --summary-string \"arr = ${var%a}\" -x \"char \\[[0-9]+\\]\"")
166
167        self.expect("frame variable strarr",
168                    substrs = ['arr = \"',
169                               'Hello world!'])
170
171        self.expect("frame variable other.strarr",
172                    substrs = ['arr = ',
173                               'Nested Hello world!'])
174
175        self.expect("p strarr",
176                    substrs = ['arr = \"',
177                               'Hello world!'])
178
179        self.expect("p other.strarr",
180                    substrs = ['arr = ',
181                               'Nested Hello world!'])
182
183        self.runCmd("type summary add --summary-string \"ptr = ${var%a}\" \"char *\"")
184
185        self.expect("frame variable strptr",
186                    substrs = ['ptr = \"',
187                               'Hello world!'])
188
189        self.expect("frame variable other.strptr",
190                    substrs = ['ptr = \"',
191                               'Nested Hello world!'])
192
193        self.expect("p strptr",
194                    substrs = ['ptr = \"',
195                               'Hello world!'])
196
197        self.expect("p other.strptr",
198                    substrs = ['ptr = \"',
199                               'Nested Hello world!'])
200
201        self.runCmd("type summary add --summary-string \"ptr = ${var[]%char[]}\" \"char *\"")
202
203# I do not know the size of the data, but you are asking for a full array slice..
204# use the ${var%char[]} to obtain a string as result
205        self.expect("frame variable strptr", matching=False,
206                    substrs = ['ptr = \"',
207                               'Hello world!'])
208
209        self.expect("frame variable other.strptr", matching=False,
210                    substrs = ['ptr = \"',
211                               'Nested Hello world!'])
212
213        self.expect("p strptr", matching=False,
214                    substrs = ['ptr = \"',
215                               'Hello world!'])
216
217        self.expect("p other.strptr", matching=False,
218                    substrs = ['ptr = \"',
219                               'Nested Hello world!'])
220
221# You asked an array-style printout...
222        self.runCmd("type summary add --summary-string \"ptr = ${var[0-1]%char[]}\" \"char *\"")
223
224        self.expect("frame variable strptr",
225                    substrs = ['ptr = ',
226                               '[{H},{e}]'])
227
228        self.expect("frame variable other.strptr",
229                    substrs = ['ptr = ',
230                               '[{N},{e}]'])
231
232        self.expect("p strptr",
233                    substrs = ['ptr = ',
234                               '[{H},{e}]'])
235
236        self.expect("p other.strptr",
237                    substrs = ['ptr = ',
238                               '[{N},{e}]'])
239
240# using [] is required here
241        self.runCmd("type summary add --summary-string \"arr = ${var%x}\" \"int [5]\"")
242
243        self.expect("frame variable intarr",matching=False,
244                    substrs = ['0x00000001,0x00000001,0x00000002,0x00000003,0x00000005'])
245
246        self.expect("frame variable other.intarr", matching=False,
247                    substrs = ['0x00000009,0x00000008,0x00000007,0x00000006,0x00000005'])
248
249        self.runCmd("type summary add --summary-string \"arr = ${var[]%x}\" \"int [5]\"")
250
251        self.expect("frame variable intarr",
252                    substrs = ['intarr = arr =',
253                               '0x00000001,0x00000001,0x00000002,0x00000003,0x00000005'])
254
255        self.expect("frame variable other.intarr",
256                    substrs = ['intarr = arr =',
257                               '0x00000009,0x00000008,0x00000007,0x00000006,0x00000005'])
258
259# printing each array item as an array
260        self.runCmd("type summary add --summary-string \"arr = ${var[]%uint32_t[]}\" \"int [5]\"")
261
262        self.expect("frame variable intarr",
263                    substrs = ['intarr = arr =',
264                               '{0x00000001},{0x00000001},{0x00000002},{0x00000003},{0x00000005}'])
265
266        self.expect("frame variable other.intarr",
267                    substrs = ['intarr = arr = ',
268                               '{0x00000009},{0x00000008},{0x00000007},{0x00000006},{0x00000005}'])
269
270# printing full array as an array
271        self.runCmd("type summary add --summary-string \"arr = ${var%uint32_t[]}\" \"int [5]\"")
272
273        self.expect("frame variable intarr",
274                    substrs = ['intarr = arr =',
275                               '0x00000001,0x00000001,0x00000002,0x00000003,0x00000005'])
276
277        self.expect("frame variable other.intarr",
278                    substrs = ['intarr = arr =',
279                               '0x00000009,0x00000008,0x00000007,0x00000006,0x00000005'])
280
281# printing each array item as an array
282        self.runCmd("type summary add --summary-string \"arr = ${var[]%float32[]}\" \"float [7]\"")
283
284        self.expect("frame variable flarr",
285                    substrs = ['flarr = arr =',
286                               '{78.5},{77.25},{78},{76.125},{76.75},{76.875},{77}'])
287
288        self.expect("frame variable other.flarr",
289                    substrs = ['flarr = arr = ',
290                               '{25.5},{25.25},{25.125},{26.75},{27.375},{27.5},{26.125}'])
291
292# printing full array as an array
293        self.runCmd("type summary add --summary-string \"arr = ${var%float32[]}\" \"float [7]\"")
294
295        self.expect("frame variable flarr",
296                    substrs = ['flarr = arr =',
297                               '78.5,77.25,78,76.125,76.75,76.875,77'])
298
299        self.expect("frame variable other.flarr",
300                    substrs = ['flarr = arr =',
301                               '25.5,25.25,25.125,26.75,27.375,27.5,26.125'])
302
303# using array smart summary strings for pointers should make no sense
304        self.runCmd("type summary add --summary-string \"arr = ${var%float32[]}\" \"float *\"")
305        self.runCmd("type summary add --summary-string \"arr = ${var%int32_t[]}\" \"int *\"")
306
307        self.expect("frame variable flptr", matching=False,
308                    substrs = ['78.5,77.25,78,76.125,76.75,76.875,77'])
309
310        self.expect("frame variable intptr", matching=False,
311                    substrs = ['1,1,2,3,5'])
312
313# use y and Y
314        self.runCmd("type summary add --summary-string \"arr = ${var%y}\" \"float [7]\"")
315        self.runCmd("type summary add --summary-string \"arr = ${var%y}\" \"int [5]\"")
316
317        self.expect("frame variable flarr",
318                    substrs = ['flarr = arr =',
319                               '00 00 9d 42,00 80 9a 42,00 00 9c 42,00 40 98 42,00 80 99 42,00 c0 99 42,00 00 9a 42'])
320
321        self.expect("frame variable other.flarr",
322                    substrs = ['flarr = arr =',
323                               '00 00 cc 41,00 00 ca 41,00 00 c9 41,00 00 d6 41,00 00 db 41,00 00 dc 41,00 00 d1 41'])
324
325        self.expect("frame variable intarr",
326                    substrs = ['intarr = arr =',
327                               '01 00 00 00,01 00 00 00,02 00 00 00,03 00 00 00,05 00 00 00'])
328
329        self.expect("frame variable other.intarr",
330                    substrs = ['intarr = arr = ',
331                               '09 00 00 00,08 00 00 00,07 00 00 00,06 00 00 00,05 00 00 00'])
332
333        self.runCmd("type summary add --summary-string \"arr = ${var%Y}\" \"float [7]\"")
334        self.runCmd("type summary add --summary-string \"arr = ${var%Y}\" \"int [5]\"")
335
336        self.expect("frame variable flarr",
337                    substrs = ['flarr = arr =',
338                               '00 00 9d 42             ...B,00 80 9a 42             ...B,00 00 9c 42             ...B,00 40 98 42             .@.B,00 80 99 42             ...B,00 c0 99 42             ...B,00 00 9a 42             ...B'])
339
340        self.expect("frame variable other.flarr",
341                    substrs = ['flarr = arr =',
342                               '00 00 cc 41             ...A,00 00 ca 41             ...A,00 00 c9 41             ...A,00 00 d6 41             ...A,00 00 db 41             ...A,00 00 dc 41             ...A,00 00 d1 41             ...A'])
343
344        self.expect("frame variable intarr",
345                    substrs = ['intarr = arr =',
346                               '....,01 00 00 00',
347                               '....,05 00 00 00'])
348
349        self.expect("frame variable other.intarr",
350                    substrs = ['intarr = arr = ',
351                               '09 00 00 00',
352                               '....,07 00 00 00'])
353
354
355if __name__ == '__main__':
356    import atexit
357    lldb.SBDebugger.Initialize()
358    atexit.register(lambda: lldb.SBDebugger.Terminate())
359    unittest2.main()
360