1 """Test Python APIs for working with formatters"""
4 from lldbsuite
.test
.decorators
import *
5 from lldbsuite
.test
.lldbtest
import *
6 from lldbsuite
.test
import lldbutil
9 class SBFormattersAPITestCase(TestBase
):
10 NO_DEBUG_INFO_TESTCASE
= True
13 # Call super's setUp().
15 self
.line
= line_number("main.cpp", "// Set break point at this line.")
17 def test_formatters_api(self
):
18 """Test Python APIs for working with formatters"""
20 self
.setTearDownCleanup()
22 """Test Python APIs for working with formatters"""
23 self
.runCmd("file " + self
.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET
)
25 lldbutil
.run_break_set_by_file_and_line(
26 self
, "main.cpp", self
.line
, num_expected_locations
=1, loc_exact
=True
29 self
.runCmd("run", RUN_SUCCEEDED
)
31 # The stop reason of the thread should be breakpoint.
34 STOPPED_DUE_TO_BREAKPOINT
,
35 substrs
=["stopped", "stop reason = breakpoint"],
38 # This is the function to remove the custom formats in order to have a
39 # clean slate for the next test case.
41 self
.runCmd("type format clear", check
=False)
42 self
.runCmd("type summary clear", check
=False)
43 self
.runCmd("type filter clear", check
=False)
44 self
.runCmd("type synthetic clear", check
=False)
45 self
.runCmd("type category delete foobar", check
=False)
46 self
.runCmd("type category delete JASSynth", check
=False)
47 self
.runCmd("type category delete newbar", check
=False)
49 # Execute the cleanup function during test case tear down.
50 self
.addTearDownHook(cleanup
)
52 format
= lldb
.SBTypeFormat(lldb
.eFormatHex
)
53 category
= self
.dbg
.GetDefaultCategory()
54 category
.AddTypeFormat(lldb
.SBTypeNameSpecifier("int"), format
)
56 self
.expect("frame variable foo.A", substrs
=["0x00000001"])
57 self
.expect("frame variable foo.E", matching
=False, substrs
=["b8cca70a"])
59 category
.AddTypeFormat(lldb
.SBTypeNameSpecifier("long"), format
)
60 self
.expect("frame variable foo.A", substrs
=["0x00000001"])
61 self
.expect("frame variable foo.E", substrs
=["b8cca70a"])
63 format
.SetFormat(lldb
.eFormatOctal
)
64 category
.AddTypeFormat(lldb
.SBTypeNameSpecifier("int"), format
)
65 self
.expect("frame variable foo.A", substrs
=[" 01"])
66 self
.expect("frame variable foo.E", substrs
=["b8cca70a"])
68 category
.DeleteTypeFormat(lldb
.SBTypeNameSpecifier("int"))
69 category
.DeleteTypeFormat(lldb
.SBTypeNameSpecifier("long"))
70 self
.expect("frame variable foo.A", matching
=False, substrs
=[" 01"])
71 self
.expect("frame variable foo.E", matching
=False, substrs
=["b8cca70a"])
73 summary
= lldb
.SBTypeSummary
.CreateWithSummaryString(
74 "the hello world you'll never see"
76 summary
.SetSummaryString("hello world")
77 new_category
= self
.dbg
.GetCategory("foobar")
79 new_category
.IsValid(), "getting a non-existing category worked"
81 new_category
= self
.dbg
.CreateCategory("foobar")
82 new_category
.SetEnabled(True)
83 new_category
.AddTypeSummary(
84 lldb
.SBTypeNameSpecifier(
91 self
.expect("frame variable foo.A", substrs
=["hello world"])
92 self
.expect("frame variable foo.E", matching
=False, substrs
=["hello world"])
93 self
.expect("frame variable foo.B", substrs
=["hello world"])
94 self
.expect("frame variable foo.F", substrs
=["hello world"])
95 new_category
.SetEnabled(False)
96 self
.expect("frame variable foo.A", matching
=False, substrs
=["hello world"])
97 self
.expect("frame variable foo.E", matching
=False, substrs
=["hello world"])
98 self
.expect("frame variable foo.B", matching
=False, substrs
=["hello world"])
99 self
.expect("frame variable foo.F", matching
=False, substrs
=["hello world"])
100 self
.dbg
.DeleteCategory(new_category
.GetName())
101 self
.expect("frame variable foo.A", matching
=False, substrs
=["hello world"])
102 self
.expect("frame variable foo.E", matching
=False, substrs
=["hello world"])
103 self
.expect("frame variable foo.B", matching
=False, substrs
=["hello world"])
104 self
.expect("frame variable foo.F", matching
=False, substrs
=["hello world"])
106 filter = lldb
.SBTypeFilter(0)
107 filter.AppendExpressionPath("A")
108 filter.AppendExpressionPath("D")
110 filter.GetNumberOfExpressionPaths(),
112 "filter with two items does not have two items",
115 category
.AddTypeFilter(lldb
.SBTypeNameSpecifier("JustAStruct"), filter)
116 self
.expect("frame variable foo", substrs
=["A = 1", "D = 6.28"])
118 "frame variable foo",
120 substrs
=["B = ", "C = ", "E = ", "F = "],
123 category
.DeleteTypeFilter(lldb
.SBTypeNameSpecifier("JustAStruct", True))
124 self
.expect("frame variable foo", substrs
=["A = 1", "D = 6.28"])
126 "frame variable foo",
128 substrs
=["B = ", "C = ", "E = ", "F = "],
131 category
.DeleteTypeFilter(lldb
.SBTypeNameSpecifier("JustAStruct", False))
132 self
.expect("frame variable foo", substrs
=["A = 1", "D = 6.28"])
134 "frame variable foo",
136 substrs
=["B = ", "C = ", "E = ", "F = "],
139 self
.runCmd("command script import --allow-reload ./synth.py")
141 self
.expect("frame variable foo", matching
=False, substrs
=["X = 1"])
143 self
.dbg
.GetCategory("JASSynth").SetEnabled(True)
144 self
.expect("frame variable foo", matching
=True, substrs
=["X = 1"])
146 self
.dbg
.GetCategory("CCCSynth").SetEnabled(True)
148 "frame variable ccc",
151 "CCC object with leading value (int) a = 111",
159 self
.dbg
.GetSelectedTarget()
165 self
.assertTrue(foo_var
.IsValid(), "could not find foo")
167 foo_var
.GetDeclaration().IsValid(), "foo declaration is invalid"
171 foo_var
.GetNumChildren(),
173 "synthetic value has wrong number of child items (synth)",
176 foo_var
.GetChildMemberWithName("X").GetValueAsUnsigned(),
178 "foo_synth.X has wrong value (synth)",
181 foo_var
.GetChildMemberWithName("B").IsValid(),
182 "foo_synth.B is valid but should not (synth)",
185 self
.dbg
.GetCategory("JASSynth").SetEnabled(False)
187 self
.dbg
.GetSelectedTarget()
193 self
.assertTrue(foo_var
.IsValid(), "could not find foo")
195 self
.assertFalse(foo_var
.GetNumChildren() == 2, "still seeing synthetic value")
197 filter = lldb
.SBTypeFilter(0)
198 filter.AppendExpressionPath("A")
199 filter.AppendExpressionPath("D")
200 category
.AddTypeFilter(lldb
.SBTypeNameSpecifier("JustAStruct"), filter)
201 self
.expect("frame variable foo", substrs
=["A = 1", "D = 6.28"])
204 self
.dbg
.GetSelectedTarget()
210 self
.assertTrue(foo_var
.IsValid(), "could not find foo")
213 foo_var
.GetNumChildren(),
215 "synthetic value has wrong number of child items (filter)",
218 foo_var
.GetChildMemberWithName("X").GetValueAsUnsigned(),
220 "foo_synth.X has wrong value (filter)",
223 foo_var
.GetChildMemberWithName("A").GetValueAsUnsigned(),
225 "foo_synth.A has wrong value (filter)",
229 filter.ReplaceExpressionPathAtIndex(0, "C"),
230 "failed to replace an expression path in filter",
232 self
.expect("frame variable foo", substrs
=["A = 1", "D = 6.28"])
233 category
.AddTypeFilter(lldb
.SBTypeNameSpecifier("JustAStruct"), filter)
234 self
.expect("frame variable foo", substrs
=["C = 'e'", "D = 6.28"])
235 category
.AddTypeFilter(lldb
.SBTypeNameSpecifier("FooType"), filter)
236 filter.ReplaceExpressionPathAtIndex(1, "F")
237 self
.expect("frame variable foo", substrs
=["C = 'e'", "D = 6.28"])
238 category
.AddTypeFilter(lldb
.SBTypeNameSpecifier("JustAStruct"), filter)
239 self
.expect("frame variable foo", substrs
=["C = 'e'", "F = 0"])
240 self
.expect("frame variable bar", substrs
=["C = 'e'", "D = 6.28"])
243 self
.dbg
.GetSelectedTarget()
249 self
.assertTrue(foo_var
.IsValid(), "could not find foo")
251 foo_var
.GetChildMemberWithName("C").GetValueAsUnsigned(),
253 "foo_synth.C has wrong value (filter)",
256 chosen
= self
.dbg
.GetFilterForType(lldb
.SBTypeNameSpecifier("JustAStruct"))
257 self
.assertEqual(chosen
.count
, 2, "wrong filter found for JustAStruct")
259 chosen
.GetExpressionPathAtIndex(0),
261 "wrong item at index 0 for JustAStruct",
264 chosen
.GetExpressionPathAtIndex(1),
266 "wrong item at index 1 for JustAStruct",
270 category
.DeleteTypeFilter(lldb
.SBTypeNameSpecifier("NoSuchType")),
271 "deleting a non-existing filter worked",
274 category
.DeleteTypeSummary(lldb
.SBTypeNameSpecifier("NoSuchType")),
275 "deleting a non-existing summary worked",
278 category
.DeleteTypeFormat(lldb
.SBTypeNameSpecifier("NoSuchType")),
279 "deleting a non-existing format worked",
282 category
.DeleteTypeSynthetic(lldb
.SBTypeNameSpecifier("NoSuchType")),
283 "deleting a non-existing synthetic worked",
287 category
.DeleteTypeFilter(lldb
.SBTypeNameSpecifier("")),
288 "deleting a filter for '' worked",
291 category
.DeleteTypeSummary(lldb
.SBTypeNameSpecifier("")),
292 "deleting a summary for '' worked",
295 category
.DeleteTypeFormat(lldb
.SBTypeNameSpecifier("")),
296 "deleting a format for '' worked",
299 category
.DeleteTypeSynthetic(lldb
.SBTypeNameSpecifier("")),
300 "deleting a synthetic for '' worked",
305 category
.AddTypeSummary(lldb
.SBTypeNameSpecifier("NoneSuchType"), None),
306 "adding a summary valued None worked",
311 self
.assertFalse(True, "adding a summary valued None worked")
315 category
.AddTypeFilter(lldb
.SBTypeNameSpecifier("NoneSuchType"), None),
316 "adding a filter valued None worked",
321 self
.assertFalse(True, "adding a filter valued None worked")
325 category
.AddTypeSynthetic(
326 lldb
.SBTypeNameSpecifier("NoneSuchType"), None
328 "adding a synthetic valued None worked",
333 self
.assertFalse(True, "adding a synthetic valued None worked")
337 category
.AddTypeFormat(lldb
.SBTypeNameSpecifier("NoneSuchType"), None),
338 "adding a format valued None worked",
343 self
.assertFalse(True, "adding a format valued None worked")
346 category
.AddTypeSummary(
347 lldb
.SBTypeNameSpecifier("EmptySuchType"), lldb
.SBTypeSummary()
349 "adding a summary without value worked",
352 category
.AddTypeFilter(
353 lldb
.SBTypeNameSpecifier("EmptySuchType"), lldb
.SBTypeFilter()
355 "adding a filter without value worked",
358 category
.AddTypeSynthetic(
359 lldb
.SBTypeNameSpecifier("EmptySuchType"), lldb
.SBTypeSynthetic()
361 "adding a synthetic without value worked",
364 category
.AddTypeFormat(
365 lldb
.SBTypeNameSpecifier("EmptySuchType"), lldb
.SBTypeFormat()
367 "adding a format without value worked",
371 category
.AddTypeSummary(
372 lldb
.SBTypeNameSpecifier(""),
373 lldb
.SBTypeSummary
.CreateWithSummaryString(""),
375 "adding a summary for an invalid type worked",
378 category
.AddTypeFilter(lldb
.SBTypeNameSpecifier(""), lldb
.SBTypeFilter(0)),
379 "adding a filter for an invalid type worked",
382 category
.AddTypeSynthetic(
383 lldb
.SBTypeNameSpecifier(""),
384 lldb
.SBTypeSynthetic
.CreateWithClassName(""),
386 "adding a synthetic for an invalid type worked",
389 category
.AddTypeFormat(
390 lldb
.SBTypeNameSpecifier(""), lldb
.SBTypeFormat(lldb
.eFormatHex
)
392 "adding a format for an invalid type worked",
395 new_category
= self
.dbg
.CreateCategory("newbar")
396 new_category
.AddTypeSummary(
397 lldb
.SBTypeNameSpecifier("JustAStruct"),
398 lldb
.SBTypeSummary
.CreateWithScriptCode("return 'hello scripted world';"),
401 "frame variable foo", matching
=False, substrs
=["hello scripted world"]
403 new_category
.SetEnabled(True)
405 "frame variable foo", matching
=True, substrs
=["hello scripted world"]
409 "frame variable foo_ptr", matching
=True, substrs
=["hello scripted world"]
411 new_category
.AddTypeSummary(
412 lldb
.SBTypeNameSpecifier("JustAStruct"),
413 lldb
.SBTypeSummary
.CreateWithScriptCode(
414 "return 'hello scripted world';", lldb
.eTypeOptionSkipPointers
418 "frame variable foo", matching
=True, substrs
=["hello scripted world"]
422 self
.dbg
.GetSelectedTarget()
427 foo_ptr
= frame
.FindVariable("foo_ptr")
428 summary
= foo_ptr
.GetTypeSummary()
431 summary
.IsValid(), "summary found for foo* when none was planned"
435 "frame variable foo_ptr", matching
=False, substrs
=["hello scripted world"]
438 new_category
.AddTypeSummary(
439 lldb
.SBTypeNameSpecifier("JustAStruct"),
440 lldb
.SBTypeSummary
.CreateWithSummaryString(
441 "hello static world", lldb
.eTypeOptionNone
445 summary
= foo_ptr
.GetTypeSummary()
448 summary
.IsValid(), "no summary found for foo* when one was in place"
451 summary
.GetData(), "hello static world", "wrong summary found for foo*"
454 self
.expect("frame variable e1", substrs
=["I am an empty Empty1 {}"])
455 self
.expect("frame variable e2", substrs
=["I am an empty Empty2"])
457 "frame variable e2", substrs
=["I am an empty Empty2 {}"], matching
=False
461 self
.dbg
.GetCategory(lldb
.eLanguageTypeObjC
) is not None,
462 "ObjC category is None",
465 def test_force_synth_off(self
):
466 """Test that one can have the public API return non-synthetic SBValues if desired"""
467 self
.build(dictionary
={"EXE": "no_synth"})
468 self
.setTearDownCleanup()
470 self
.runCmd("file " + self
.getBuildArtifact("no_synth"), CURRENT_EXECUTABLE_SET
)
472 lldbutil
.run_break_set_by_file_and_line(
473 self
, "main.cpp", self
.line
, num_expected_locations
=1, loc_exact
=True
476 self
.runCmd("run", RUN_SUCCEEDED
)
478 # The stop reason of the thread should be breakpoint.
481 STOPPED_DUE_TO_BREAKPOINT
,
482 substrs
=["stopped", "stop reason = breakpoint"],
485 # This is the function to remove the custom formats in order to have a
486 # clean slate for the next test case.
488 self
.runCmd("type format clear", check
=False)
489 self
.runCmd("type summary clear", check
=False)
490 self
.runCmd("type filter clear", check
=False)
491 self
.runCmd("type synthetic clear", check
=False)
492 self
.runCmd("type category delete foobar", check
=False)
493 self
.runCmd("type category delete JASSynth", check
=False)
494 self
.runCmd("type category delete newbar", check
=False)
495 self
.runCmd("settings set target.enable-synthetic-value true")
497 # Execute the cleanup function during test case tear down.
498 self
.addTearDownHook(cleanup
)
501 self
.dbg
.GetSelectedTarget()
506 int_vector
= frame
.FindVariable("int_vector")
509 self
.assertEqual(int_vector
.GetNumChildren(), 0, "synthetic vector is empty")
511 self
.runCmd("settings set target.enable-synthetic-value false")
513 self
.dbg
.GetSelectedTarget()
518 int_vector
= frame
.FindVariable("int_vector")
522 int_vector
.GetNumChildren() == 0, '"physical" vector is not empty'
525 self
.runCmd("settings set target.enable-synthetic-value true")
527 self
.dbg
.GetSelectedTarget()
532 int_vector
= frame
.FindVariable("int_vector")
536 int_vector
.GetNumChildren(), 0, "synthetic vector is still empty"