1"""
2Test calling a function that waits a while, and make sure the timeout option to expr works.
3"""
4
5import unittest2
6import lldb
7import lldbutil
8from lldbtest import *
9
10class ExprCommandWithTimeoutsTestCase(TestBase):
11
12    mydir = os.path.join("expression_command", "timeout")
13
14    def setUp(self):
15        # Call super's setUp().
16        TestBase.setUp(self)
17
18        self.main_source = "wait-a-while.c"
19        self.main_source_spec = lldb.SBFileSpec (self.main_source)
20
21
22    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
23    @dsym_test
24    def test_with_dsym(self):
25        """Test calling std::String member function."""
26        self.buildDsym()
27        self.call_function()
28
29    @dwarf_test
30    def test_with_dwarf(self):
31        """Test calling std::String member function."""
32        self.buildDwarf()
33        self.call_function()
34
35    def call_function(self):
36        """Test calling function with timeout."""
37        exe_name = "a.out"
38        exe = os.path.join(os.getcwd(), exe_name)
39
40        target = self.dbg.CreateTarget(exe)
41        self.assertTrue(target, VALID_TARGET)
42
43        breakpoint = target.BreakpointCreateBySourceRegex('stop here in main.',self.main_source_spec)
44        self.assertTrue(breakpoint, VALID_BREAKPOINT)
45        self.runCmd("breakpoint list")
46
47        # Launch the process, and do not stop at the entry point.
48        process = target.LaunchSimple(None, None, os.getcwd())
49
50        self.assertTrue(process, PROCESS_IS_VALID)
51
52        # Frame #0 should be on self.step_out_of_malloc.
53        threads = lldbutil.get_threads_stopped_at_breakpoint (process, breakpoint)
54
55        self.assertTrue(len(threads) == 1)
56        thread = threads[0]
57
58        # First set the timeout too short, and make sure we fail.
59        options = lldb.SBExpressionOptions()
60        options.SetTimeoutInMicroSeconds(100)
61        options.SetUnwindOnError(True)
62
63        frame = thread.GetFrameAtIndex(0)
64
65        value = frame.EvaluateExpression ("wait_a_while (10000)", options)
66        self.assertTrue (value.IsValid())
67        self.assertTrue (value.GetError().Success() == False)
68
69        # Now do the same thing with the command line command, and make sure it works too.
70        interp = self.dbg.GetCommandInterpreter()
71
72        result = lldb.SBCommandReturnObject()
73        return_value = interp.HandleCommand ("expr -t 100 -u true -- wait_a_while(10000)", result)
74        self.assertTrue (return_value == lldb.eReturnStatusFailed)
75
76        # Okay, now do it again with long enough time outs:
77
78        options.SetTimeoutInMicroSeconds(1000000)
79        value = frame.EvaluateExpression ("wait_a_while (1000)", options)
80        self.assertTrue(value.IsValid())
81        self.assertTrue (value.GetError().Success() == True)
82
83        # Now do the same thingwith the command line command, and make sure it works too.
84        interp = self.dbg.GetCommandInterpreter()
85
86        result = lldb.SBCommandReturnObject()
87        return_value = interp.HandleCommand ("expr -t 1000000 -u true -- wait_a_while(1000)", result)
88        self.assertTrue(return_value == lldb.eReturnStatusSuccessFinishResult)
89
90if __name__ == '__main__':
91    import atexit
92    lldb.SBDebugger.Initialize()
93    atexit.register(lambda: lldb.SBDebugger.Terminate())
94    unittest2.main()
95