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