1"""
2Use lldb Python API to make sure the dynamic checkers are doing their jobs.
3"""
4
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class ObjCCheckerTestCase(TestBase):
14
15    NO_DEBUG_INFO_TESTCASE = True
16
17    def setUp(self):
18        # Call super's setUp().
19        TestBase.setUp(self)
20
21        # Find the line number to break for main.c.
22        self.source_name = 'main.m'
23
24    @add_test_categories(['pyapi'])
25    def test_objc_checker(self):
26        """Test that checkers catch unrecognized selectors"""
27        if self.getArchitecture() == 'i386':
28            self.skipTest("requires Objective-C 2.0 runtime")
29
30        self.build()
31        exe = self.getBuildArtifact("a.out")
32
33        # Create a target from the debugger.
34
35        target = self.dbg.CreateTarget(exe)
36        self.assertTrue(target, VALID_TARGET)
37
38        # Set up our breakpoints:
39
40        main_bkpt = target.BreakpointCreateBySourceRegex(
41            "Set a breakpoint here.", lldb.SBFileSpec(self.source_name))
42        self.assertTrue(main_bkpt and
43                        main_bkpt.GetNumLocations() == 1,
44                        VALID_BREAKPOINT)
45
46        # Now launch the process, and do not stop at the entry point.
47        process = target.LaunchSimple(
48            None, None, self.get_process_working_directory())
49
50        self.assertState(process.GetState(), lldb.eStateStopped,
51                         PROCESS_STOPPED)
52
53        threads = lldbutil.get_threads_stopped_at_breakpoint(
54            process, main_bkpt)
55        self.assertEqual(len(threads), 1)
56        thread = threads[0]
57
58        #
59        #  The class Simple doesn't have a count method.  Make sure that we don't
60        #  actually try to send count but catch it as an unrecognized selector.
61
62        frame = thread.GetFrameAtIndex(0)
63        expr_value = frame.EvaluateExpression("(int) [my_simple count]", False)
64        expr_error = expr_value.GetError()
65
66        self.assertTrue(expr_error.Fail())
67
68        # Make sure the call produced no NSLog stdout.
69        stdout = process.GetSTDOUT(100)
70        self.assertTrue(stdout is None or (len(stdout) == 0))
71
72        # Make sure the error is helpful:
73        err_string = expr_error.GetCString()
74        self.assertIn("selector", err_string)
75
76        #
77        # Check that we correctly insert the checker for an
78        # ObjC method with the struct return convention.
79        # Getting this wrong would cause us to call the checker
80        # with the wrong arguments, and the checker would crash
81        # So I'm just checking "expression runs successfully" here:
82        #
83        expr_value = frame.EvaluateExpression("[my_simple getBigStruct]", False)
84        expr_error = expr_value.GetError()
85
86        self.assertSuccess(expr_error)
87
88