[ValueObjectSynthetic and ValueObjectDynamicValue] Override GetDeclaration
[lldb.git] / lldb / test / python_api / formatters / TestFormattersSBAPI.py
1 """Test Python APIs for working with formatters"""
2
3 import os, sys, time
4 import unittest2
5 import lldb
6 from lldbtest import *
7 import lldbutil
8
9 class SBFormattersAPITestCase(TestBase):
10
11     mydir = TestBase.compute_mydir(__file__)
12
13     @skipUnlessDarwin
14     @python_api_test
15     @dsym_test
16     def test_with_dsym_formatters_api(self):
17         """Test Python APIs for working with formatters"""
18         self.buildDsym()
19         self.setTearDownCleanup()
20         self.formatters()
21
22     @expectedFailureFreeBSD("llvm.org/pr24282 Empty2 fails")
23     @python_api_test
24     @dwarf_test
25     def test_with_dwarf_formatters_api(self):
26         """Test Python APIs for working with formatters"""
27         self.buildDwarf()
28         self.setTearDownCleanup()
29         self.formatters()
30
31     @python_api_test
32     def test_force_synth_off(self):
33         """Test that one can have the public API return non-synthetic SBValues if desired"""
34         self.buildDwarf(dictionary={'EXE':'no_synth'})
35         self.setTearDownCleanup()
36         self.force_synth_off()
37
38     def setUp(self):
39         # Call super's setUp().
40         TestBase.setUp(self)
41         self.line = line_number('main.cpp', '// Set break point at this line.')
42
43     def formatters(self):
44         """Test Python APIs for working with formatters"""
45         self.runCmd("file a.out", CURRENT_EXECUTABLE_SET)
46
47         lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True)
48
49         self.runCmd("run", RUN_SUCCEEDED)
50
51         # The stop reason of the thread should be breakpoint.
52         self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
53             substrs = ['stopped',
54                        'stop reason = breakpoint'])
55         
56         # This is the function to remove the custom formats in order to have a
57         # clean slate for the next test case.
58         def cleanup():
59             self.runCmd('type format clear', check=False)
60             self.runCmd('type summary clear', check=False)
61             self.runCmd('type filter clear', check=False)
62             self.runCmd('type synthetic clear', check=False)
63             self.runCmd('type category delete foobar', check=False)
64             self.runCmd('type category delete JASSynth', check=False)
65             self.runCmd('type category delete newbar', check=False)
66
67         # Execute the cleanup function during test case tear down.
68         self.addTearDownHook(cleanup)
69
70
71         format = lldb.SBTypeFormat(lldb.eFormatHex)
72         category = self.dbg.GetDefaultCategory()
73         category.AddTypeFormat(lldb.SBTypeNameSpecifier("int"),format)
74
75         self.expect("frame variable foo.A",
76              substrs = ['0x00000001'])
77         self.expect("frame variable foo.E", matching=False,
78              substrs = ['b8cca70a'])
79
80         category.AddTypeFormat(lldb.SBTypeNameSpecifier("long"),format)
81         self.expect("frame variable foo.A",
82              substrs = ['0x00000001'])
83         self.expect("frame variable foo.E",
84              substrs = ['b8cca70a'])
85         
86         format.format = lldb.eFormatOctal
87         category.AddTypeFormat(lldb.SBTypeNameSpecifier("int"),format)
88         self.expect("frame variable foo.A",
89              substrs = ['01'])
90         self.expect("frame variable foo.E",
91              substrs = ['b8cca70a'])
92         
93         category.DeleteTypeFormat(lldb.SBTypeNameSpecifier("int"))
94         category.DeleteTypeFormat(lldb.SBTypeNameSpecifier("long"))
95         self.expect("frame variable foo.A", matching=False,
96              substrs = ['01'])
97         self.expect("frame variable foo.E", matching=False,
98              substrs = ['b8cca70a'])
99
100         summary = lldb.SBTypeSummary.CreateWithSummaryString("the hello world you'll never see")
101         summary.SetSummaryString('hello world')
102         new_category = self.dbg.GetCategory("foobar")
103         self.assertFalse(new_category.IsValid(), "getting a non-existing category worked")
104         new_category = self.dbg.CreateCategory("foobar")
105         new_category.enabled = True
106         new_category.AddTypeSummary(lldb.SBTypeNameSpecifier("^.*t$",True),summary)
107         self.expect("frame variable foo.A",
108              substrs = ['hello world'])
109         self.expect("frame variable foo.E", matching=False,
110              substrs = ['hello world'])
111         self.expect("frame variable foo.B",
112              substrs = ['hello world'])
113         self.expect("frame variable foo.F",
114              substrs = ['hello world'])
115         new_category.enabled = False
116         self.expect("frame variable foo.A", matching=False,
117              substrs = ['hello world'])
118         self.expect("frame variable foo.E", matching=False,
119              substrs = ['hello world'])
120         self.expect("frame variable foo.B", matching=False,
121              substrs = ['hello world'])
122         self.expect("frame variable foo.F", matching=False,
123              substrs = ['hello world'])
124         self.dbg.DeleteCategory(new_category.GetName())
125         self.expect("frame variable foo.A", matching=False,
126              substrs = ['hello world'])
127         self.expect("frame variable foo.E", matching=False,
128              substrs = ['hello world'])
129         self.expect("frame variable foo.B", matching=False,
130              substrs = ['hello world'])
131         self.expect("frame variable foo.F", matching=False,
132              substrs = ['hello world'])
133
134         filter = lldb.SBTypeFilter(0)
135         filter.AppendExpressionPath("A")
136         filter.AppendExpressionPath("D")
137         self.assertTrue(filter.GetNumberOfExpressionPaths() == 2, "filter with two items does not have two items")
138
139         category.AddTypeFilter(lldb.SBTypeNameSpecifier("JustAStruct"),filter)
140         self.expect("frame variable foo",
141              substrs = ['A = 1', 'D = 6.28'])
142         self.expect("frame variable foo", matching=False,
143              substrs = ['B = ', 'C = ', 'E = ', 'F = '])
144
145         category.DeleteTypeFilter(lldb.SBTypeNameSpecifier("JustAStruct",True))
146         self.expect("frame variable foo",
147              substrs = ['A = 1', 'D = 6.28'])
148         self.expect("frame variable foo", matching=False,
149              substrs = ['B = ', 'C = ', 'E = ', 'F = '])
150
151         category.DeleteTypeFilter(lldb.SBTypeNameSpecifier("JustAStruct",False))
152         self.expect("frame variable foo",
153              substrs = ['A = 1', 'D = 6.28'])
154         self.expect("frame variable foo", matching=True,
155              substrs = ['B = ', 'C = ', 'E = ', 'F = '])
156
157         self.runCmd("command script import --allow-reload ./synth.py")
158
159         self.expect("frame variable foo", matching=False,
160              substrs = ['X = 1'])
161
162         self.dbg.GetCategory("JASSynth").SetEnabled(True)
163         self.expect("frame variable foo", matching=True,
164              substrs = ['X = 1'])
165
166         self.dbg.GetCategory("CCCSynth").SetEnabled(True)
167         self.expect("frame variable ccc", matching=True,
168              substrs = ['CCC object with leading value (int) a = 111', 'a = 111', 'b = 222', 'c = 333'])
169
170         foo_var = self.dbg.GetSelectedTarget().GetProcess().GetSelectedThread().GetSelectedFrame().FindVariable('foo')
171         self.assertTrue(foo_var.IsValid(), 'could not find foo')
172         self.assertTrue(foo_var.GetDeclaration().IsValid(), 'foo declaration is invalid')
173
174         self.assertTrue(foo_var.GetNumChildren() == 2, 'synthetic value has wrong number of child items (synth)')
175         self.assertTrue(foo_var.GetChildMemberWithName('X').GetValueAsUnsigned() == 1, 'foo_synth.X has wrong value (synth)')
176         self.assertFalse(foo_var.GetChildMemberWithName('B').IsValid(), 'foo_synth.B is valid but should not (synth)')
177
178         self.dbg.GetCategory("JASSynth").SetEnabled(False)
179         foo_var = self.dbg.GetSelectedTarget().GetProcess().GetSelectedThread().GetSelectedFrame().FindVariable('foo')
180         self.assertTrue(foo_var.IsValid(), 'could not find foo')
181
182         self.assertFalse(foo_var.GetNumChildren() == 2, 'still seeing synthetic value')
183
184         filter = lldb.SBTypeFilter(0)
185         filter.AppendExpressionPath("A")
186         filter.AppendExpressionPath("D")
187         category.AddTypeFilter(lldb.SBTypeNameSpecifier("JustAStruct"),filter)
188         self.expect("frame variable foo",
189              substrs = ['A = 1', 'D = 6.28'])
190
191         foo_var = self.dbg.GetSelectedTarget().GetProcess().GetSelectedThread().GetSelectedFrame().FindVariable('foo')
192         self.assertTrue(foo_var.IsValid(), 'could not find foo')
193
194         self.assertTrue(foo_var.GetNumChildren() == 2, 'synthetic value has wrong number of child items (filter)')
195         self.assertTrue(foo_var.GetChildMemberWithName('X').GetValueAsUnsigned() == 0, 'foo_synth.X has wrong value (filter)')
196         self.assertTrue(foo_var.GetChildMemberWithName('A').GetValueAsUnsigned() == 1, 'foo_synth.A has wrong value (filter)')
197
198         self.assertTrue(filter.ReplaceExpressionPathAtIndex(0,"C"), "failed to replace an expression path in filter")
199         self.expect("frame variable foo",
200              substrs = ['A = 1', 'D = 6.28'])
201         category.AddTypeFilter(lldb.SBTypeNameSpecifier("JustAStruct"),filter)
202         self.expect("frame variable foo",
203              substrs = ["C = 'e'", 'D = 6.28'])
204         category.AddTypeFilter(lldb.SBTypeNameSpecifier("FooType"),filter)
205         filter.ReplaceExpressionPathAtIndex(1,"F")
206         self.expect("frame variable foo",
207              substrs = ["C = 'e'", 'D = 6.28'])
208         category.AddTypeFilter(lldb.SBTypeNameSpecifier("JustAStruct"),filter)
209         self.expect("frame variable foo",
210              substrs = ["C = 'e'", 'F = 0'])
211         self.expect("frame variable bar",
212              substrs = ["C = 'e'", 'D = 6.28'])
213
214         foo_var = self.dbg.GetSelectedTarget().GetProcess().GetSelectedThread().GetSelectedFrame().FindVariable('foo')
215         self.assertTrue(foo_var.IsValid(), 'could not find foo')
216         self.assertTrue(foo_var.GetChildMemberWithName('C').GetValueAsUnsigned() == ord('e'), 'foo_synth.C has wrong value (filter)')
217
218         chosen = self.dbg.GetFilterForType(lldb.SBTypeNameSpecifier("JustAStruct"))
219         self.assertTrue(chosen.count == 2, "wrong filter found for JustAStruct")
220         self.assertTrue(chosen.GetExpressionPathAtIndex(0) == 'C', "wrong item at index 0 for JustAStruct")
221         self.assertTrue(chosen.GetExpressionPathAtIndex(1) == 'F', "wrong item at index 1 for JustAStruct")
222
223         self.assertFalse(category.DeleteTypeFilter(lldb.SBTypeNameSpecifier("NoSuchType")),"deleting a non-existing filter worked")
224         self.assertFalse(category.DeleteTypeSummary(lldb.SBTypeNameSpecifier("NoSuchType")),"deleting a non-existing summary worked")
225         self.assertFalse(category.DeleteTypeFormat(lldb.SBTypeNameSpecifier("NoSuchType")),"deleting a non-existing format worked")
226         self.assertFalse(category.DeleteTypeSynthetic(lldb.SBTypeNameSpecifier("NoSuchType")),"deleting a non-existing synthetic worked")
227
228         self.assertFalse(category.DeleteTypeFilter(lldb.SBTypeNameSpecifier("")),"deleting a filter for '' worked")
229         self.assertFalse(category.DeleteTypeSummary(lldb.SBTypeNameSpecifier("")),"deleting a summary for '' worked")
230         self.assertFalse(category.DeleteTypeFormat(lldb.SBTypeNameSpecifier("")),"deleting a format for '' worked")
231         self.assertFalse(category.DeleteTypeSynthetic(lldb.SBTypeNameSpecifier("")),"deleting a synthetic for '' worked")
232
233         try:
234              self.assertFalse(category.AddTypeSummary(lldb.SBTypeNameSpecifier("NoneSuchType"), None), "adding a summary valued None worked")
235         except:
236              pass
237         else:
238              self.assertFalse(True, "adding a summary valued None worked")
239
240         try:
241              self.assertFalse(category.AddTypeFilter(lldb.SBTypeNameSpecifier("NoneSuchType"), None), "adding a filter valued None worked")
242         except:
243              pass
244         else:
245              self.assertFalse(True, "adding a filter valued None worked")
246
247         try:
248              self.assertFalse(category.AddTypeSynthetic(lldb.SBTypeNameSpecifier("NoneSuchType"), None), "adding a synthetic valued None worked")
249         except:
250              pass
251         else:
252              self.assertFalse(True, "adding a synthetic valued None worked")
253
254         try:
255              self.assertFalse(category.AddTypeFormat(lldb.SBTypeNameSpecifier("NoneSuchType"), None), "adding a format valued None worked")
256         except:
257              pass
258         else:
259              self.assertFalse(True, "adding a format valued None worked")
260
261
262         self.assertFalse(category.AddTypeSummary(lldb.SBTypeNameSpecifier("EmptySuchType"), lldb.SBTypeSummary()), "adding a summary without value worked")
263         self.assertFalse(category.AddTypeFilter(lldb.SBTypeNameSpecifier("EmptySuchType"), lldb.SBTypeFilter()), "adding a filter without value worked")
264         self.assertFalse(category.AddTypeSynthetic(lldb.SBTypeNameSpecifier("EmptySuchType"), lldb.SBTypeSynthetic()), "adding a synthetic without value worked")
265         self.assertFalse(category.AddTypeFormat(lldb.SBTypeNameSpecifier("EmptySuchType"), lldb.SBTypeFormat()), "adding a format without value worked")
266
267         self.assertFalse(category.AddTypeSummary(lldb.SBTypeNameSpecifier(""), lldb.SBTypeSummary.CreateWithSummaryString("")), "adding a summary for an invalid type worked")
268         self.assertFalse(category.AddTypeFilter(lldb.SBTypeNameSpecifier(""), lldb.SBTypeFilter(0)), "adding a filter for an invalid type worked")
269         self.assertFalse(category.AddTypeSynthetic(lldb.SBTypeNameSpecifier(""), lldb.SBTypeSynthetic.CreateWithClassName("")), "adding a synthetic for an invalid type worked")
270         self.assertFalse(category.AddTypeFormat(lldb.SBTypeNameSpecifier(""), lldb.SBTypeFormat(lldb.eFormatHex)), "adding a format for an invalid type worked")
271
272         new_category = self.dbg.CreateCategory("newbar")
273         new_category.AddTypeSummary(lldb.SBTypeNameSpecifier("JustAStruct"),
274              lldb.SBTypeSummary.CreateWithScriptCode("return 'hello scripted world';"))
275         self.expect("frame variable foo", matching=False,
276              substrs = ['hello scripted world'])
277         new_category.enabled = True
278         self.expect("frame variable foo", matching=True,
279              substrs = ['hello scripted world'])
280
281         self.expect("frame variable foo_ptr", matching=True,
282              substrs = ['hello scripted world'])
283         new_category.AddTypeSummary(lldb.SBTypeNameSpecifier("JustAStruct"),
284              lldb.SBTypeSummary.CreateWithScriptCode("return 'hello scripted world';",
285              lldb.eTypeOptionSkipPointers))
286         self.expect("frame variable foo", matching=True,
287              substrs = ['hello scripted world'])
288
289         frame = self.dbg.GetSelectedTarget().GetProcess().GetSelectedThread().GetSelectedFrame()
290         foo_ptr = frame.FindVariable("foo_ptr")
291         summary = foo_ptr.GetTypeSummary()
292
293         self.assertFalse(summary.IsValid(), "summary found for foo* when none was planned")
294
295         self.expect("frame variable foo_ptr", matching=False,
296              substrs = ['hello scripted world'])
297
298         new_category.AddTypeSummary(lldb.SBTypeNameSpecifier("JustAStruct"),
299              lldb.SBTypeSummary.CreateWithSummaryString("hello static world",
300              lldb.eTypeOptionNone))
301
302         summary = foo_ptr.GetTypeSummary()
303
304         self.assertTrue(summary.IsValid(), "no summary found for foo* when one was in place")
305         self.assertTrue(summary.GetData() == "hello static world", "wrong summary found for foo*")
306
307         self.expect("frame variable e1", substrs=["I am an empty Empty1 {}"])
308         self.expect("frame variable e2", substrs=["I am an empty Empty2"])
309         self.expect("frame variable e2", substrs=["I am an empty Empty2 {}"], matching=False)
310
311     def force_synth_off(self):
312         """Test that one can have the public API return non-synthetic SBValues if desired"""
313         self.runCmd("file no_synth", CURRENT_EXECUTABLE_SET)
314
315         lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True)
316
317         self.runCmd("run", RUN_SUCCEEDED)
318
319         # The stop reason of the thread should be breakpoint.
320         self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
321             substrs = ['stopped',
322                        'stop reason = breakpoint'])
323
324         # This is the function to remove the custom formats in order to have a
325         # clean slate for the next test case.
326         def cleanup():
327             self.runCmd('type format clear', check=False)
328             self.runCmd('type summary clear', check=False)
329             self.runCmd('type filter clear', check=False)
330             self.runCmd('type synthetic clear', check=False)
331             self.runCmd('type category delete foobar', check=False)
332             self.runCmd('type category delete JASSynth', check=False)
333             self.runCmd('type category delete newbar', check=False)
334             self.runCmd('settings set target.enable-synthetic-value true')
335
336         # Execute the cleanup function during test case tear down.
337         self.addTearDownHook(cleanup)
338
339         frame = self.dbg.GetSelectedTarget().GetProcess().GetSelectedThread().GetSelectedFrame()
340         int_vector = frame.FindVariable("int_vector")
341         if self.TraceOn():
342              print int_vector
343         self.assertTrue(int_vector.GetNumChildren() == 0, 'synthetic vector is empty')
344
345         self.runCmd('settings set target.enable-synthetic-value false')
346         frame = self.dbg.GetSelectedTarget().GetProcess().GetSelectedThread().GetSelectedFrame()
347         int_vector = frame.FindVariable("int_vector")
348         if self.TraceOn():
349              print int_vector
350         self.assertFalse(int_vector.GetNumChildren() == 0, '"physical" vector is not empty')
351
352         self.runCmd('settings set target.enable-synthetic-value true')
353         frame = self.dbg.GetSelectedTarget().GetProcess().GetSelectedThread().GetSelectedFrame()
354         int_vector = frame.FindVariable("int_vector")
355         if self.TraceOn():
356              print int_vector
357         self.assertTrue(int_vector.GetNumChildren() == 0, 'synthetic vector is still empty')
358
359
360 if __name__ == '__main__':
361     import atexit
362     lldb.SBDebugger.Initialize()
363     atexit.register(lambda: lldb.SBDebugger.Terminate())
364     unittest2.main()