1"""Test variable with function ptr type and that break on the function works."""
2
3import os, time
4import unittest2
5import lldb
6from lldbtest import *
7import lldbutil
8
9class FunctionTypesTestCase(TestBase):
10
11    mydir = os.path.join("lang", "c", "function_types")
12
13    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
14    @dsym_test
15    def test_with_dsym(self):
16        """Test 'callback' has function ptr type, then break on the function."""
17        self.buildDsym()
18        self.function_types()
19
20    @dwarf_test
21    def test_with_dwarf(self):
22        """Test 'callback' has function ptr type, then break on the function."""
23        self.buildDwarf()
24        self.function_types()
25
26    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
27    @dsym_test
28    def test_pointers_with_dsym(self):
29        """Test that a function pointer to 'printf' works and can be called."""
30        self.buildDsym()
31        self.function_pointers()
32
33    @expectedFailureFreeBSD('llvm.org/pr16697') # Expression fails with 'there is no JIT compiled function'
34    @dwarf_test
35    def test_pointers_with_dwarf(self):
36        """Test that a function pointer to 'printf' works and can be called."""
37        self.buildDwarf()
38        self.function_pointers()
39
40    def setUp(self):
41        # Call super's setUp().
42        TestBase.setUp(self)
43        # Find the line number to break inside main().
44        self.line = line_number('main.c', '// Set break point at this line.')
45
46    def runToBreakpoint(self):
47        exe = os.path.join(os.getcwd(), "a.out")
48        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
49
50        # Break inside the main.
51        lldbutil.run_break_set_by_file_and_line (self, "main.c", self.line, num_expected_locations=1, loc_exact=True)
52
53        self.runCmd("run", RUN_SUCCEEDED)
54
55        # The stop reason of the thread should be breakpoint.
56        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
57                    substrs = ['stopped',
58                               'stop reason = breakpoint'])
59
60        # The breakpoint should have a hit count of 1.
61        self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
62                    substrs = [' resolved, hit count = 1'])
63
64    def function_types(self):
65        """Test 'callback' has function ptr type, then break on the function."""
66
67        self.runToBreakpoint()
68
69        # Check that the 'callback' variable display properly.
70        self.expect("frame variable --show-types callback", VARIABLES_DISPLAYED_CORRECTLY,
71            startstr = '(int (*)(const char *)) callback =')
72
73        # And that we can break on the callback function.
74        lldbutil.run_break_set_by_symbol (self, "string_not_empty", num_expected_locations=1, sym_exact=True)
75        self.runCmd("continue")
76
77        # Check that we do indeed stop on the string_not_empty function.
78        self.expect("process status", STOPPED_DUE_TO_BREAKPOINT,
79            substrs = ['a.out`string_not_empty',
80                       'stop reason = breakpoint'])
81
82    def function_pointers(self):
83        """Test that a function pointer to 'printf' works and can be called."""
84
85        self.runToBreakpoint()
86
87        self.expect("expr string_not_empty",
88                    substrs = ['(int (*)(const char *)) $0 = ', '(a.out`'])
89
90        if sys.platform.startswith("darwin"):
91            regexps = ['lib.*\.dylib`printf']
92        else:
93            regexps = ['printf']
94        self.expect("expr (int (*)(const char*, ...))printf",
95                    substrs = ['(int (*)(const char *, ...)) $1 = '],
96                    patterns = regexps)
97
98        self.expect("expr $1(\"Hello world\\n\")",
99                    startstr = '(int) $2 = 12')
100
101if __name__ == '__main__':
102    import atexit
103    lldb.SBDebugger.Initialize()
104    atexit.register(lambda: lldb.SBDebugger.Terminate())
105    unittest2.main()
106