1"""Test printing ivars and ObjC objects captured in blocks that are made in methods of an ObjC class."""
2
3import lldb
4from lldbsuite.test.decorators import *
5from lldbsuite.test.lldbtest import *
6from lldbsuite.test import lldbutil
7
8
9class TestObjCIvarsInBlocks(TestBase):
10
11    def setUp(self):
12        # Call super's setUp().
13        TestBase.setUp(self)
14        # Find the line numbers to break inside main().
15        self.main_source = "main.m"
16        self.class_source = "ivars-in-blocks.m"
17        self.class_source_file_spec = lldb.SBFileSpec(self.class_source)
18
19    @add_test_categories(['pyapi'])
20    @skipIf(dwarf_version=['<', '4'])
21    @expectedFailureAll(
22        archs=["i[3-6]86"],
23        bugnumber="This test requires the 2.0 runtime, so it will fail on i386")
24    def test_with_python_api(self):
25        """Test printing the ivars of the self when captured in blocks"""
26        self.build()
27        exe = self.getBuildArtifact("a.out")
28
29        target = self.dbg.CreateTarget(exe)
30        self.assertTrue(target, VALID_TARGET)
31
32        breakpoint = target.BreakpointCreateBySourceRegex(
33            '// Break here inside the block.', self.class_source_file_spec)
34        self.assertTrue(breakpoint, VALID_BREAKPOINT)
35
36        breakpoint_two = target.BreakpointCreateBySourceRegex(
37            '// Break here inside the class method block.', self.class_source_file_spec)
38        self.assertTrue(breakpoint, VALID_BREAKPOINT)
39
40        process = target.LaunchSimple(
41            None, None, self.get_process_working_directory())
42        self.assertTrue(process, "Created a process.")
43        self.assertEqual(
44            process.GetState(), lldb.eStateStopped,
45            "Stopped it too.")
46
47        thread_list = lldbutil.get_threads_stopped_at_breakpoint(
48            process, breakpoint)
49        self.assertEqual(len(thread_list), 1)
50        thread = thread_list[0]
51
52        frame = thread.GetFrameAtIndex(0)
53        self.assertTrue(frame, "frame 0 is valid")
54
55        # First use the FindVariable API to see if we can find the ivar by
56        # undecorated name:
57        direct_blocky = frame.GetValueForVariablePath("blocky_ivar")
58        self.assertTrue(direct_blocky, "Found direct access to blocky_ivar.")
59
60        # Now get it as a member of "self" and make sure the two values are
61        # equal:
62        self_var = frame.GetValueForVariablePath("self")
63        self.assertTrue(self_var, "Found self in block.")
64        indirect_blocky = self_var.GetChildMemberWithName("blocky_ivar")
65        self.assertTrue(indirect_blocky, "Found blocky_ivar through self")
66
67        error = lldb.SBError()
68        direct_value = direct_blocky.GetValueAsSigned(error)
69        self.assertSuccess(error, "Got direct value for blocky_ivar")
70
71        indirect_value = indirect_blocky.GetValueAsSigned(error)
72        self.assertSuccess(error, "Got indirect value for blocky_ivar")
73
74        self.assertEqual(
75            direct_value, indirect_value,
76            "Direct and indirect values are equal.")
77
78        # Now make sure that we can get at the captured ivar through the expression parser.
79        # Doing a little trivial math will force this into the real expression
80        # parser:
81        direct_expr = frame.EvaluateExpression("blocky_ivar + 10")
82        self.assertTrue(
83            direct_expr,
84            "Got blocky_ivar through the expression parser")
85
86        # Again, get the value through self directly and make sure they are the
87        # same:
88        indirect_expr = frame.EvaluateExpression("self->blocky_ivar + 10")
89        self.assertTrue(
90            indirect_expr,
91            "Got blocky ivar through expression parser using self.")
92
93        direct_value = direct_expr.GetValueAsSigned(error)
94        self.assertTrue(
95            error.Success(),
96            "Got value from direct use of expression parser")
97
98        indirect_value = indirect_expr.GetValueAsSigned(error)
99        self.assertTrue(
100            error.Success(),
101            "Got value from indirect access using the expression parser")
102
103        self.assertEqual(
104            direct_value, indirect_value,
105            "Direct ivar access and indirect through expression parser produce same value.")
106
107        process.Continue()
108        self.assertEqual(
109            process.GetState(), lldb.eStateStopped,
110            "Stopped at the second breakpoint.")
111
112        thread_list = lldbutil.get_threads_stopped_at_breakpoint(
113            process, breakpoint_two)
114        self.assertEqual(len(thread_list), 1)
115        thread = thread_list[0]
116
117        frame = thread.GetFrameAtIndex(0)
118        self.assertTrue(frame, "frame 0 is valid")
119
120        expr = frame.EvaluateExpression("(ret)")
121        self.assertTrue(
122            expr, "Successfully got a local variable in a block in a class method.")
123
124        ret_value_signed = expr.GetValueAsSigned(error)
125        self.trace('ret_value_signed = %i' % (ret_value_signed))
126        self.assertEqual(
127            ret_value_signed, 5,
128            "The local variable in the block was what we expected.")
129