1 """Test Python APIs for working with formatters"""
3 from __future__
import print_function
7 from lldbsuite
.test
.decorators
import *
8 from lldbsuite
.test
.lldbtest
import *
9 from lldbsuite
.test
import lldbutil
12 class SBFormattersAPITestCase(TestBase
):
14 mydir
= TestBase
.compute_mydir(__file__
)
15 NO_DEBUG_INFO_TESTCASE
= True
18 # Call super's setUp().
20 self
.line
= line_number('main.cpp', '// Set break point at this line.')
22 @add_test_categories(['pyapi'])
23 def test_formatters_api(self
):
24 """Test Python APIs for working with formatters"""
26 self
.setTearDownCleanup()
28 """Test Python APIs for working with formatters"""
29 self
.runCmd("file " + self
.getBuildArtifact("a.out"),
30 CURRENT_EXECUTABLE_SET
)
32 lldbutil
.run_break_set_by_file_and_line(
33 self
, "main.cpp", self
.line
, num_expected_locations
=1,
36 self
.runCmd("run", RUN_SUCCEEDED
)
38 # The stop reason of the thread should be breakpoint.
39 self
.expect("thread list", STOPPED_DUE_TO_BREAKPOINT
,
41 'stop reason = breakpoint'])
43 # This is the function to remove the custom formats in order to have a
44 # clean slate for the next test case.
46 self
.runCmd('type format clear', check
=False)
47 self
.runCmd('type summary clear', check
=False)
48 self
.runCmd('type filter clear', check
=False)
49 self
.runCmd('type synthetic clear', check
=False)
50 self
.runCmd('type category delete foobar', check
=False)
51 self
.runCmd('type category delete JASSynth', check
=False)
52 self
.runCmd('type category delete newbar', check
=False)
54 # Execute the cleanup function during test case tear down.
55 self
.addTearDownHook(cleanup
)
57 format
= lldb
.SBTypeFormat(lldb
.eFormatHex
)
58 category
= self
.dbg
.GetDefaultCategory()
59 category
.AddTypeFormat(lldb
.SBTypeNameSpecifier("int"), format
)
61 self
.expect("frame variable foo.A",
62 substrs
=['0x00000001'])
63 self
.expect("frame variable foo.E", matching
=False,
66 category
.AddTypeFormat(lldb
.SBTypeNameSpecifier("long"), format
)
67 self
.expect("frame variable foo.A",
68 substrs
=['0x00000001'])
69 self
.expect("frame variable foo.E",
72 format
.SetFormat(lldb
.eFormatOctal
)
73 category
.AddTypeFormat(lldb
.SBTypeNameSpecifier("int"), format
)
74 self
.expect("frame variable foo.A",
76 self
.expect("frame variable foo.E",
79 category
.DeleteTypeFormat(lldb
.SBTypeNameSpecifier("int"))
80 category
.DeleteTypeFormat(lldb
.SBTypeNameSpecifier("long"))
81 self
.expect("frame variable foo.A", matching
=False,
83 self
.expect("frame variable foo.E", matching
=False,
86 summary
= lldb
.SBTypeSummary
.CreateWithSummaryString(
87 "the hello world you'll never see")
88 summary
.SetSummaryString('hello world')
89 new_category
= self
.dbg
.GetCategory("foobar")
91 new_category
.IsValid(),
92 "getting a non-existing category worked")
93 new_category
= self
.dbg
.CreateCategory("foobar")
94 new_category
.SetEnabled(True)
95 new_category
.AddTypeSummary(
96 lldb
.SBTypeNameSpecifier(
101 self
.expect("frame variable foo.A",
102 substrs
=['hello world'])
103 self
.expect("frame variable foo.E", matching
=False,
104 substrs
=['hello world'])
105 self
.expect("frame variable foo.B",
106 substrs
=['hello world'])
107 self
.expect("frame variable foo.F",
108 substrs
=['hello world'])
109 new_category
.SetEnabled(False)
110 self
.expect("frame variable foo.A", matching
=False,
111 substrs
=['hello world'])
112 self
.expect("frame variable foo.E", matching
=False,
113 substrs
=['hello world'])
114 self
.expect("frame variable foo.B", matching
=False,
115 substrs
=['hello world'])
116 self
.expect("frame variable foo.F", matching
=False,
117 substrs
=['hello world'])
118 self
.dbg
.DeleteCategory(new_category
.GetName())
119 self
.expect("frame variable foo.A", matching
=False,
120 substrs
=['hello world'])
121 self
.expect("frame variable foo.E", matching
=False,
122 substrs
=['hello world'])
123 self
.expect("frame variable foo.B", matching
=False,
124 substrs
=['hello world'])
125 self
.expect("frame variable foo.F", matching
=False,
126 substrs
=['hello world'])
128 filter = lldb
.SBTypeFilter(0)
129 filter.AppendExpressionPath("A")
130 filter.AppendExpressionPath("D")
132 filter.GetNumberOfExpressionPaths() == 2,
133 "filter with two items does not have two items")
135 category
.AddTypeFilter(lldb
.SBTypeNameSpecifier("JustAStruct"), filter)
136 self
.expect("frame variable foo",
137 substrs
=['A = 1', 'D = 6.28'])
138 self
.expect("frame variable foo", matching
=False,
139 substrs
=['B = ', 'C = ', 'E = ', 'F = '])
141 category
.DeleteTypeFilter(
142 lldb
.SBTypeNameSpecifier(
143 "JustAStruct", True))
144 self
.expect("frame variable foo",
145 substrs
=['A = 1', 'D = 6.28'])
146 self
.expect("frame variable foo", matching
=False,
147 substrs
=['B = ', 'C = ', 'E = ', 'F = '])
149 category
.DeleteTypeFilter(
150 lldb
.SBTypeNameSpecifier(
151 "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 = '])
157 self
.runCmd("command script import --allow-reload ./synth.py")
159 self
.expect("frame variable foo", matching
=False,
162 self
.dbg
.GetCategory("JASSynth").SetEnabled(True)
163 self
.expect("frame variable foo", matching
=True,
166 self
.dbg
.GetCategory("CCCSynth").SetEnabled(True)
168 "frame variable ccc",
171 'CCC object with leading value (int) a = 111',
176 foo_var
= self
.dbg
.GetSelectedTarget().GetProcess(
177 ).GetSelectedThread().GetSelectedFrame().FindVariable('foo')
178 self
.assertTrue(foo_var
.IsValid(), 'could not find foo')
180 foo_var
.GetDeclaration().IsValid(),
181 'foo declaration is invalid')
184 foo_var
.GetNumChildren() == 2,
185 'synthetic value has wrong number of child items (synth)')
187 foo_var
.GetChildMemberWithName('X').GetValueAsUnsigned() == 1,
188 'foo_synth.X has wrong value (synth)')
190 foo_var
.GetChildMemberWithName('B').IsValid(),
191 'foo_synth.B is valid but should not (synth)')
193 self
.dbg
.GetCategory("JASSynth").SetEnabled(False)
194 foo_var
= self
.dbg
.GetSelectedTarget().GetProcess(
195 ).GetSelectedThread().GetSelectedFrame().FindVariable('foo')
196 self
.assertTrue(foo_var
.IsValid(), 'could not find foo')
199 foo_var
.GetNumChildren() == 2,
200 'still seeing synthetic value')
202 filter = lldb
.SBTypeFilter(0)
203 filter.AppendExpressionPath("A")
204 filter.AppendExpressionPath("D")
205 category
.AddTypeFilter(lldb
.SBTypeNameSpecifier("JustAStruct"), filter)
206 self
.expect("frame variable foo",
207 substrs
=['A = 1', 'D = 6.28'])
209 foo_var
= self
.dbg
.GetSelectedTarget().GetProcess(
210 ).GetSelectedThread().GetSelectedFrame().FindVariable('foo')
211 self
.assertTrue(foo_var
.IsValid(), 'could not find foo')
214 foo_var
.GetNumChildren() == 2,
215 'synthetic value has wrong number of child items (filter)')
217 foo_var
.GetChildMemberWithName('X').GetValueAsUnsigned() == 0,
218 'foo_synth.X has wrong value (filter)')
220 foo_var
.GetChildMemberWithName('A').GetValueAsUnsigned() == 1,
221 'foo_synth.A has wrong value (filter)')
223 self
.assertTrue(filter.ReplaceExpressionPathAtIndex(
224 0, "C"), "failed to replace an expression path in filter")
225 self
.expect("frame variable foo",
226 substrs
=['A = 1', 'D = 6.28'])
227 category
.AddTypeFilter(lldb
.SBTypeNameSpecifier("JustAStruct"), filter)
228 self
.expect("frame variable foo",
229 substrs
=["C = 'e'", 'D = 6.28'])
230 category
.AddTypeFilter(lldb
.SBTypeNameSpecifier("FooType"), filter)
231 filter.ReplaceExpressionPathAtIndex(1, "F")
232 self
.expect("frame variable foo",
233 substrs
=["C = 'e'", 'D = 6.28'])
234 category
.AddTypeFilter(lldb
.SBTypeNameSpecifier("JustAStruct"), filter)
235 self
.expect("frame variable foo",
236 substrs
=["C = 'e'", 'F = 0'])
237 self
.expect("frame variable bar",
238 substrs
=["C = 'e'", 'D = 6.28'])
240 foo_var
= self
.dbg
.GetSelectedTarget().GetProcess(
241 ).GetSelectedThread().GetSelectedFrame().FindVariable('foo')
242 self
.assertTrue(foo_var
.IsValid(), 'could not find foo')
244 foo_var
.GetChildMemberWithName('C').GetValueAsUnsigned() == ord('e'),
245 'foo_synth.C has wrong value (filter)')
247 chosen
= self
.dbg
.GetFilterForType(
248 lldb
.SBTypeNameSpecifier("JustAStruct"))
251 "wrong filter found for JustAStruct")
253 chosen
.GetExpressionPathAtIndex(0) == 'C',
254 "wrong item at index 0 for JustAStruct")
256 chosen
.GetExpressionPathAtIndex(1) == 'F',
257 "wrong item at index 1 for JustAStruct")
260 category
.DeleteTypeFilter(
261 lldb
.SBTypeNameSpecifier("NoSuchType")),
262 "deleting a non-existing filter worked")
264 category
.DeleteTypeSummary(
265 lldb
.SBTypeNameSpecifier("NoSuchType")),
266 "deleting a non-existing summary worked")
268 category
.DeleteTypeFormat(
269 lldb
.SBTypeNameSpecifier("NoSuchType")),
270 "deleting a non-existing format worked")
272 category
.DeleteTypeSynthetic(
273 lldb
.SBTypeNameSpecifier("NoSuchType")),
274 "deleting a non-existing synthetic worked")
277 category
.DeleteTypeFilter(
278 lldb
.SBTypeNameSpecifier("")),
279 "deleting a filter for '' worked")
281 category
.DeleteTypeSummary(
282 lldb
.SBTypeNameSpecifier("")),
283 "deleting a summary for '' worked")
285 category
.DeleteTypeFormat(
286 lldb
.SBTypeNameSpecifier("")),
287 "deleting a format for '' worked")
289 category
.DeleteTypeSynthetic(
290 lldb
.SBTypeNameSpecifier("")),
291 "deleting a synthetic for '' worked")
295 category
.AddTypeSummary(
296 lldb
.SBTypeNameSpecifier("NoneSuchType"),
298 "adding a summary valued None worked")
302 self
.assertFalse(True, "adding a summary valued None worked")
306 category
.AddTypeFilter(
307 lldb
.SBTypeNameSpecifier("NoneSuchType"),
309 "adding a filter valued None worked")
313 self
.assertFalse(True, "adding a filter valued None worked")
317 category
.AddTypeSynthetic(
318 lldb
.SBTypeNameSpecifier("NoneSuchType"),
320 "adding a synthetic valued None worked")
324 self
.assertFalse(True, "adding a synthetic valued None worked")
328 category
.AddTypeFormat(
329 lldb
.SBTypeNameSpecifier("NoneSuchType"),
331 "adding a format valued None worked")
335 self
.assertFalse(True, "adding a format valued None worked")
338 category
.AddTypeSummary(
339 lldb
.SBTypeNameSpecifier("EmptySuchType"),
340 lldb
.SBTypeSummary()),
341 "adding a summary without value worked")
343 category
.AddTypeFilter(
344 lldb
.SBTypeNameSpecifier("EmptySuchType"),
345 lldb
.SBTypeFilter()),
346 "adding a filter without value worked")
348 category
.AddTypeSynthetic(
349 lldb
.SBTypeNameSpecifier("EmptySuchType"),
350 lldb
.SBTypeSynthetic()),
351 "adding a synthetic without value worked")
353 category
.AddTypeFormat(
354 lldb
.SBTypeNameSpecifier("EmptySuchType"),
355 lldb
.SBTypeFormat()),
356 "adding a format without value worked")
359 category
.AddTypeSummary(
360 lldb
.SBTypeNameSpecifier(""),
361 lldb
.SBTypeSummary
.CreateWithSummaryString("")),
362 "adding a summary for an invalid type worked")
364 category
.AddTypeFilter(
365 lldb
.SBTypeNameSpecifier(""),
366 lldb
.SBTypeFilter(0)),
367 "adding a filter for an invalid type worked")
369 category
.AddTypeSynthetic(
370 lldb
.SBTypeNameSpecifier(""),
371 lldb
.SBTypeSynthetic
.CreateWithClassName("")),
372 "adding a synthetic for an invalid type worked")
374 category
.AddTypeFormat(
375 lldb
.SBTypeNameSpecifier(""),
378 "adding a format for an invalid type worked")
380 new_category
= self
.dbg
.CreateCategory("newbar")
381 new_category
.AddTypeSummary(
382 lldb
.SBTypeNameSpecifier("JustAStruct"),
383 lldb
.SBTypeSummary
.CreateWithScriptCode("return 'hello scripted world';"))
384 self
.expect("frame variable foo", matching
=False,
385 substrs
=['hello scripted world'])
386 new_category
.SetEnabled(True)
387 self
.expect("frame variable foo", matching
=True,
388 substrs
=['hello scripted world'])
390 self
.expect("frame variable foo_ptr", matching
=True,
391 substrs
=['hello scripted world'])
392 new_category
.AddTypeSummary(
393 lldb
.SBTypeNameSpecifier("JustAStruct"),
394 lldb
.SBTypeSummary
.CreateWithScriptCode(
395 "return 'hello scripted world';",
396 lldb
.eTypeOptionSkipPointers
))
397 self
.expect("frame variable foo", matching
=True,
398 substrs
=['hello scripted world'])
400 frame
= self
.dbg
.GetSelectedTarget().GetProcess(
401 ).GetSelectedThread().GetSelectedFrame()
402 foo_ptr
= frame
.FindVariable("foo_ptr")
403 summary
= foo_ptr
.GetTypeSummary()
407 "summary found for foo* when none was planned")
409 self
.expect("frame variable foo_ptr", matching
=False,
410 substrs
=['hello scripted world'])
412 new_category
.AddTypeSummary(
413 lldb
.SBTypeNameSpecifier("JustAStruct"),
414 lldb
.SBTypeSummary
.CreateWithSummaryString(
415 "hello static world",
416 lldb
.eTypeOptionNone
))
418 summary
= foo_ptr
.GetTypeSummary()
422 "no summary found for foo* when one was in place")
424 summary
.GetData() == "hello static world",
425 "wrong summary found for foo*")
427 self
.expect("frame variable e1", substrs
=["I am an empty Empty1 {}"])
428 self
.expect("frame variable e2", substrs
=["I am an empty Empty2"])
431 substrs
=["I am an empty Empty2 {}"],
435 self
.dbg
.GetCategory(
436 lldb
.eLanguageTypeObjC
) is not None,
437 "ObjC category is None")
439 @add_test_categories(['pyapi'])
440 def test_force_synth_off(self
):
441 """Test that one can have the public API return non-synthetic SBValues if desired"""
442 self
.build(dictionary
={'EXE': 'no_synth'})
443 self
.setTearDownCleanup()
445 self
.runCmd("file " + self
.getBuildArtifact("no_synth"),
446 CURRENT_EXECUTABLE_SET
)
448 lldbutil
.run_break_set_by_file_and_line(
449 self
, "main.cpp", self
.line
, num_expected_locations
=1, loc_exact
=True)
451 self
.runCmd("run", RUN_SUCCEEDED
)
453 # The stop reason of the thread should be breakpoint.
454 self
.expect("thread list", STOPPED_DUE_TO_BREAKPOINT
,
456 'stop reason = breakpoint'])
458 # This is the function to remove the custom formats in order to have a
459 # clean slate for the next test case.
461 self
.runCmd('type format clear', check
=False)
462 self
.runCmd('type summary clear', check
=False)
463 self
.runCmd('type filter clear', check
=False)
464 self
.runCmd('type synthetic clear', check
=False)
465 self
.runCmd('type category delete foobar', check
=False)
466 self
.runCmd('type category delete JASSynth', check
=False)
467 self
.runCmd('type category delete newbar', check
=False)
468 self
.runCmd('settings set target.enable-synthetic-value true')
470 # Execute the cleanup function during test case tear down.
471 self
.addTearDownHook(cleanup
)
473 frame
= self
.dbg
.GetSelectedTarget().GetProcess(
474 ).GetSelectedThread().GetSelectedFrame()
475 int_vector
= frame
.FindVariable("int_vector")
479 int_vector
.GetNumChildren() == 0,
480 'synthetic vector is empty')
482 self
.runCmd('settings set target.enable-synthetic-value false')
483 frame
= self
.dbg
.GetSelectedTarget().GetProcess(
484 ).GetSelectedThread().GetSelectedFrame()
485 int_vector
= frame
.FindVariable("int_vector")
489 int_vector
.GetNumChildren() == 0,
490 '"physical" vector is not empty')
492 self
.runCmd('settings set target.enable-synthetic-value true')
493 frame
= self
.dbg
.GetSelectedTarget().GetProcess(
494 ).GetSelectedThread().GetSelectedFrame()
495 int_vector
= frame
.FindVariable("int_vector")
499 int_vector
.GetNumChildren() == 0,
500 'synthetic vector is still empty')