From 1422d6760c47f1b04ff66e3bd3e7fda89b2c0b59 Mon Sep 17 00:00:00 2001 From: Enrico Granata Date: Wed, 15 Feb 2012 02:36:32 +0000 Subject: [PATCH] adding a new test case for the SB formatters API llvm-svn: 150559 --- lldb/test/python_api/formatters/Makefile | 5 + .../formatters/TestFormattersSBAPI.py | 261 ++++++++++++++++++ lldb/test/python_api/formatters/jas_synth.py | 23 ++ lldb/test/python_api/formatters/main.cpp | 38 +++ 4 files changed, 327 insertions(+) create mode 100644 lldb/test/python_api/formatters/Makefile create mode 100644 lldb/test/python_api/formatters/TestFormattersSBAPI.py create mode 100644 lldb/test/python_api/formatters/jas_synth.py create mode 100644 lldb/test/python_api/formatters/main.cpp diff --git a/lldb/test/python_api/formatters/Makefile b/lldb/test/python_api/formatters/Makefile new file mode 100644 index 000000000000..8a7102e347af --- /dev/null +++ b/lldb/test/python_api/formatters/Makefile @@ -0,0 +1,5 @@ +LEVEL = ../../make + +CXX_SOURCES := main.cpp + +include $(LEVEL)/Makefile.rules diff --git a/lldb/test/python_api/formatters/TestFormattersSBAPI.py b/lldb/test/python_api/formatters/TestFormattersSBAPI.py new file mode 100644 index 000000000000..39f0fc275034 --- /dev/null +++ b/lldb/test/python_api/formatters/TestFormattersSBAPI.py @@ -0,0 +1,261 @@ +"""Test Python APIs for working with formatters""" + +import os, sys, time +import unittest2 +import lldb +from lldbtest import * + +class SBFormattersAPITestCase(TestBase): + + mydir = os.path.join("python_api", "formatters") + + @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") + @python_api_test + def test_with_dsym_formatters_api(self): + """Test Python APIs for working with formatters""" + self.buildDsym() + self.setTearDownCleanup() + self.formatters() + + @python_api_test + def test_with_dwarf_formatters_api(self): + """Test Python APIs for working with formatters""" + self.buildDwarf() + self.setTearDownCleanup() + self.formatters() + + def setUp(self): + # Call super's setUp(). + TestBase.setUp(self) + self.line = line_number('main.cpp', '// Set break point at this line.') + + def formatters(self): + """Test Python APIs for working with formatters""" + self.runCmd("file a.out", CURRENT_EXECUTABLE_SET) + + self.expect("breakpoint set -f main.cpp -l %d" % self.line, + BREAKPOINT_CREATED, + startstr = "Breakpoint created: 1: file ='main.cpp', line = %d, locations = 1" % + self.line) + + self.runCmd("run", RUN_SUCCEEDED) + + # The stop reason of the thread should be breakpoint. + self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, + substrs = ['stopped', + 'stop reason = breakpoint']) + + # This is the function to remove the custom formats in order to have a + # clean slate for the next test case. + def cleanup(): + self.runCmd('type format clear', check=False) + self.runCmd('type summary clear', check=False) + self.runCmd('type filter clear', check=False) + self.runCmd('type synthetic clear', check=False) + self.runCmd('type category delete foobar', check=False) + self.runCmd('type category delete JASSynth', check=False) + self.runCmd('type category delete newbar', check=False) + + # Execute the cleanup function during test case tear down. + self.addTearDownHook(cleanup) + + + format = lldb.SBTypeFormat(lldb.eFormatHex) + category = self.dbg.GetDefaultCategory() + category.AddTypeFormat(lldb.SBTypeNameSpecifier("int"),format) + + self.expect("frame variable foo.A", + substrs = ['0x00000001']) + self.expect("frame variable foo.E", matching=False, + substrs = ['0x00000000b8cca70a']) + + category.AddTypeFormat(lldb.SBTypeNameSpecifier("long"),format) + self.expect("frame variable foo.A", + substrs = ['0x00000001']) + self.expect("frame variable foo.E", + substrs = ['0x00000000b8cca70a']) + + format.format = lldb.eFormatOctal + category.AddTypeFormat(lldb.SBTypeNameSpecifier("int"),format) + self.expect("frame variable foo.A", + substrs = ['01']) + self.expect("frame variable foo.E", + substrs = ['0x00000000b8cca70a']) + + category.DeleteTypeFormat(lldb.SBTypeNameSpecifier("int")) + category.DeleteTypeFormat(lldb.SBTypeNameSpecifier("long")) + self.expect("frame variable foo.A", matching=False, + substrs = ['01']) + self.expect("frame variable foo.E", matching=False, + substrs = ['0x00000000b8cca70a']) + + summary = lldb.SBTypeSummary.CreateWithSummaryString("the hello world you'll never see") + summary.SetSummaryString('hello world') + new_category = self.dbg.GetCategory("foobar") + self.assertFalse(new_category.IsValid(), "getting a non-existing category worked") + new_category = self.dbg.CreateCategory("foobar") + new_category.enabled = True + new_category.AddTypeSummary(lldb.SBTypeNameSpecifier("^.*t$",True),summary) + self.expect("frame variable foo.A", + substrs = ['hello world']) + self.expect("frame variable foo.E", matching=False, + substrs = ['hello world']) + self.expect("frame variable foo.B", + substrs = ['hello world']) + self.expect("frame variable foo.F", + substrs = ['hello world']) + new_category.enabled = False + self.expect("frame variable foo.A", matching=False, + substrs = ['hello world']) + self.expect("frame variable foo.E", matching=False, + substrs = ['hello world']) + self.expect("frame variable foo.B", matching=False, + substrs = ['hello world']) + self.expect("frame variable foo.F", matching=False, + substrs = ['hello world']) + self.dbg.DeleteCategory(new_category.GetName()) + self.expect("frame variable foo.A", matching=False, + substrs = ['hello world']) + self.expect("frame variable foo.E", matching=False, + substrs = ['hello world']) + self.expect("frame variable foo.B", matching=False, + substrs = ['hello world']) + self.expect("frame variable foo.F", matching=False, + substrs = ['hello world']) + + filter = lldb.SBTypeFilter(0) + filter.AppendExpressionPath("A") + filter.AppendExpressionPath("D") + self.assertTrue(filter.GetNumberOfExpressionPaths() == 2, "filter with two items does not have two items") + + category.AddTypeFilter(lldb.SBTypeNameSpecifier("JustAStruct"),filter) + self.expect("frame variable foo", + substrs = ['A = 1', 'D = 6.28']) + self.expect("frame variable foo", matching=False, + substrs = ['B = ', 'C = ', 'E = ', 'F = ']) + + category.DeleteTypeFilter(lldb.SBTypeNameSpecifier("JustAStruct",True)) + self.expect("frame variable foo", + substrs = ['A = 1', 'D = 6.28']) + self.expect("frame variable foo", matching=False, + substrs = ['B = ', 'C = ', 'E = ', 'F = ']) + + category.DeleteTypeFilter(lldb.SBTypeNameSpecifier("JustAStruct",False)) + self.expect("frame variable foo", + substrs = ['A = 1', 'D = 6.28']) + self.expect("frame variable foo", matching=True, + substrs = ['B = ', 'C = ', 'E = ', 'F = ']) + + self.runCmd("command script import --allow-reload ./jas_synth.py") + + self.expect("frame variable foo", matching=False, + substrs = ['X = 1']) + + self.dbg.GetCategory("JASSynth").SetEnabled(True) + self.expect("frame variable foo", matching=True, + substrs = ['X = 1']) + + self.dbg.GetCategory("JASSynth").SetEnabled(False) + + filter = lldb.SBTypeFilter(0) + filter.AppendExpressionPath("A") + filter.AppendExpressionPath("D") + category.AddTypeFilter(lldb.SBTypeNameSpecifier("JustAStruct"),filter) + self.expect("frame variable foo", + substrs = ['A = 1', 'D = 6.28']) + + self.assertTrue(filter.ReplaceExpressionPathAtIndex(0,"C"), "failed to replace an expression path in filter") + self.expect("frame variable foo", + substrs = ['A = 1', 'D = 6.28']) + category.AddTypeFilter(lldb.SBTypeNameSpecifier("JustAStruct"),filter) + self.expect("frame variable foo", + substrs = ["C = 'e'", 'D = 6.28']) + category.AddTypeFilter(lldb.SBTypeNameSpecifier("FooType"),filter) + filter.ReplaceExpressionPathAtIndex(1,"F") + self.expect("frame variable foo", + substrs = ["C = 'e'", 'D = 6.28']) + category.AddTypeFilter(lldb.SBTypeNameSpecifier("JustAStruct"),filter) + self.expect("frame variable foo", + substrs = ["C = 'e'", 'F = 0']) + self.expect("frame variable bar", + substrs = ["C = 'e'", 'D = 6.28']) + + chosen = self.dbg.GetFilterForType(lldb.SBTypeNameSpecifier("JustAStruct")) + self.assertTrue(chosen.count == 2, "wrong filter found for JustAStruct") + self.assertTrue(chosen.GetExpressionPathAtIndex(0) == 'C', "wrong item at index 0 for JustAStruct") + self.assertTrue(chosen.GetExpressionPathAtIndex(1) == 'F', "wrong item at index 1 for JustAStruct") + + self.assertFalse(category.DeleteTypeFilter(lldb.SBTypeNameSpecifier("NoSuchType")),"deleting a non-existing filter worked") + self.assertFalse(category.DeleteTypeSummary(lldb.SBTypeNameSpecifier("NoSuchType")),"deleting a non-existing summary worked") + self.assertFalse(category.DeleteTypeFormat(lldb.SBTypeNameSpecifier("NoSuchType")),"deleting a non-existing format worked") + self.assertFalse(category.DeleteTypeSynthetic(lldb.SBTypeNameSpecifier("NoSuchType")),"deleting a non-existing synthetic worked") + + self.assertFalse(category.DeleteTypeFilter(lldb.SBTypeNameSpecifier("")),"deleting a filter for '' worked") + self.assertFalse(category.DeleteTypeSummary(lldb.SBTypeNameSpecifier("")),"deleting a summary for '' worked") + self.assertFalse(category.DeleteTypeFormat(lldb.SBTypeNameSpecifier("")),"deleting a format for '' worked") + self.assertFalse(category.DeleteTypeSynthetic(lldb.SBTypeNameSpecifier("")),"deleting a synthetic for '' worked") + + try: + self.assertFalse(category.AddTypeSummary(lldb.SBTypeNameSpecifier("NoneSuchType"), None), "adding a summary valued None worked") + except: + pass + else: + self.assertFalse(True, "adding a summary valued None worked") + + try: + self.assertFalse(category.AddTypeFilter(lldb.SBTypeNameSpecifier("NoneSuchType"), None), "adding a filter valued None worked") + except: + pass + else: + self.assertFalse(True, "adding a filter valued None worked") + + try: + self.assertFalse(category.AddTypeSynthetic(lldb.SBTypeNameSpecifier("NoneSuchType"), None), "adding a synthetic valued None worked") + except: + pass + else: + self.assertFalse(True, "adding a synthetic valued None worked") + + try: + self.assertFalse(category.AddTypeFormat(lldb.SBTypeNameSpecifier("NoneSuchType"), None), "adding a format valued None worked") + except: + pass + else: + self.assertFalse(True, "adding a format valued None worked") + + + self.assertFalse(category.AddTypeSummary(lldb.SBTypeNameSpecifier("EmptySuchType"), lldb.SBTypeSummary()), "adding a summary without value worked") + self.assertFalse(category.AddTypeFilter(lldb.SBTypeNameSpecifier("EmptySuchType"), lldb.SBTypeFilter()), "adding a filter without value worked") + self.assertFalse(category.AddTypeSynthetic(lldb.SBTypeNameSpecifier("EmptySuchType"), lldb.SBTypeSynthetic()), "adding a synthetic without value worked") + self.assertFalse(category.AddTypeFormat(lldb.SBTypeNameSpecifier("EmptySuchType"), lldb.SBTypeFormat()), "adding a format without value worked") + + self.assertFalse(category.AddTypeSummary(lldb.SBTypeNameSpecifier(""), lldb.SBTypeSummary.CreateWithSummaryString("")), "adding a summary for an invalid type worked") + self.assertFalse(category.AddTypeFilter(lldb.SBTypeNameSpecifier(""), lldb.SBTypeFilter(0)), "adding a filter for an invalid type worked") + self.assertFalse(category.AddTypeSynthetic(lldb.SBTypeNameSpecifier(""), lldb.SBTypeSynthetic.CreateWithClassName("")), "adding a synthetic for an invalid type worked") + self.assertFalse(category.AddTypeFormat(lldb.SBTypeNameSpecifier(""), lldb.SBTypeFormat(lldb.eFormatHex)), "adding a format for an invalid type worked") + + new_category = self.dbg.CreateCategory("newbar") + new_category.AddTypeSummary(lldb.SBTypeNameSpecifier("JustAStruct"), + lldb.SBTypeSummary.CreateWithScriptCode("return 'hello scripted world';")) + self.expect("frame variable foo", matching=False, + substrs = ['hello scripted world']) + new_category.enabled = True + self.expect("frame variable foo", matching=True, + substrs = ['hello scripted world']) + + self.expect("frame variable foo_ptr", matching=True, + substrs = ['hello scripted world']) + new_category.AddTypeSummary(lldb.SBTypeNameSpecifier("JustAStruct"), + lldb.SBTypeSummary.CreateWithScriptCode("return 'hello scripted world';", + lldb.eTypeOptionSkipPointers)) + self.expect("frame variable foo", matching=True, + substrs = ['hello scripted world']) + + self.expect("frame variable foo_ptr", matching=False, + substrs = ['hello scripted world']) + +if __name__ == '__main__': + import atexit + lldb.SBDebugger.Initialize() + atexit.register(lambda: lldb.SBDebugger.Terminate()) + unittest2.main() diff --git a/lldb/test/python_api/formatters/jas_synth.py b/lldb/test/python_api/formatters/jas_synth.py new file mode 100644 index 000000000000..16db646d6605 --- /dev/null +++ b/lldb/test/python_api/formatters/jas_synth.py @@ -0,0 +1,23 @@ +import lldb +class jasSynthProvider: + def __init__(self, valobj, dict): + self.valobj = valobj; + def num_children(self): + return 2; + def get_child_at_index(self, index): + if index == 0: + child = self.valobj.GetChildMemberWithName('A'); + if index == 1: + child = self.valobj.CreateValueFromExpression('X', '(int)1') + return child; + def get_child_index(self, name): + if name == 'A': + return 0; + if name == 'X': + return 1; + return 2; + +def __lldb_init_module(debugger,dict): + debugger.CreateCategory("JASSynth").AddTypeSynthetic(lldb.SBTypeNameSpecifier("JustAStruct"), + lldb.SBTypeSynthetic.CreateWithClassName("jas_synth.jasSynthProvider")) + diff --git a/lldb/test/python_api/formatters/main.cpp b/lldb/test/python_api/formatters/main.cpp new file mode 100644 index 000000000000..6785749e7d2f --- /dev/null +++ b/lldb/test/python_api/formatters/main.cpp @@ -0,0 +1,38 @@ +#include + +struct JustAStruct +{ + int A; + float B; + char C; + double D; + long E; + short F; +}; + +struct FooType +{ + int A; + float B; + char C; + double D; + long E; + short F; +}; + +int main(int argc, char const *argv[]) { + JustAStruct foo; + foo.A = 1; + foo.B = 3.14; + foo.C = 'e'; + foo.D = 6.28; + foo.E = 3100419850; + FooType bar; + bar.A = 1; + bar.B = 3.14; + bar.C = 'e'; + bar.D = 6.28; + bar.E = 3100419850; + JustAStruct* foo_ptr = &foo; + return 0; // Set break point at this line. +}