733c427cdaaf2b34e79f31f418993b8b643e8753
[openbsd] /
1 """
2 Test lldb data formatter subsystem.
3 """
4
5
6
7 import lldb
8 from lldbsuite.test.decorators import *
9 from lldbsuite.test.lldbtest import *
10 from lldbsuite.test import lldbutil
11
12
13 class CppDataFormatterTestCase(TestBase):
14
15     mydir = TestBase.compute_mydir(__file__)
16
17     def setUp(self):
18         # Call super's setUp().
19         TestBase.setUp(self)
20         # Find the line number to break at.
21         self.line = line_number('main.cpp', '// Set break point at this line.')
22
23     @skipIf(debug_info="gmodules",
24             bugnumber="https://bugs.llvm.org/show_bug.cgi?id=36048")
25     def test_with_run_command(self):
26         """Test that that file and class static variables display correctly."""
27         self.build()
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         # The stop reason of the thread should be breakpoint.
36         self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
37                     substrs=['stopped',
38                              'stop reason = breakpoint'])
39
40         self.expect("frame variable",
41                     substrs=['(Speed) SPILookHex = 5.55'  # Speed by default is 5.55.
42                              ])
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         self.runCmd("type format add -C yes -f x Speed BitField")
54         self.runCmd("type format add -C no -f c RealNumber")
55         self.runCmd("type format add -C no -f x Type2")
56         self.runCmd("type format add -C yes -f c Type1")
57
58         # The type format list should show our custom formats.
59         self.expect("type format list",
60                     substrs=['RealNumber',
61                              'Speed',
62                              'BitField',
63                              'Type1',
64                              'Type2'])
65
66         self.expect("frame variable",
67                     patterns=['\(Speed\) SPILookHex = 0x[0-9a-f]+'  # Speed should look hex-ish now.
68                               ])
69
70         # gcc4.2 on Mac OS X skips typedef chains in the DWARF output
71         if self.getCompiler() in ['clang', 'llvm-gcc']:
72             self.expect("frame variable",
73                         patterns=['\(SignalMask\) SMILookHex = 0x[0-9a-f]+'  # SignalMask should look hex-ish now.
74                                   ])
75             self.expect("frame variable", matching=False,
76                         patterns=['\(Type4\) T4ILookChar = 0x[0-9a-f]+'  # Type4 should NOT look hex-ish now.
77                                   ])
78
79         # Now let's delete the 'Speed' custom format.
80         self.runCmd("type format delete Speed")
81
82         # The type format list should not show 'Speed' at this point.
83         self.expect("type format list", matching=False,
84                     substrs=['Speed'])
85
86         # Delete type format for 'Speed', we should expect an error message.
87         self.expect("type format delete Speed", error=True,
88                     substrs=['no custom formatter for Speed'])
89
90         self.runCmd(
91             "type summary add --summary-string \"arr = ${var%s}\" -x \"char \\[[0-9]+\\]\" -v")
92
93         self.expect("frame variable strarr",
94                     substrs=['arr = "Hello world!"'])
95
96         self.runCmd("type summary clear")
97
98         self.runCmd(
99             "type summary add --summary-string \"ptr = ${var%s}\" \"char *\" -v")
100
101         self.expect("frame variable strptr",
102                     substrs=['ptr = "Hello world!"'])
103
104         self.runCmd(
105             "type summary add --summary-string \"arr = ${var%s}\" -x \"char \\[[0-9]+\\]\" -v")
106
107         self.expect("frame variable strarr",
108                     substrs=['arr = "Hello world!'])
109
110         # check that rdar://problem/10011145 (Standard summary format for
111         # char[] doesn't work as the result of "expr".) is solved
112         self.expect("p strarr",
113                     substrs=['arr = "Hello world!'])
114
115         self.expect("frame variable strptr",
116                     substrs=['ptr = "Hello world!"'])
117
118         self.expect("p strptr",
119                     substrs=['ptr = "Hello world!"'])
120
121         self.expect(
122             "p (char*)\"1234567890123456789012345678901234567890123456789012345678901234ABC\"",
123             substrs=[
124                 '(char *) $',
125                 ' = ptr = ',
126                 ' "1234567890123456789012345678901234567890123456789012345678901234ABC"'])
127
128         self.runCmd("type summary add -c Point")
129
130         self.expect("frame variable iAmSomewhere",
131                     substrs=['x = 4',
132                              'y = 6'])
133
134         self.expect("type summary list",
135                     substrs=['Point',
136                              'one-line'])
137
138         self.runCmd("type summary add --summary-string \"y=${var.y%x}\" Point")
139
140         self.expect("frame variable iAmSomewhere",
141                     substrs=['y=0x'])
142
143         self.runCmd(
144             "type summary add --summary-string \"y=${var.y},x=${var.x}\" Point")
145
146         self.expect("frame variable iAmSomewhere",
147                     substrs=['y=6',
148                              'x=4'])
149
150         self.runCmd("type summary add --summary-string \"hello\" Point -e")
151
152         self.expect("type summary list",
153                     substrs=['Point',
154                              'show children'])
155
156         self.expect("frame variable iAmSomewhere",
157                     substrs=['hello',
158                              'x = 4',
159                              '}'])
160
161         self.runCmd(
162             "type summary add --summary-string \"Sign: ${var[31]%B} Exponent: ${var[23-30]%x} Mantissa: ${var[0-22]%u}\" ShowMyGuts")
163
164         self.expect("frame variable cool_pointer->floating",
165                     substrs=['Sign: true',
166                              'Exponent: 0x',
167                              '80'])
168
169         self.runCmd("type summary add --summary-string \"a test\" i_am_cool")
170
171         self.expect("frame variable cool_pointer",
172                     substrs=['a test'])
173
174         self.runCmd(
175             "type summary add --summary-string \"a test\" i_am_cool --skip-pointers")
176
177         self.expect("frame variable cool_pointer",
178                     substrs=['a test'],
179                     matching=False)
180
181         self.runCmd(
182             "type summary add --summary-string \"${var[1-3]}\" \"int [5]\"")
183
184         self.expect("frame variable int_array",
185                     substrs=['2',
186                              '3',
187                              '4'])
188
189         self.runCmd("type summary clear")
190
191         self.runCmd(
192             "type summary add --summary-string \"${var[0-2].integer}\" \"i_am_cool *\"")
193         self.runCmd(
194             "type summary add --summary-string \"${var[2-4].integer}\" \"i_am_cool [5]\"")
195
196         self.expect("frame variable cool_array",
197                     substrs=['1,1,6'])
198
199         self.expect("frame variable cool_pointer",
200                     substrs=['3,0,0'])
201
202         # test special symbols for formatting variables into summaries
203         self.runCmd(
204             "type summary add --summary-string \"cool object @ ${var%L}\" i_am_cool")
205         self.runCmd("type summary delete \"i_am_cool [5]\"")
206
207         # this test might fail if the compiler tries to store
208         # these values into registers.. hopefully this is not
209         # going to be the case
210         self.expect("frame variable cool_array",
211                     substrs=['[0] = cool object @ 0x',
212                              '[1] = cool object @ 0x',
213                              '[2] = cool object @ 0x',
214                              '[3] = cool object @ 0x',
215                              '[4] = cool object @ 0x'])
216
217         # test getting similar output by exploiting ${var} = 'type @ location'
218         # for aggregates
219         self.runCmd("type summary add --summary-string \"${var}\" i_am_cool")
220
221         # this test might fail if the compiler tries to store
222         # these values into registers.. hopefully this is not
223         # going to be the case
224         self.expect("frame variable cool_array",
225                     substrs=['[0] = i_am_cool @ 0x',
226                              '[1] = i_am_cool @ 0x',
227                              '[2] = i_am_cool @ 0x',
228                              '[3] = i_am_cool @ 0x',
229                              '[4] = i_am_cool @ 0x'])
230
231         # test getting same output by exploiting %T and %L together for
232         # aggregates
233         self.runCmd(
234             "type summary add --summary-string \"${var%T} @ ${var%L}\" i_am_cool")
235
236         # this test might fail if the compiler tries to store
237         # these values into registers.. hopefully this is not
238         # going to be the case
239         self.expect("frame variable cool_array",
240                     substrs=['[0] = i_am_cool @ 0x',
241                              '[1] = i_am_cool @ 0x',
242                              '[2] = i_am_cool @ 0x',
243                              '[3] = i_am_cool @ 0x',
244                              '[4] = i_am_cool @ 0x'])
245
246         self.runCmd("type summary add --summary-string \"goofy\" i_am_cool")
247         self.runCmd(
248             "type summary add --summary-string \"${var.second_cool%S}\" i_am_cooler")
249
250         self.expect("frame variable the_coolest_guy",
251                     substrs=['(i_am_cooler) the_coolest_guy = goofy'])
252
253         # check that unwanted type specifiers are removed
254         self.runCmd("type summary delete i_am_cool")
255         self.runCmd(
256             "type summary add --summary-string \"goofy\" \"class i_am_cool\"")
257         self.expect("frame variable the_coolest_guy",
258                     substrs=['(i_am_cooler) the_coolest_guy = goofy'])
259
260         self.runCmd("type summary delete i_am_cool")
261         self.runCmd(
262             "type summary add --summary-string \"goofy\" \"enum i_am_cool\"")
263         self.expect("frame variable the_coolest_guy",
264                     substrs=['(i_am_cooler) the_coolest_guy = goofy'])
265
266         self.runCmd("type summary delete i_am_cool")
267         self.runCmd(
268             "type summary add --summary-string \"goofy\" \"struct i_am_cool\"")
269         self.expect("frame variable the_coolest_guy",
270                     substrs=['(i_am_cooler) the_coolest_guy = goofy'])
271
272         # many spaces, but we still do the right thing
273         self.runCmd("type summary delete i_am_cool")
274         self.runCmd(
275             "type summary add --summary-string \"goofy\" \"union     i_am_cool\"")
276         self.expect("frame variable the_coolest_guy",
277                     substrs=['(i_am_cooler) the_coolest_guy = goofy'])
278
279         # but that not *every* specifier is removed
280         self.runCmd("type summary delete i_am_cool")
281         self.runCmd(
282             "type summary add --summary-string \"goofy\" \"wrong i_am_cool\"")
283         self.expect("frame variable the_coolest_guy", matching=False,
284                     substrs=['(i_am_cooler) the_coolest_guy = goofy'])
285
286         # check that formats are not sticking since that is the behavior we
287         # want
288         self.expect("frame variable iAmInt --format hex",
289                     substrs=['(int) iAmInt = 0x00000001'])
290         self.expect(
291             "frame variable iAmInt",
292             matching=False,
293             substrs=['(int) iAmInt = 0x00000001'])
294         self.expect("frame variable iAmInt", substrs=['(int) iAmInt = 1'])