TestFunctionTypes.py revision c01c05c7a8a198761fb917308dd53e6e143384ad
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")
13d9ed552026a5d2842d95b2852f33346e867e9799Johnny Chen    def test_with_dsym(self):
14d9ed552026a5d2842d95b2852f33346e867e9799Johnny Chen        """Test 'callback' has function ptr type, then break on the function."""
15d9ed552026a5d2842d95b2852f33346e867e9799Johnny Chen        self.buildDsym()
16d9ed552026a5d2842d95b2852f33346e867e9799Johnny Chen        self.function_types()
17d9ed552026a5d2842d95b2852f33346e867e9799Johnny Chen
18d9ed552026a5d2842d95b2852f33346e867e9799Johnny Chen    def test_with_dwarf(self):
19d9ed552026a5d2842d95b2852f33346e867e9799Johnny Chen        """Test 'callback' has function ptr type, then break on the function."""
20d9ed552026a5d2842d95b2852f33346e867e9799Johnny Chen        self.buildDwarf()
21d9ed552026a5d2842d95b2852f33346e867e9799Johnny Chen        self.function_types()
226c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan
236c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
246c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan    def test_pointers_with_dsym(self):
256c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan        """Test that a function pointer to 'printf' works and can be called."""
266c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan        self.buildDsym()
27c01c05c7a8a198761fb917308dd53e6e143384adSean Callanan        self.function_pointers()
286c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan
296c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan    def test_pointers_with_dwarf(self):
306c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan        """Test that a function pointer to 'printf' works and can be called."""
316c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan        self.buildDwarf()
32c01c05c7a8a198761fb917308dd53e6e143384adSean Callanan        self.function_pointers()
33d9ed552026a5d2842d95b2852f33346e867e9799Johnny Chen
34143914211a7e009c90a7d88e7584513d7a4af67fJohnny Chen    def setUp(self):
350910eb32b94bd67b0357d34d0951aad197742569Johnny Chen        # Call super's setUp().
360910eb32b94bd67b0357d34d0951aad197742569Johnny Chen        TestBase.setUp(self)
37143914211a7e009c90a7d88e7584513d7a4af67fJohnny Chen        # Find the line number to break inside main().
38143914211a7e009c90a7d88e7584513d7a4af67fJohnny Chen        self.line = line_number('main.c', '// Set break point at this line.')
39143914211a7e009c90a7d88e7584513d7a4af67fJohnny Chen
406c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan    def runToBreakpoint(self):
41fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        exe = os.path.join(os.getcwd(), "a.out")
424f995f0718299696719089084a622835b6a7b4b8Johnny Chen        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
436c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan
44fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        # Break inside the main.
45143914211a7e009c90a7d88e7584513d7a4af67fJohnny Chen        self.expect("breakpoint set -f main.c -l %d" % self.line,
46143914211a7e009c90a7d88e7584513d7a4af67fJohnny Chen                    BREAKPOINT_CREATED,
476c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan                    startstr = "Breakpoint created: 1: file ='main.c', line = %d, locations = 1" %
486c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan                    self.line)
496c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan
501bb9f9a3917c7ac7228d0a81a0ff8a225165800aJohnny Chen        self.runCmd("run", RUN_SUCCEEDED)
516c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan
52fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        # The stop reason of the thread should be breakpoint.
534f995f0718299696719089084a622835b6a7b4b8Johnny Chen        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
546c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan                    substrs = ['stopped',
556c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan                               'stop reason = breakpoint'])
566c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan
57fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        # The breakpoint should have a hit count of 1.
5841950cc77813637e6c67b069e4ad2faf8c5f6fa7Caroline Tice        self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
596c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan                    substrs = [' resolved, hit count = 1'])
606c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan
616c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan    def function_types(self):
626c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan        """Test 'callback' has function ptr type, then break on the function."""
636c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan
646c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan        self.runToBreakpoint()
65fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
66fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        # Check that the 'callback' variable display properly.
6710de7d1db3ec782ea2ccda1f39c0a40b9c301594Jim Ingham        self.expect("frame variable -T callback", VARIABLES_DISPLAYED_CORRECTLY,
68397636ec9b675f4d917765400419d6b4805bb8a3Johnny Chen            startstr = '(int (*)(const char *)) callback =')
69fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
70fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        # And that we can break on the callback function.
714f995f0718299696719089084a622835b6a7b4b8Johnny Chen        self.runCmd("breakpoint set -n string_not_empty", BREAKPOINT_CREATED)
724f995f0718299696719089084a622835b6a7b4b8Johnny Chen        self.runCmd("continue")
73fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
74fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        # Check that we do indeed stop on the string_not_empty function.
754f995f0718299696719089084a622835b6a7b4b8Johnny Chen        self.expect("process status", STOPPED_DUE_TO_BREAKPOINT,
7659ea45fc85deacf0b8ca8250f116b7cc7b3691a5Johnny Chen            substrs = ['a.out`string_not_empty',
774f995f0718299696719089084a622835b6a7b4b8Johnny Chen                       'stop reason = breakpoint'])
78fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
796c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan    def function_pointers(self):
806c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan        """Test that a function pointer to 'printf' works and can be called."""
816c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan
826c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan        self.runToBreakpoint()
836c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan
846c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan        self.expect("expr string_not_empty",
856c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan                    substrs = ['(int (*)(const char *)) $0 = ', '(a.out`'])
866c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan
87081137057abc6b2c32eb077bde7459f46544f7d8Johnny Chen        if sys.platform.startswith("darwin"):
88081137057abc6b2c32eb077bde7459f46544f7d8Johnny Chen            regexps = ['lib.*\.dylib`printf']
89081137057abc6b2c32eb077bde7459f46544f7d8Johnny Chen        else:
90081137057abc6b2c32eb077bde7459f46544f7d8Johnny Chen            regexps = ['printf']
916c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan        self.expect("expr (int (*)(const char*, ...))printf",
92081137057abc6b2c32eb077bde7459f46544f7d8Johnny Chen                    substrs = ['(int (*)(const char *, ...)) $1 = '],
93081137057abc6b2c32eb077bde7459f46544f7d8Johnny Chen                    patterns = regexps)
946c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan
956c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan        self.expect("expr $1(\"Hello world\\n\")",
966c98ba7d2b7f5e3381cbba5f24e97673c47d109aSean Callanan                    startstr = '(int) $2 = 12')
97fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
98fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chenif __name__ == '__main__':
9988f8304a5b7864ce3c6966bc0250fa3b7069aef0Johnny Chen    import atexit
100fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen    lldb.SBDebugger.Initialize()
10188f8304a5b7864ce3c6966bc0250fa3b7069aef0Johnny Chen    atexit.register(lambda: lldb.SBDebugger.Terminate())
10275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen    unittest2.main()
103