xref: /llvm-project/lldb/test/API/lang/objc/blocks/TestObjCIvarsInBlocks.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
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    def setUp(self):
11        # Call super's setUp().
12        TestBase.setUp(self)
13        # Find the line numbers to break inside main().
14        self.main_source = "main.m"
15        self.class_source = "ivars-in-blocks.m"
16        self.class_source_file_spec = lldb.SBFileSpec(self.class_source)
17
18    @add_test_categories(["pyapi"])
19    @skipIf(dwarf_version=["<", "4"])
20    @expectedFailureAll(
21        archs=["i[3-6]86"],
22        bugnumber="This test requires the 2.0 runtime, so it will fail on i386",
23    )
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        )
35        self.assertTrue(breakpoint, VALID_BREAKPOINT)
36
37        breakpoint_two = target.BreakpointCreateBySourceRegex(
38            "// Break here inside the class method block.", self.class_source_file_spec
39        )
40        self.assertTrue(breakpoint, VALID_BREAKPOINT)
41
42        process = target.LaunchSimple(None, None, self.get_process_working_directory())
43        self.assertTrue(process, "Created a process.")
44        self.assertEqual(process.GetState(), lldb.eStateStopped, "Stopped it too.")
45
46        self.runCmd("settings set target.prefer-dynamic-value no-dynamic-values")
47
48        thread_list = lldbutil.get_threads_stopped_at_breakpoint(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, "Direct and indirect values are equal."
76        )
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(direct_expr, "Got blocky_ivar through the expression parser")
83
84        # Again, get the value through self directly and make sure they are the
85        # same:
86        indirect_expr = frame.EvaluateExpression("self->blocky_ivar + 10")
87        self.assertTrue(
88            indirect_expr, "Got blocky ivar through expression parser using self."
89        )
90
91        direct_value = direct_expr.GetValueAsSigned(error)
92        self.assertTrue(
93            error.Success(), "Got value from direct use of expression parser"
94        )
95
96        indirect_value = indirect_expr.GetValueAsSigned(error)
97        self.assertTrue(
98            error.Success(),
99            "Got value from indirect access using the expression parser",
100        )
101
102        self.assertEqual(
103            direct_value,
104            indirect_value,
105            "Direct ivar access and indirect through expression parser produce same value.",
106        )
107
108        process.Continue()
109        self.assertEqual(
110            process.GetState(), lldb.eStateStopped, "Stopped at the second breakpoint."
111        )
112
113        thread_list = lldbutil.get_threads_stopped_at_breakpoint(
114            process, breakpoint_two
115        )
116        self.assertEqual(len(thread_list), 1)
117        thread = thread_list[0]
118
119        frame = thread.GetFrameAtIndex(0)
120        self.assertTrue(frame, "frame 0 is valid")
121
122        expr = frame.EvaluateExpression("(ret)")
123        self.assertTrue(
124            expr, "Successfully got a local variable in a block in a class method."
125        )
126
127        ret_value_signed = expr.GetValueAsSigned(error)
128        self.trace("ret_value_signed = %i" % (ret_value_signed))
129        self.assertEqual(
130            ret_value_signed, 5, "The local variable in the block was what we expected."
131        )
132