TestFunctionTypes.py revision fce55e0f97822a11a92cacb556c38bf69c79af25
1fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen"""Test variable with function ptr type and that break on the function works."""
2fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
3fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chenimport os, time
4fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chenimport lldb
5fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chenimport unittest
6fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
7fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chenmain = False
8fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
9fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chenclass TestClassTypes(unittest.TestCase):
10fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
11fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen    def setUp(self):
12fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        global main
13fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
14fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        # Save old working directory.
15fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.oldcwd = os.getcwd()
16fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        # Change current working directory if ${LLDB_TEST} is defined.
17fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        if ("LLDB_TEST" in os.environ):
18fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen            os.chdir(os.path.join(os.environ["LLDB_TEST"], "function_types"));
19fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.dbg = lldb.SBDebugger.Create() if main else lldb.DBG
20fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        if not self.dbg.IsValid():
21fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen            raise Exception('Invalid debugger instance')
22fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.dbg.SetAsync(False)
23fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.ci = self.dbg.GetCommandInterpreter()
24fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        if not self.ci:
25fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen            raise Exception('Could not get the command interpreter')
26fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
27fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen    def tearDown(self):
28fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        # Restore old working directory.
29fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        os.chdir(self.oldcwd)
30fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        del self.dbg
31fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
32fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen    def test_function_types(self):
33fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        """Test 'callback' has function ptr type, then break on the function."""
34fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        res = lldb.SBCommandReturnObject()
35fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        exe = os.path.join(os.getcwd(), "a.out")
36fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.ci.HandleCommand("file " + exe, res)
37fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.assertTrue(res.Succeeded())
38fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
39fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        # Break inside the main.
40fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.ci.HandleCommand("breakpoint set -f main.c -l 21", res)
41fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.assertTrue(res.Succeeded())
42fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.assertTrue(res.GetOutput().startswith(
43fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen            "Breakpoint created: 1: file ='main.c', line = 21, locations = 1"))
44fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
45fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.ci.HandleCommand("run", res)
46fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        time.sleep(0.1)
47fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.assertTrue(res.Succeeded())
48fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
49fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        # The stop reason of the thread should be breakpoint.
50fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.ci.HandleCommand("thread list", res)
51fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        print "thread list ->", res.GetOutput()
52fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.assertTrue(res.Succeeded())
53fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.assertTrue(res.GetOutput().find('state is Stopped') and
54fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen                        res.GetOutput().find('stop reason = breakpoint'))
55fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
56fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        # The breakpoint should have a hit count of 1.
57fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.ci.HandleCommand("breakpoint list", res)
58fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.assertTrue(res.Succeeded())
59fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.assertTrue(res.GetOutput().find(' resolved, hit count = 1'))
60fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
61fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        # Check that the 'callback' variable display properly.
62fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.ci.HandleCommand("variable list callback", res);
63fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.assertTrue(res.Succeeded())
64fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        output = res.GetOutput()
65fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.assertTrue(output.startswith('(int (*)(char const *)) callback ='))
66fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
67fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        # And that we can break on the callback function.
68fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.ci.HandleCommand("breakpoint set -n string_not_empty", res);
69fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.assertTrue(res.Succeeded())
70fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.ci.HandleCommand("continue", res)
71fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.assertTrue(res.Succeeded())
72fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
73fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        # Check that we do indeed stop on the string_not_empty function.
74fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.ci.HandleCommand("process status", res)
75fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.assertTrue(res.Succeeded())
76fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        output = res.GetOutput()
77fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        #print "process status =", output
78fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.assertTrue(output.find('where = a.out`string_not_empty') and
79fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen                        output.find('main.c:12') and
80fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen                        output.find('stop reason = breakpoint'))
81fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
82fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.ci.HandleCommand("continue", res)
83fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen        self.assertTrue(res.Succeeded())
84fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
85fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen
86fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chenif __name__ == '__main__':
87fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen    lldb.SBDebugger.Initialize()
88fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen    main = True
89fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen    unittest.main()
90fce55e0f97822a11a92cacb556c38bf69c79af25Johnny Chen    lldb.SBDebugger.Terminate()
91