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 SmartArrayDataFormatterTestCase(TestBase):
14
15    def test_with_run_command(self):
16        """Test data formatter commands."""
17        self.build()
18        self.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', '// Set break point at this line.')
25
26    def data_formatter_commands(self):
27        """Test that that file and class static variables display correctly."""
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        process = self.dbg.GetSelectedTarget().GetProcess()
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
48        # Execute the cleanup function during test case tear down.
49        self.addTearDownHook(cleanup)
50
51# check that we are not looping here
52        self.runCmd("type summary add --summary-string \"${var%V}\" SomeData")
53
54        self.expect("frame variable data",
55                    substrs=['SomeData @ 0x'])
56# ${var%s}
57        self.runCmd(
58            "type summary add --summary-string \"ptr = ${var%s}\" \"char *\"")
59
60        self.expect("frame variable strptr",
61                    substrs=['ptr = \"',
62                             'Hello world!'])
63
64        self.expect("frame variable other.strptr",
65                    substrs=['ptr = \"',
66                             'Nested Hello world!'])
67
68        self.runCmd(
69            "type summary add --summary-string \"arr = ${var%s}\" -x \"char\\[[0-9]+\\]\"")
70
71        self.expect("frame variable strarr",
72                    substrs=['arr = \"',
73                             'Hello world!'])
74
75        self.expect("frame variable other.strarr",
76                    substrs=['arr = \"',
77                             'Nested Hello world!'])
78
79        self.expect("expression strarr",
80                    substrs=['arr = \"',
81                             'Hello world!'])
82
83        self.expect("expression other.strarr",
84                    substrs=['arr = \"',
85                             'Nested Hello world!'])
86
87# ${var%c}
88        self.runCmd(
89            "type summary add --summary-string \"ptr = ${var%c}\" \"char *\"")
90
91        self.expect("frame variable strptr",
92                    substrs=['ptr = \"',
93                             'Hello world!'])
94
95        self.expect("frame variable other.strptr",
96                    substrs=['ptr = \"',
97                             'Nested Hello world!'])
98
99        self.expect("expression strptr",
100                    substrs=['ptr = \"',
101                             'Hello world!'])
102
103        self.expect("expression other.strptr",
104                    substrs=['ptr = \"',
105                             'Nested Hello world!'])
106
107        self.runCmd(
108            "type summary add --summary-string \"arr = ${var%c}\" -x \"char\\[[0-9]+\\]\"")
109
110        self.expect("frame variable strarr",
111                    substrs=['arr = \"',
112                             'Hello world!'])
113
114        self.expect("frame variable other.strarr",
115                    substrs=['arr = \"',
116                             'Nested Hello world!'])
117
118        self.expect("expression strarr",
119                    substrs=['arr = \"',
120                             'Hello world!'])
121
122        self.expect("expression other.strarr",
123                    substrs=['arr = \"',
124                             'Nested Hello world!'])
125
126# ${var%char[]}
127        self.runCmd(
128            "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("expression strarr",
139                    substrs=['arr = \"',
140                             'Hello world!'])
141
142        self.expect("expression other.strarr",
143                    substrs=['arr = ',
144                             'Nested Hello world!'])
145
146        self.runCmd(
147            "type summary add --summary-string \"ptr = ${var%char[]}\" \"char *\"")
148
149        self.expect("frame variable strptr",
150                    substrs=['ptr = \"',
151                             'Hello world!'])
152
153        self.expect("frame variable other.strptr",
154                    substrs=['ptr = \"',
155                             'Nested Hello world!'])
156
157        self.expect("expression strptr",
158                    substrs=['ptr = \"',
159                             'Hello world!'])
160
161        self.expect("expression other.strptr",
162                    substrs=['ptr = \"',
163                             'Nested Hello world!'])
164
165# ${var%a}
166        self.runCmd(
167            "type summary add --summary-string \"arr = ${var%a}\" -x \"char\\[[0-9]+\\]\"")
168
169        self.expect("frame variable strarr",
170                    substrs=['arr = \"',
171                             'Hello world!'])
172
173        self.expect("frame variable other.strarr",
174                    substrs=['arr = ',
175                             'Nested Hello world!'])
176
177        self.expect("expression strarr",
178                    substrs=['arr = \"',
179                             'Hello world!'])
180
181        self.expect("expression other.strarr",
182                    substrs=['arr = ',
183                             'Nested Hello world!'])
184
185        self.runCmd(
186            "type summary add --summary-string \"ptr = ${var%a}\" \"char *\"")
187
188        self.expect("frame variable strptr",
189                    substrs=['ptr = \"',
190                             'Hello world!'])
191
192        self.expect("frame variable other.strptr",
193                    substrs=['ptr = \"',
194                             'Nested Hello world!'])
195
196        self.expect("expression strptr",
197                    substrs=['ptr = \"',
198                             'Hello world!'])
199
200        self.expect("expression other.strptr",
201                    substrs=['ptr = \"',
202                             'Nested Hello world!'])
203
204        self.runCmd(
205            "type summary add --summary-string \"ptr = ${var[]%char[]}\" \"char *\"")
206
207# I do not know the size of the data, but you are asking for a full array slice..
208# use the ${var%char[]} to obtain a string as result
209        self.expect("frame variable strptr", matching=False,
210                    substrs=['ptr = \"',
211                             'Hello world!'])
212
213        self.expect("frame variable other.strptr", matching=False,
214                    substrs=['ptr = \"',
215                             'Nested Hello world!'])
216
217        self.expect("expression strptr", matching=False,
218                    substrs=['ptr = \"',
219                             'Hello world!'])
220
221        self.expect("expression other.strptr", matching=False,
222                    substrs=['ptr = \"',
223                             'Nested Hello world!'])
224
225# You asked an array-style printout...
226        self.runCmd(
227            "type summary add --summary-string \"ptr = ${var[0-1]%char[]}\" \"char *\"")
228
229        self.expect("frame variable strptr",
230                    substrs=['ptr = ',
231                             '[{H},{e}]'])
232
233        self.expect("frame variable other.strptr",
234                    substrs=['ptr = ',
235                             '[{N},{e}]'])
236
237        self.expect("expression strptr",
238                    substrs=['ptr = ',
239                             '[{H},{e}]'])
240
241        self.expect("expression other.strptr",
242                    substrs=['ptr = ',
243                             '[{N},{e}]'])
244
245# using [] is required here
246        self.runCmd(
247            "type summary add --summary-string \"arr = ${var%x}\" \"int [5]\"")
248
249        self.expect("frame variable intarr", matching=False, substrs=[
250                    '0x00000001,0x00000001,0x00000002,0x00000003,0x00000005'])
251
252        self.expect("frame variable other.intarr", matching=False, substrs=[
253                    '0x00000009,0x00000008,0x00000007,0x00000006,0x00000005'])
254
255        self.runCmd(
256            "type summary add --summary-string \"arr = ${var[]%x}\" \"int[5]\"")
257
258        self.expect(
259            "frame variable intarr",
260            substrs=[
261                'intarr = arr =',
262                '0x00000001,0x00000001,0x00000002,0x00000003,0x00000005'])
263
264        self.expect(
265            "frame variable other.intarr",
266            substrs=[
267                'intarr = arr =',
268                '0x00000009,0x00000008,0x00000007,0x00000006,0x00000005'])
269
270# printing each array item as an array
271        self.runCmd(
272            "type summary add --summary-string \"arr = ${var[]%uint32_t[]}\" \"int[5]\"")
273
274        self.expect(
275            "frame variable intarr",
276            substrs=[
277                'intarr = arr =',
278                '{0x00000001},{0x00000001},{0x00000002},{0x00000003},{0x00000005}'])
279
280        self.expect(
281            "frame variable other.intarr",
282            substrs=[
283                'intarr = arr = ',
284                '{0x00000009},{0x00000008},{0x00000007},{0x00000006},{0x00000005}'])
285
286# printing full array as an array
287        self.runCmd(
288            "type summary add --summary-string \"arr = ${var%uint32_t[]}\" \"int[5]\"")
289
290        self.expect(
291            "frame variable intarr",
292            substrs=[
293                'intarr = arr =',
294                '0x00000001,0x00000001,0x00000002,0x00000003,0x00000005'])
295
296        self.expect(
297            "frame variable other.intarr",
298            substrs=[
299                'intarr = arr =',
300                '0x00000009,0x00000008,0x00000007,0x00000006,0x00000005'])
301
302# printing each array item as an array
303        self.runCmd(
304            "type summary add --summary-string \"arr = ${var[]%float32[]}\" \"float[7]\"")
305
306        self.expect(
307            "frame variable flarr",
308            substrs=[
309                'flarr = arr =',
310                '{78.5},{77.25},{78},{76.125},{76.75},{76.875},{77}'])
311
312        self.expect(
313            "frame variable other.flarr",
314            substrs=[
315                'flarr = arr = ',
316                '{25.5},{25.25},{25.125},{26.75},{27.375},{27.5},{26.125}'])
317
318# printing full array as an array
319        self.runCmd(
320            "type summary add --summary-string \"arr = ${var%float32[]}\" \"float[7]\"")
321
322        self.expect("frame variable flarr",
323                    substrs=['flarr = arr =',
324                             '78.5,77.25,78,76.125,76.75,76.875,77'])
325
326        self.expect("frame variable other.flarr",
327                    substrs=['flarr = arr =',
328                             '25.5,25.25,25.125,26.75,27.375,27.5,26.125'])
329
330# using array smart summary strings for pointers should make no sense
331        self.runCmd(
332            "type summary add --summary-string \"arr = ${var%float32[]}\" \"float *\"")
333        self.runCmd(
334            "type summary add --summary-string \"arr = ${var%int32_t[]}\" \"int *\"")
335
336        self.expect("frame variable flptr", matching=False,
337                    substrs=['78.5,77.25,78,76.125,76.75,76.875,77'])
338
339        self.expect("frame variable intptr", matching=False,
340                    substrs=['1,1,2,3,5'])
341
342# use y and Y
343        self.runCmd(
344            "type summary add --summary-string \"arr = ${var%y}\" \"float[7]\"")
345        self.runCmd(
346            "type summary add --summary-string \"arr = ${var%y}\" \"int[5]\"")
347
348        if process.GetByteOrder() == lldb.eByteOrderLittle:
349            self.expect(
350                "frame variable flarr",
351                substrs=[
352                    'flarr = arr =',
353                    '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'])
354        else:
355            self.expect(
356                "frame variable flarr",
357                substrs=[
358                    'flarr = arr =',
359                    '42 9d 00 00,42 9a 80 00,42 9c 00 00,42 98 40 00,42 99 80 00,42 99 c0 00,42 9a 00 00'])
360
361        if process.GetByteOrder() == lldb.eByteOrderLittle:
362            self.expect(
363                "frame variable other.flarr",
364                substrs=[
365                    'flarr = arr =',
366                    '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'])
367        else:
368            self.expect(
369                "frame variable other.flarr",
370                substrs=[
371                    'flarr = arr =',
372                    '41 cc 00 00,41 ca 00 00,41 c9 00 00,41 d6 00 00,41 db 00 00,41 dc 00 00,41 d1 00 00'])
373
374        if process.GetByteOrder() == lldb.eByteOrderLittle:
375            self.expect(
376                "frame variable intarr",
377                substrs=[
378                    'intarr = arr =',
379                    '01 00 00 00,01 00 00 00,02 00 00 00,03 00 00 00,05 00 00 00'])
380        else:
381            self.expect(
382                "frame variable intarr",
383                substrs=[
384                    'intarr = arr =',
385                    '00 00 00 01,00 00 00 01,00 00 00 02,00 00 00 03,00 00 00 05'])
386
387        if process.GetByteOrder() == lldb.eByteOrderLittle:
388            self.expect(
389                "frame variable other.intarr",
390                substrs=[
391                    'intarr = arr = ',
392                    '09 00 00 00,08 00 00 00,07 00 00 00,06 00 00 00,05 00 00 00'])
393        else:
394            self.expect(
395                "frame variable other.intarr",
396                substrs=[
397                    'intarr = arr = ',
398                    '00 00 00 09,00 00 00 08,00 00 00 07,00 00 00 06,00 00 00 05'])
399
400        self.runCmd(
401            "type summary add --summary-string \"arr = ${var%Y}\" \"float[7]\"")
402        self.runCmd(
403            "type summary add --summary-string \"arr = ${var%Y}\" \"int[5]\"")
404
405        if process.GetByteOrder() == lldb.eByteOrderLittle:
406            self.expect(
407                "frame variable flarr",
408                substrs=[
409                    'flarr = arr =',
410                    '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'])
411        else:
412            self.expect(
413                "frame variable flarr",
414                substrs=[
415                    'flarr = arr =',
416                    '42 9d 00 00', '42 9a 80 00', '42 9c 00 00', '42 98 40 00', '42 99 80 00', '42 99 c0 00', '42 9a 00 00'])
417
418        if process.GetByteOrder() == lldb.eByteOrderLittle:
419            self.expect(
420                "frame variable other.flarr",
421                substrs=[
422                    'flarr = arr =',
423                    '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'])
424        else:
425            self.expect(
426                "frame variable other.flarr",
427                substrs=[
428                    'flarr = arr =',
429                    '41 cc 00 00', '41 ca 00 00', '41 c9 00 00', '41 d6 00 00', '41 db 00 00', '41 dc 00 00', '41 d1 00 00'])
430
431        if process.GetByteOrder() == lldb.eByteOrderLittle:
432            self.expect("frame variable intarr",
433                        substrs=['intarr = arr =',
434                                 '....,01 00 00 00',
435                                 '....,05 00 00 00'])
436        else:
437            self.expect("frame variable intarr",
438                        substrs=['intarr = arr =',
439                                 '....,00 00 00 01',
440                                 '....,00 00 00 05'])
441
442        if process.GetByteOrder() == lldb.eByteOrderLittle:
443            self.expect("frame variable other.intarr",
444                        substrs=['intarr = arr = ',
445                                 '09 00 00 00',
446                                 '....,07 00 00 00'])
447        else:
448            self.expect("frame variable other.intarr",
449                        substrs=['intarr = arr = ',
450                                 '00 00 00 09',
451                                 '....,00 00 00 07'])
452