xref: /llvm-project/lldb/test/API/lang/c/unions/TestUnionMembers.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1import lldb
2from lldbsuite.test.lldbtest import *
3import lldbsuite.test.lldbutil as lldbutil
4
5
6class TestUnionMembers(TestBase):
7    def test_union_members(self):
8        self._load_exe()
9
10        # Set breakpoints
11        bp = self.target.BreakpointCreateBySourceRegex("Break here", self.src_file_spec)
12        self.assertTrue(bp.IsValid() and bp.GetNumLocations() >= 1, VALID_BREAKPOINT)
13
14        # Launch the process
15        self.process = self.target.LaunchSimple(
16            None, None, self.get_process_working_directory()
17        )
18        self.assertTrue(self.process.IsValid(), PROCESS_IS_VALID)
19        self.assertEqual(self.process.GetState(), lldb.eStateStopped, PROCESS_STOPPED)
20
21        thread = lldbutil.get_stopped_thread(self.process, lldb.eStopReasonBreakpoint)
22        self.assertTrue(thread.IsValid())
23        frame = thread.GetSelectedFrame()
24        self.assertTrue(frame.IsValid())
25
26        val = frame.EvaluateExpression("u")
27        self.assertTrue(val.IsValid())
28        val = frame.EvaluateExpression("u.s")
29        self.assertTrue(val.IsValid())
30        self.assertEqual(val.GetNumChildren(), 2)
31
32    def _load_exe(self):
33        self.build()
34
35        src_file = os.path.join(self.getSourceDir(), "main.c")
36        self.src_file_spec = lldb.SBFileSpec(src_file)
37        self.assertTrue(self.src_file_spec.IsValid(), "breakpoint file")
38
39        # Get the path of the executable
40        exe_path = self.getBuildArtifact("a.out")
41
42        # Load the executable
43        self.target = self.dbg.CreateTarget(exe_path)
44        self.assertTrue(self.target.IsValid(), VALID_TARGET)
45