1"""Test the SBCommandInterpreter APIs."""
2
3import os
4import unittest2
5import lldb
6import pexpect
7from lldbtest import *
8
9class CommandInterpreterAPICase(TestBase):
10
11    mydir = os.path.join("python_api", "interpreter")
12
13    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
14    @python_api_test
15    @dsym_test
16    def test_with_dsym_and_process_launch_api(self):
17        """Test the SBCommandInterpreter APIs."""
18        self.buildDsym()
19        self.command_interpreter_api()
20
21    @python_api_test
22    @dwarf_test
23    def test_with_dwarf_and_process_launch_api(self):
24        """Test the SBCommandInterpreter APIs."""
25        self.buildDwarf()
26        self.command_interpreter_api()
27
28    def setUp(self):
29        # Call super's setUp().
30        TestBase.setUp(self)
31        # Find the line number to break on inside main.cpp.
32        self.line = line_number('main.c', 'Hello world.')
33
34    def command_interpreter_api(self):
35        """Test the SBCommandInterpreter APIs."""
36        exe = os.path.join(os.getcwd(), "a.out")
37
38        # Create a target by the debugger.
39        target = self.dbg.CreateTarget(exe)
40        self.assertTrue(target, VALID_TARGET)
41
42        # Retrieve the associated command interpreter from our debugger.
43        ci = self.dbg.GetCommandInterpreter()
44        self.assertTrue(ci, VALID_COMMAND_INTERPRETER)
45
46        # Exercise some APIs....
47
48        self.assertTrue(ci.HasCommands())
49        self.assertTrue(ci.HasAliases())
50        self.assertTrue(ci.HasAliasOptions())
51        self.assertTrue(ci.CommandExists("breakpoint"))
52        self.assertTrue(ci.CommandExists("target"))
53        self.assertTrue(ci.CommandExists("platform"))
54        self.assertTrue(ci.AliasExists("file"))
55        self.assertTrue(ci.AliasExists("run"))
56        self.assertTrue(ci.AliasExists("bt"))
57
58        res = lldb.SBCommandReturnObject()
59        ci.HandleCommand("breakpoint set -f main.c -l %d" % self.line, res)
60        self.assertTrue(res.Succeeded())
61        ci.HandleCommand("process launch", res)
62        self.assertTrue(res.Succeeded())
63
64        # Boundary conditions should not crash lldb!
65        self.assertFalse(ci.CommandExists(None))
66        self.assertFalse(ci.AliasExists(None))
67        ci.HandleCommand(None, res)
68        self.assertFalse(res.Succeeded())
69        res.AppendMessage("Just appended a message.")
70        res.AppendMessage(None)
71        if self.TraceOn():
72            print res
73
74        process = ci.GetProcess()
75        self.assertTrue(process)
76
77        import lldbutil
78        if process.GetState() != lldb.eStateStopped:
79            self.fail("Process should be in the 'stopped' state, "
80                      "instead the actual state is: '%s'" %
81                      lldbutil.state_type_to_str(process.GetState()))
82
83        if self.TraceOn():
84            lldbutil.print_stacktraces(process)
85
86
87if __name__ == '__main__':
88    import atexit
89    lldb.SBDebugger.Initialize()
90    atexit.register(lambda: lldb.SBDebugger.Terminate())
91    unittest2.main()
92