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.assertSuccess(val.GetError(), "We did complete the execution.")
49        self.assertEquals(47, val.GetValueAsSigned())
50
51
52    @add_test_categories(['pyapi'])
53    @expectedFlakeyNetBSD
54    def test_unwind_expression(self):
55        """Test unwinding from an expression."""
56        self.build_and_run_to_bkpt()
57
58        # Run test with varying one thread timeouts to also test the halting
59        # logic in the IgnoreBreakpoints = False case
60        self.do_unwind_test(self.thread, self.fun_bkpt, 1000)
61        self.do_unwind_test(self.thread, self.fun_bkpt, 100000)
62
63    def do_unwind_test(self, thread, bkpt, timeout):
64        #
65        # Use Python API to evaluate expressions while stopped in a stack frame.
66        #
67        main_frame = thread.GetFrameAtIndex(0)
68
69        options = lldb.SBExpressionOptions()
70        options.SetIgnoreBreakpoints(False)
71        options.SetUnwindOnError(False)
72        options.SetOneThreadTimeoutInMicroSeconds(timeout)
73
74        val = main_frame.EvaluateExpression("a_function_to_call()", options)
75
76        self.assertTrue(
77            val.GetError().Fail(),
78            "We did not complete the execution.")
79        error_str = val.GetError().GetCString()
80        self.assertTrue(
81            "Execution was interrupted, reason: breakpoint" in error_str,
82            "And the reason was right.")
83
84        thread = lldbutil.get_one_thread_stopped_at_breakpoint(
85            self.process(), bkpt)
86        self.assertTrue(
87            thread.IsValid(),
88            "We are indeed stopped at our breakpoint")
89
90        # Now unwind the expression, and make sure we got back to where we
91        # started.
92        self.assertSuccess(thread.UnwindInnermostExpression(),
93                "We succeeded in unwinding")
94
95        cur_frame = thread.GetFrameAtIndex(0)
96        self.assertTrue(
97            cur_frame.IsEqual(main_frame),
98            "We got back to the main frame.")
99