1"""
2Test stopping at a breakpoint in an expression, and unwinding from there.
3"""
4
5
6
7import unittest2
8
9import lldb
10from lldbsuite.test.decorators import *
11from lldbsuite.test.lldbtest import *
12from lldbsuite.test import lldbutil
13
14
15class UnwindFromExpressionTest(TestBase):
16
17    mydir = TestBase.compute_mydir(__file__)
18    main_spec = lldb.SBFileSpec("main.cpp", False)
19
20    def build_and_run_to_bkpt(self):
21        self.build()
22
23        (target, process, self.thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
24                "// Set a breakpoint here to get started", self.main_spec)
25
26        # Next set a breakpoint in this function, set up Expression options to stop on
27        # breakpoint hits, and call the function.
28        self.fun_bkpt = self.target().BreakpointCreateBySourceRegex(
29            "// Stop inside the function here.", self.main_spec)
30        self.assertTrue(self.fun_bkpt, VALID_BREAKPOINT)
31
32
33    @no_debug_info_test
34    @expectedFailureAll(bugnumber="llvm.org/pr33164")
35    def test_conditional_bktp(self):
36        """
37        Test conditional breakpoint handling in the IgnoreBreakpoints = False case
38        """
39        self.build_and_run_to_bkpt()
40
41        self.fun_bkpt.SetCondition("0") # Should not get hit
42        options = lldb.SBExpressionOptions()
43        options.SetIgnoreBreakpoints(False)
44        options.SetUnwindOnError(False)
45
46        main_frame = self.thread.GetFrameAtIndex(0)
47        val = main_frame.EvaluateExpression("second_function(47)", options)
48        self.assertTrue(
49            val.GetError().Success(),
50            "We did complete the execution.")
51        self.assertEquals(47, val.GetValueAsSigned())
52
53
54    @add_test_categories(['pyapi'])
55    @expectedFlakeyNetBSD
56    def test_unwind_expression(self):
57        """Test unwinding from an expression."""
58        self.build_and_run_to_bkpt()
59
60        # Run test with varying one thread timeouts to also test the halting
61        # logic in the IgnoreBreakpoints = False case
62        self.do_unwind_test(self.thread, self.fun_bkpt, 1000)
63        self.do_unwind_test(self.thread, self.fun_bkpt, 100000)
64
65    def do_unwind_test(self, thread, bkpt, timeout):
66        #
67        # Use Python API to evaluate expressions while stopped in a stack frame.
68        #
69        main_frame = thread.GetFrameAtIndex(0)
70
71        options = lldb.SBExpressionOptions()
72        options.SetIgnoreBreakpoints(False)
73        options.SetUnwindOnError(False)
74        options.SetOneThreadTimeoutInMicroSeconds(timeout)
75
76        val = main_frame.EvaluateExpression("a_function_to_call()", options)
77
78        self.assertTrue(
79            val.GetError().Fail(),
80            "We did not complete the execution.")
81        error_str = val.GetError().GetCString()
82        self.assertTrue(
83            "Execution was interrupted, reason: breakpoint" in error_str,
84            "And the reason was right.")
85
86        thread = lldbutil.get_one_thread_stopped_at_breakpoint(
87            self.process(), bkpt)
88        self.assertTrue(
89            thread.IsValid(),
90            "We are indeed stopped at our breakpoint")
91
92        # Now unwind the expression, and make sure we got back to where we
93        # started.
94        error = thread.UnwindInnermostExpression()
95        self.assertTrue(error.Success(), "We succeeded in unwinding")
96
97        cur_frame = thread.GetFrameAtIndex(0)
98        self.assertTrue(
99            cur_frame.IsEqual(main_frame),
100            "We got back to the main frame.")
101