xref: /llvm-project/lldb/test/API/python_api/interpreter/TestCommandInterpreterAPI.py (revision 05d7d6949c7cd3f1566d4c8394fa59160a7ffd05)
1"""Test the SBCommandInterpreter APIs."""
2
3from __future__ import print_function
4
5
6import lldb
7from lldbsuite.test.decorators import *
8from lldbsuite.test.lldbtest import *
9from lldbsuite.test import lldbutil
10
11
12class CommandInterpreterAPICase(TestBase):
13
14    mydir = TestBase.compute_mydir(__file__)
15    NO_DEBUG_INFO_TESTCASE = True
16
17    def setUp(self):
18        # Call super's setUp().
19        TestBase.setUp(self)
20        # Find the line number to break on inside main.cpp.
21        self.line = line_number('main.c', 'Hello world.')
22
23    def test_with_process_launch_api(self):
24        """Test the SBCommandInterpreter APIs."""
25        self.build()
26        exe = self.getBuildArtifact("a.out")
27
28        # Create a target by the debugger.
29        target = self.dbg.CreateTarget(exe)
30        self.assertTrue(target, VALID_TARGET)
31
32        # Retrieve the associated command interpreter from our debugger.
33        ci = self.dbg.GetCommandInterpreter()
34        self.assertTrue(ci, VALID_COMMAND_INTERPRETER)
35
36        # Exercise some APIs....
37
38        self.assertTrue(ci.HasCommands())
39        self.assertTrue(ci.HasAliases())
40        self.assertTrue(ci.HasAliasOptions())
41        self.assertTrue(ci.CommandExists("breakpoint"))
42        self.assertTrue(ci.CommandExists("target"))
43        self.assertTrue(ci.CommandExists("platform"))
44        self.assertTrue(ci.AliasExists("file"))
45        self.assertTrue(ci.AliasExists("run"))
46        self.assertTrue(ci.AliasExists("bt"))
47
48        res = lldb.SBCommandReturnObject()
49        ci.HandleCommand("breakpoint set -f main.c -l %d" % self.line, res)
50        self.assertTrue(res.Succeeded())
51        ci.HandleCommand("process launch", res)
52        self.assertTrue(res.Succeeded())
53
54        # Boundary conditions should not crash lldb!
55        self.assertFalse(ci.CommandExists(None))
56        self.assertFalse(ci.AliasExists(None))
57        ci.HandleCommand(None, res)
58        self.assertFalse(res.Succeeded())
59        res.AppendMessage("Just appended a message.")
60        res.AppendMessage(None)
61        if self.TraceOn():
62            print(res)
63
64        process = ci.GetProcess()
65        self.assertTrue(process)
66
67        import lldbsuite.test.lldbutil as lldbutil
68        if process.GetState() != lldb.eStateStopped:
69            self.fail("Process should be in the 'stopped' state, "
70                      "instead the actual state is: '%s'" %
71                      lldbutil.state_type_to_str(process.GetState()))
72
73        if self.TraceOn():
74            lldbutil.print_stacktraces(process)
75
76    def test_command_output(self):
77        """Test command output handling."""
78        ci = self.dbg.GetCommandInterpreter()
79        self.assertTrue(ci, VALID_COMMAND_INTERPRETER)
80
81        # Test that a command which produces no output returns "" instead of
82        # None.
83        res = lldb.SBCommandReturnObject()
84        ci.HandleCommand("settings set use-color false", res)
85        self.assertTrue(res.Succeeded())
86        self.assertIsNotNone(res.GetOutput())
87        self.assertEquals(res.GetOutput(), "")
88        self.assertIsNotNone(res.GetError())
89        self.assertEquals(res.GetError(), "")
90