1# encoding: utf-8
2"""
3Test lldb's frame recognizers.
4"""
5
6import lldb
7from lldbsuite.test.decorators import *
8from lldbsuite.test.lldbtest import *
9from lldbsuite.test import lldbutil
10
11import recognizer
12
13class FrameRecognizerTestCase(TestBase):
14
15    mydir = TestBase.compute_mydir(__file__)
16    NO_DEBUG_INFO_TESTCASE = True
17
18    @skipUnlessDarwin
19    def test_frame_recognizer_1(self):
20        self.build()
21
22        target = self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
23        self.assertTrue(target, VALID_TARGET)
24
25        # Clear internal & plugins recognizers that get initialized at launch
26        self.runCmd("frame recognizer clear")
27
28        self.runCmd("command script import " + os.path.join(self.getSourceDir(), "recognizer.py"))
29
30        self.expect("frame recognizer list",
31                    substrs=['no matching results found.'])
32
33        self.runCmd("frame recognizer add -l recognizer.MyFrameRecognizer -s a.out -n foo")
34
35        self.expect("frame recognizer list",
36                    substrs=['0: recognizer.MyFrameRecognizer, module a.out, function foo'])
37
38        self.runCmd("frame recognizer add -l recognizer.MyOtherFrameRecognizer -s a.out -n bar -x")
39
40        self.expect(
41            "frame recognizer list",
42            substrs=[
43                '1: recognizer.MyOtherFrameRecognizer, module a.out, function bar (regexp)',
44                '0: recognizer.MyFrameRecognizer, module a.out, function foo'
45            ])
46
47        self.runCmd("frame recognizer delete 0")
48
49        self.expect("frame recognizer list",
50                    substrs=['1: recognizer.MyOtherFrameRecognizer, module a.out, function bar (regexp)'])
51
52        self.runCmd("frame recognizer clear")
53
54        self.expect("frame recognizer list",
55                    substrs=['no matching results found.'])
56
57        self.runCmd("frame recognizer add -l recognizer.MyFrameRecognizer -s a.out -n foo")
58
59        lldbutil.run_break_set_by_symbol(self, "foo")
60        self.runCmd("r")
61
62        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
63                    substrs=['stopped', 'stop reason = breakpoint'])
64
65        process = target.GetProcess()
66        thread = process.GetSelectedThread()
67        frame = thread.GetSelectedFrame()
68
69        self.assertEqual(frame.GetSymbol().GetName(), "foo")
70        self.assertFalse(frame.GetLineEntry().IsValid())
71
72        self.expect("frame variable",
73                    substrs=['(int) a = 42', '(int) b = 56'])
74
75        # Recognized arguments don't show up by default...
76        variables = frame.GetVariables(lldb.SBVariablesOptions())
77        self.assertEqual(variables.GetSize(), 0)
78
79        # ...unless you set target.display-recognized-arguments to 1...
80        self.runCmd("settings set target.display-recognized-arguments 1")
81        variables = frame.GetVariables(lldb.SBVariablesOptions())
82        self.assertEqual(variables.GetSize(), 2)
83
84        # ...and you can reset it back to 0 to hide them again...
85        self.runCmd("settings set target.display-recognized-arguments 0")
86        variables = frame.GetVariables(lldb.SBVariablesOptions())
87        self.assertEqual(variables.GetSize(), 0)
88
89        # ... or explicitly ask for them with SetIncludeRecognizedArguments(True).
90        opts = lldb.SBVariablesOptions()
91        opts.SetIncludeRecognizedArguments(True)
92        variables = frame.GetVariables(opts)
93
94        self.assertEqual(variables.GetSize(), 2)
95        self.assertEqual(variables.GetValueAtIndex(0).name, "a")
96        self.assertEqual(variables.GetValueAtIndex(0).signed, 42)
97        self.assertEqual(variables.GetValueAtIndex(0).GetValueType(), lldb.eValueTypeVariableArgument)
98        self.assertEqual(variables.GetValueAtIndex(1).name, "b")
99        self.assertEqual(variables.GetValueAtIndex(1).signed, 56)
100        self.assertEqual(variables.GetValueAtIndex(1).GetValueType(), lldb.eValueTypeVariableArgument)
101
102        self.expect("frame recognizer info 0",
103                    substrs=['frame 0 is recognized by recognizer.MyFrameRecognizer'])
104
105        self.expect("frame recognizer info 999", error=True,
106                    substrs=['no frame with index 999'])
107
108        self.expect("frame recognizer info 1",
109                    substrs=['frame 1 not recognized by any recognizer'])
110
111        # FIXME: The following doesn't work yet, but should be fixed.
112        """
113        lldbutil.run_break_set_by_symbol(self, "bar")
114        self.runCmd("c")
115
116        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
117                    substrs=['stopped', 'stop reason = breakpoint'])
118
119        self.expect("frame variable -t",
120                    substrs=['(int *) a = '])
121
122        self.expect("frame variable -t *a",
123                    substrs=['*a = 78'])
124        """
125