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