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