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