5f908f76b0abc91a72b9ddec3934887d85458c68
[openbsd] /
1 """
2 Test lldb data formatter subsystem.
3 """
4
5 from __future__ import print_function
6
7
8 import lldb
9 from lldbsuite.test.decorators import *
10 from lldbsuite.test.lldbtest import *
11 from lldbsuite.test import lldbutil
12
13
14 class PythonSynthDataFormatterTestCase(TestBase):
15
16     mydir = TestBase.compute_mydir(__file__)
17
18     @skipIfFreeBSD  # llvm.org/pr20545 bogus output confuses buildbot parser
19     def test_with_run_command(self):
20         """Test data formatter commands."""
21         self.build()
22         self.data_formatter_commands()
23
24     def test_rdar10960550_with_run_command(self):
25         """Test data formatter commands."""
26         self.build()
27         self.rdar10960550_formatter_commands()
28
29     def setUp(self):
30         # Call super's setUp().
31         TestBase.setUp(self)
32         # Find the line number to break at.
33         self.line = line_number('main.cpp', '// Set break point at this line.')
34         self.line2 = line_number('main.cpp',
35                                  '// Set cast break point at this line.')
36         self.line3 = line_number(
37             'main.cpp', '// Set second cast break point at this line.')
38
39     def data_formatter_commands(self):
40         """Test using Python synthetic children provider."""
41         self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
42
43         lldbutil.run_break_set_by_file_and_line(
44             self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True)
45
46         self.runCmd("run", RUN_SUCCEEDED)
47
48         process = self.dbg.GetSelectedTarget().GetProcess()
49
50         # The stop reason of the thread should be breakpoint.
51         self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
52                     substrs=['stopped',
53                              'stop reason = breakpoint'])
54
55         # This is the function to remove the custom formats in order to have a
56         # clean slate for the next test case.
57         def cleanup():
58             self.runCmd('type format clear', check=False)
59             self.runCmd('type summary clear', check=False)
60             self.runCmd('type filter clear', check=False)
61             self.runCmd('type synth clear', check=False)
62
63         # Execute the cleanup function during test case tear down.
64         self.addTearDownHook(cleanup)
65
66         # print the f00_1 variable without a synth
67         self.expect("frame variable f00_1",
68                     substrs=['a = 1',
69                              'b = 2',
70                              'r = 34'])
71
72         # now set up the synth
73         self.runCmd("script from fooSynthProvider import *")
74         self.runCmd("type synth add -l fooSynthProvider foo")
75         self.expect("type synthetic list foo", substrs=['fooSynthProvider'])
76
77         # note that the value of fake_a depends on target byte order
78         if process.GetByteOrder() == lldb.eByteOrderLittle:
79             fake_a_val = 0x02000000
80         else:
81             fake_a_val = 0x00000100
82
83         # check that we get the two real vars and the fake_a variables
84         self.expect("frame variable f00_1",
85                     substrs=['r = 34',
86                              'fake_a = %d' % fake_a_val,
87                              'a = 1'])
88
89         # check that we do not get the extra vars
90         self.expect("frame variable f00_1", matching=False,
91                     substrs=['b = 2'])
92
93         # check access to members by name
94         self.expect('frame variable f00_1.fake_a',
95                     substrs=['%d' % fake_a_val])
96
97         # check access to members by index
98         self.expect('frame variable f00_1[1]',
99                     substrs=['%d' % fake_a_val])
100
101         # put synthetic children in summary in several combinations
102         self.runCmd(
103             "type summary add --summary-string \"fake_a=${svar.fake_a}\" foo")
104         self.expect('frame variable f00_1',
105                     substrs=['fake_a=%d' % fake_a_val])
106         self.runCmd(
107             "type summary add --summary-string \"fake_a=${svar[1]}\" foo")
108         self.expect('frame variable f00_1',
109                     substrs=['fake_a=%d' % fake_a_val])
110
111         # clear the summary
112         self.runCmd("type summary delete foo")
113
114         # check that the caching does not span beyond the stopoint
115         self.runCmd("n")
116
117         if process.GetByteOrder() == lldb.eByteOrderLittle:
118             fake_a_val = 0x02000000
119         else:
120             fake_a_val = 0x00000200
121
122         self.expect("frame variable f00_1",
123                     substrs=['r = 34',
124                              'fake_a = %d' % fake_a_val,
125                              'a = 2'])
126
127         # check that altering the object also alters fake_a
128         self.runCmd("expr f00_1.a = 280")
129
130         if process.GetByteOrder() == lldb.eByteOrderLittle:
131             fake_a_val = 0x02000001
132         else:
133             fake_a_val = 0x00011800
134
135         self.expect("frame variable f00_1",
136                     substrs=['r = 34',
137                              'fake_a = %d' % fake_a_val,
138                              'a = 280'])
139
140         # check that expanding a pointer does the right thing
141         if process.GetByteOrder() == lldb.eByteOrderLittle:
142             fake_a_val = 0x0d000000
143         else:
144             fake_a_val = 0x00000c00
145
146         self.expect("frame variable --ptr-depth 1 f00_ptr",
147                     substrs=['r = 45',
148                              'fake_a = %d' % fake_a_val,
149                              'a = 12'])
150
151         # now add a filter.. it should fail
152         self.expect("type filter add foo --child b --child j", error=True,
153                     substrs=['cannot add'])
154
155         # we get the synth again..
156         self.expect('frame variable f00_1', matching=False,
157                     substrs=['b = 1',
158                              'j = 17'])
159         self.expect("frame variable --ptr-depth 1 f00_ptr",
160                     substrs=['r = 45',
161                              'fake_a = %d' % fake_a_val,
162                              'a = 12'])
163
164         # now delete the synth and add the filter
165         self.runCmd("type synth delete foo")
166         self.runCmd("type filter add foo --child b --child j")
167
168         self.expect('frame variable f00_1',
169                     substrs=['b = 2',
170                              'j = 18'])
171         self.expect("frame variable --ptr-depth 1 f00_ptr", matching=False,
172                     substrs=['r = 45',
173                              'fake_a = %d' % fake_a_val,
174                              'a = 12'])
175
176         # now add the synth and it should fail
177         self.expect("type synth add -l fooSynthProvider foo", error=True,
178                     substrs=['cannot add'])
179
180         # check the listing
181         self.expect('type synth list', matching=False,
182                     substrs=['foo',
183                              'Python class fooSynthProvider'])
184         self.expect('type filter list',
185                     substrs=['foo',
186                              '.b',
187                              '.j'])
188
189         # delete the filter, add the synth
190         self.runCmd("type filter delete foo")
191         self.runCmd("type synth add -l fooSynthProvider foo")
192
193         self.expect('frame variable f00_1', matching=False,
194                     substrs=['b = 2',
195                              'j = 18'])
196         self.expect("frame variable --ptr-depth 1 f00_ptr",
197                     substrs=['r = 45',
198                              'fake_a = %d' % fake_a_val,
199                              'a = 12'])
200
201         # check the listing
202         self.expect('type synth list',
203                     substrs=['foo',
204                              'Python class fooSynthProvider'])
205         self.expect('type filter list', matching=False,
206                     substrs=['foo',
207                              '.b',
208                              '.j'])
209
210         # delete the synth and check that we get good output
211         self.runCmd("type synth delete foo")
212
213         self.expect("frame variable f00_1",
214                     substrs=['a = 280',
215                              'b = 2',
216                              'j = 18'])
217
218         self.expect("frame variable f00_1", matching=False,
219                     substrs=['fake_a = '])
220
221     def rdar10960550_formatter_commands(self):
222         """Test that synthetic children persist stoppoints."""
223         self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
224
225         # The second breakpoint is on a multi-line expression, so the comment
226         # can't be on the right line...
227         lldbutil.run_break_set_by_file_and_line(
228             self, "main.cpp", self.line2, num_expected_locations=1, loc_exact=False)
229         lldbutil.run_break_set_by_file_and_line(
230             self, "main.cpp", self.line3, num_expected_locations=1, loc_exact=True)
231
232         self.runCmd("run", RUN_SUCCEEDED)
233
234         # The stop reason of the thread should be breakpoint.
235         self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
236                     substrs=['stopped',
237                              'stop reason = breakpoint'])
238
239         # This is the function to remove the custom formats in order to have a
240         # clean slate for the next test case.
241         def cleanup():
242             self.runCmd('type format clear', check=False)
243             self.runCmd('type summary clear', check=False)
244             self.runCmd('type filter clear', check=False)
245             self.runCmd('type synth clear', check=False)
246
247         # Execute the cleanup function during test case tear down.
248         self.addTearDownHook(cleanup)
249
250         self.runCmd("command script import ./ftsp.py --allow-reload")
251         self.runCmd("type synth add -l ftsp.ftsp wrapint")
252
253         # we need to check that the VO is properly updated so that the same synthetic children are reused
254         # but their values change correctly across stop-points - in order to do this, self.runCmd("next")
255         # does not work because it forces a wipe of the stack frame - this is why we are using this more contrived
256         # mechanism to achieve our goal of preserving test_cast as a VO
257         test_cast = self.dbg.GetSelectedTarget().GetProcess(
258         ).GetSelectedThread().GetSelectedFrame().FindVariable('test_cast')
259
260         str_cast = str(test_cast)
261
262         if self.TraceOn():
263             print(str_cast)
264
265         self.assertTrue(str_cast.find('A') != -1, 'could not find A in output')
266         self.assertTrue(str_cast.find('B') != -1, 'could not find B in output')
267         self.assertTrue(str_cast.find('C') != -1, 'could not find C in output')
268         self.assertTrue(str_cast.find('D') != -1, 'could not find D in output')
269         self.assertTrue(
270             str_cast.find("4 = '\\0'") != -1,
271             'could not find item 4 == 0')
272
273         self.dbg.GetSelectedTarget().GetProcess().GetSelectedThread().StepOver()
274
275         str_cast = str(test_cast)
276
277         if self.TraceOn():
278             print(str_cast)
279
280         # we detect that all the values of the child objects have changed - but the counter-generated item
281         # is still fixed at 0 because it is cached - this would fail if update(self): in ftsp returned False
282         # or if synthetic children were not being preserved
283         self.assertTrue(str_cast.find('Q') != -1, 'could not find Q in output')
284         self.assertTrue(str_cast.find('X') != -1, 'could not find X in output')
285         self.assertTrue(str_cast.find('T') != -1, 'could not find T in output')
286         self.assertTrue(str_cast.find('F') != -1, 'could not find F in output')
287         self.assertTrue(
288             str_cast.find("4 = '\\0'") != -1,
289             'could not find item 4 == 0')