TestFunctionTypes.py revision 6475c42148a8ea1ca86e5db465db7eca742d897d
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    @dwarf_test
34    def test_pointers_with_dwarf(self):
35        """Test that a function pointer to 'printf' works and can be called."""
36        self.buildDwarf()
37        self.function_pointers()
38
39    def setUp(self):
40        # Call super's setUp().
41        TestBase.setUp(self)
42        # Find the line number to break inside main().
43        self.line = line_number('main.c', '// Set break point at this line.')
44
45    def runToBreakpoint(self):
46        exe = os.path.join(os.getcwd(), "a.out")
47        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
48
49        # Break inside the main.
50        lldbutil.run_break_set_by_file_and_line (self, "main.c", self.line, num_expected_locations=1, loc_exact=True)
51
52        self.runCmd("run", RUN_SUCCEEDED)
53
54        # The stop reason of the thread should be breakpoint.
55        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
56                    substrs = ['stopped',
57                               'stop reason = breakpoint'])
58
59        # The breakpoint should have a hit count of 1.
60        self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
61                    substrs = [' resolved, hit count = 1'])
62
63    def function_types(self):
64        """Test 'callback' has function ptr type, then break on the function."""
65
66        self.runToBreakpoint()
67
68        # Check that the 'callback' variable display properly.
69        self.expect("frame variable --show-types callback", VARIABLES_DISPLAYED_CORRECTLY,
70            startstr = '(int (*)(const char *)) callback =')
71
72        # And that we can break on the callback function.
73        lldbutil.run_break_set_by_symbol (self, "string_not_empty", num_expected_locations=1, sym_exact=True)
74        self.runCmd("continue")
75
76        # Check that we do indeed stop on the string_not_empty function.
77        self.expect("process status", STOPPED_DUE_TO_BREAKPOINT,
78            substrs = ['a.out`string_not_empty',
79                       'stop reason = breakpoint'])
80
81    def function_pointers(self):
82        """Test that a function pointer to 'printf' works and can be called."""
83
84        self.runToBreakpoint()
85
86        self.expect("expr string_not_empty",
87                    substrs = ['(int (*)(const char *)) $0 = ', '(a.out`'])
88
89        if sys.platform.startswith("darwin"):
90            regexps = ['lib.*\.dylib`printf']
91        else:
92            regexps = ['printf']
93        self.expect("expr (int (*)(const char*, ...))printf",
94                    substrs = ['(int (*)(const char *, ...)) $1 = '],
95                    patterns = regexps)
96
97        self.expect("expr $1(\"Hello world\\n\")",
98                    startstr = '(int) $2 = 12')
99
100if __name__ == '__main__':
101    import atexit
102    lldb.SBDebugger.Initialize()
103    atexit.register(lambda: lldb.SBDebugger.Terminate())
104    unittest2.main()
105