1"""
2Test utility functions for the frame object.
3"""
4
5from __future__ import print_function
6
7
8import lldb
9from lldbsuite.test.decorators import *
10from lldbsuite.test.lldbtest import *
11from lldbsuite.test import lldbutil
12
13
14class FrameUtilsTestCase(TestBase):
15
16    mydir = TestBase.compute_mydir(__file__)
17
18    def setUp(self):
19        # Call super's setUp().
20        TestBase.setUp(self)
21        # Find the line number to break inside main().
22        self.line = line_number('main.c',
23                                "// Find the line number here.")
24
25    @add_test_categories(['pyapi'])
26    def test_frame_utils(self):
27        """Test utility functions for the frame object."""
28        self.build()
29        exe = self.getBuildArtifact("a.out")
30
31        target = self.dbg.CreateTarget(exe)
32        self.assertTrue(target, VALID_TARGET)
33
34        breakpoint = target.BreakpointCreateByLocation("main.c", self.line)
35        self.assertTrue(breakpoint, VALID_BREAKPOINT)
36
37        # Now launch the process, and do not stop at entry point.
38        process = target.LaunchSimple(
39            None, None, self.get_process_working_directory())
40
41        if not process:
42            self.fail("SBTarget.LaunchProcess() failed")
43        self.assertTrue(process.GetState() == lldb.eStateStopped,
44                        PROCESS_STOPPED)
45
46        import lldbsuite.test.lldbutil as lldbutil
47        thread = lldbutil.get_stopped_thread(
48            process, lldb.eStopReasonBreakpoint)
49        self.assertTrue(thread)
50        frame0 = thread.GetFrameAtIndex(0)
51        self.assertTrue(frame0)
52        frame1 = thread.GetFrameAtIndex(1)
53        self.assertTrue(frame1)
54        parent = lldbutil.get_parent_frame(frame0)
55        self.assertTrue(parent and parent.GetFrameID() == frame1.GetFrameID())
56        frame0_args = lldbutil.get_args_as_string(frame0)
57        parent_args = lldbutil.get_args_as_string(parent)
58        self.assertTrue(
59            frame0_args and parent_args and "(int)val=1" in frame0_args)
60        if self.TraceOn():
61            lldbutil.print_stacktrace(thread)
62            print("Current frame: %s" % frame0_args)
63            print("Parent frame: %s" % parent_args)
64