TestFunctionTypes.py revision 21b1984e161b0cadee331d32bfd721eccfdf4b1f
1fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen"""Test variable with function ptr type and that break on the function works."""
2fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
3fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chenimport os, time
475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenimport unittest2
5a1affab962b9f57b64d798ea7fa93dc71d4cc0b1Johnny Chenimport lldb
6d85dae5a089177582aff128c897c78332167fe08Johnny Chenfrom lldbtest import *
7fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
81c42e8684d26e1473f92c799eeae65a3eec991d6Johnny Chenclass FunctionTypesTestCase(TestBase):
9fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
10de2f1ed2a5dfe306aa6c5d48baf65808608cefb5Johnny Chen    mydir = os.path.join("lang", "c", "function_types")
11fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
12d9ed552026a5d2842d95b2852f33346e867e9799Johnny Chen    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
1321b1984e161b0cadee331d32bfd721eccfdf4b1fJohnny Chen    @dsym_test
14d9ed552026a5d2842d95b2852f33346e867e9799Johnny Chen    def test_with_dsym(self):
15d9ed552026a5d2842d95b2852f33346e867e9799Johnny Chen        """Test 'callback' has function ptr type, then break on the function."""
16d9ed552026a5d2842d95b2852f33346e867e9799Johnny Chen        self.buildDsym()
17d9ed552026a5d2842d95b2852f33346e867e9799Johnny Chen        self.function_types()
18d9ed552026a5d2842d95b2852f33346e867e9799Johnny Chen
1921b1984e161b0cadee331d32bfd721eccfdf4b1fJohnny Chen    @dwarf_test
20d9ed552026a5d2842d95b2852f33346e867e9799Johnny Chen    def test_with_dwarf(self):
21d9ed552026a5d2842d95b2852f33346e867e9799Johnny Chen        """Test 'callback' has function ptr type, then break on the function."""
22d9ed552026a5d2842d95b2852f33346e867e9799Johnny Chen        self.buildDwarf()
23d9ed552026a5d2842d95b2852f33346e867e9799Johnny Chen        self.function_types()
246c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan
256c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
2621b1984e161b0cadee331d32bfd721eccfdf4b1fJohnny Chen    @dsym_test
276c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan    def test_pointers_with_dsym(self):
286c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan        """Test that a function pointer to 'printf' works and can be called."""
296c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan        self.buildDsym()
30c01c05c7a8a198761fb917308dd53e6e143384adSean Callanan        self.function_pointers()
316c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan
3221b1984e161b0cadee331d32bfd721eccfdf4b1fJohnny Chen    @dwarf_test
336c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan    def test_pointers_with_dwarf(self):
346c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan        """Test that a function pointer to 'printf' works and can be called."""
356c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan        self.buildDwarf()
36c01c05c7a8a198761fb917308dd53e6e143384adSean Callanan        self.function_pointers()
37d9ed552026a5d2842d95b2852f33346e867e9799Johnny Chen
38143914211a7e009c90a7d88e7584513d7a4af67fJohnny Chen    def setUp(self):
390910eb32b94bd67b0357d34d0951aad197742569Johnny Chen        # Call super's setUp().
400910eb32b94bd67b0357d34d0951aad197742569Johnny Chen        TestBase.setUp(self)
41143914211a7e009c90a7d88e7584513d7a4af67fJohnny Chen        # Find the line number to break inside main().
42143914211a7e009c90a7d88e7584513d7a4af67fJohnny Chen        self.line = line_number('main.c', '// Set break point at this line.')
43143914211a7e009c90a7d88e7584513d7a4af67fJohnny Chen
446c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan    def runToBreakpoint(self):
45fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        exe = os.path.join(os.getcwd(), "a.out")
464f995f0718299696719089084a622835b6a7b4b8Johnny Chen        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
476c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan
48fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        # Break inside the main.
49143914211a7e009c90a7d88e7584513d7a4af67fJohnny Chen        self.expect("breakpoint set -f main.c -l %d" % self.line,
50143914211a7e009c90a7d88e7584513d7a4af67fJohnny Chen                    BREAKPOINT_CREATED,
516c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan                    startstr = "Breakpoint created: 1: file ='main.c', line = %d, locations = 1" %
526c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan                    self.line)
536c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan
541bb9f9a3917c7ac7228d0a81a0ff8a225165800aJohnny Chen        self.runCmd("run", RUN_SUCCEEDED)
556c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan
56fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        # The stop reason of the thread should be breakpoint.
574f995f0718299696719089084a622835b6a7b4b8Johnny Chen        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
586c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan                    substrs = ['stopped',
596c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan                               'stop reason = breakpoint'])
606c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan
61fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        # The breakpoint should have a hit count of 1.
6241950cc77813637e6c67b069e4ad2faf8c5f6fa7Caroline Tice        self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
636c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan                    substrs = [' resolved, hit count = 1'])
646c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan
656c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan    def function_types(self):
666c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan        """Test 'callback' has function ptr type, then break on the function."""
676c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan
686c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan        self.runToBreakpoint()
69fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
70fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        # Check that the 'callback' variable display properly.
7110de7d1db3ec782ea2ccda1f39c0a40b9c301594Jim Ingham        self.expect("frame variable -T callback", VARIABLES_DISPLAYED_CORRECTLY,
72397636ec9b675f4d917765400419d6b4805bb8a3Johnny Chen            startstr = '(int (*)(const char *)) callback =')
73fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
74fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        # And that we can break on the callback function.
754f995f0718299696719089084a622835b6a7b4b8Johnny Chen        self.runCmd("breakpoint set -n string_not_empty", BREAKPOINT_CREATED)
764f995f0718299696719089084a622835b6a7b4b8Johnny Chen        self.runCmd("continue")
77fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
78fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        # Check that we do indeed stop on the string_not_empty function.
794f995f0718299696719089084a622835b6a7b4b8Johnny Chen        self.expect("process status", STOPPED_DUE_TO_BREAKPOINT,
8059ea45fc85deacf0b8ca8250f116b7cc7b3691a5Johnny Chen            substrs = ['a.out`string_not_empty',
814f995f0718299696719089084a622835b6a7b4b8Johnny Chen                       'stop reason = breakpoint'])
82fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
836c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan    def function_pointers(self):
846c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan        """Test that a function pointer to 'printf' works and can be called."""
856c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan
866c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan        self.runToBreakpoint()
876c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan
886c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan        self.expect("expr string_not_empty",
896c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan                    substrs = ['(int (*)(const char *)) $0 = ', '(a.out`'])
906c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan
91081137057abc6b2c32eb077bde7459f46544f7d8Johnny Chen        if sys.platform.startswith("darwin"):
92081137057abc6b2c32eb077bde7459f46544f7d8Johnny Chen            regexps = ['lib.*\.dylib`printf']
93081137057abc6b2c32eb077bde7459f46544f7d8Johnny Chen        else:
94081137057abc6b2c32eb077bde7459f46544f7d8Johnny Chen            regexps = ['printf']
956c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan        self.expect("expr (int (*)(const char*, ...))printf",
96081137057abc6b2c32eb077bde7459f46544f7d8Johnny Chen                    substrs = ['(int (*)(const char *, ...)) $1 = '],
97081137057abc6b2c32eb077bde7459f46544f7d8Johnny Chen                    patterns = regexps)
986c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan
996c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan        self.expect("expr $1(\"Hello world\\n\")",
1006c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan                    startstr = '(int) $2 = 12')
101fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
102fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chenif __name__ == '__main__':
10388f8304a5b7864ce3c6966bc0250fa3b7069aef0Johnny Chen    import atexit
104fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen    lldb.SBDebugger.Initialize()
10588f8304a5b7864ce3c6966bc0250fa3b7069aef0Johnny Chen    atexit.register(lambda: lldb.SBDebugger.Terminate())
10675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen    unittest2.main()
107