xref: /llvm-project/lldb/test/API/python_api/interpreter/TestCommandInterpreterAPI.py (revision 4cc8f2a017c76af25234afc7c380550e9c93135c)
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    NO_DEBUG_INFO_TESTCASE = True
14
15    def setUp(self):
16        # Call super's setUp().
17        TestBase.setUp(self)
18        # Find the line number to break on inside main.cpp.
19        self.line = line_number('main.c', 'Hello world.')
20
21    def test_with_process_launch_api(self):
22        """Test the SBCommandInterpreter APIs."""
23        self.build()
24        exe = self.getBuildArtifact("a.out")
25
26        # Create a target by the debugger.
27        target = self.dbg.CreateTarget(exe)
28        self.assertTrue(target, VALID_TARGET)
29
30        # Retrieve the associated command interpreter from our debugger.
31        ci = self.dbg.GetCommandInterpreter()
32        self.assertTrue(ci, VALID_COMMAND_INTERPRETER)
33
34        # Exercise some APIs....
35
36        self.assertTrue(ci.HasCommands())
37        self.assertTrue(ci.HasAliases())
38        self.assertTrue(ci.HasAliasOptions())
39        self.assertTrue(ci.CommandExists("breakpoint"))
40        self.assertTrue(ci.CommandExists("target"))
41        self.assertTrue(ci.CommandExists("platform"))
42        self.assertTrue(ci.AliasExists("file"))
43        self.assertTrue(ci.AliasExists("run"))
44        self.assertTrue(ci.AliasExists("bt"))
45
46        res = lldb.SBCommandReturnObject()
47        ci.HandleCommand("breakpoint set -f main.c -l %d" % self.line, res)
48        self.assertTrue(res.Succeeded())
49        ci.HandleCommand("process launch", res)
50        self.assertTrue(res.Succeeded())
51
52        # Boundary conditions should not crash lldb!
53        self.assertFalse(ci.CommandExists(None))
54        self.assertFalse(ci.AliasExists(None))
55        ci.HandleCommand(None, res)
56        self.assertFalse(res.Succeeded())
57        res.AppendMessage("Just appended a message.")
58        res.AppendMessage(None)
59        if self.TraceOn():
60            print(res)
61
62        process = ci.GetProcess()
63        self.assertTrue(process)
64
65        import lldbsuite.test.lldbutil as lldbutil
66        if process.GetState() != lldb.eStateStopped:
67            self.fail("Process should be in the 'stopped' state, "
68                      "instead the actual state is: '%s'" %
69                      lldbutil.state_type_to_str(process.GetState()))
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.assertEquals(res.GetOutput(), "")
86        self.assertIsNotNone(res.GetError())
87        self.assertEquals(res.GetError(), "")
88