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