aboutsummaryrefslogtreecommitdiff
path: root/lldb/test/API/python_api/interpreter/TestCommandInterpreterAPI.py
blob: 1029bdc3096d04cb45502266bd1c6f0ad626a90e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
"""Test the SBCommandInterpreter APIs."""

import json
import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil


class CommandInterpreterAPICase(TestBase):
    NO_DEBUG_INFO_TESTCASE = True

    def setUp(self):
        # Call super's setUp().
        TestBase.setUp(self)
        # Find the line number to break on inside main.cpp.
        self.line = line_number("main.c", "Hello world.")

    def buildAndCreateTarget(self):
        self.build()
        exe = self.getBuildArtifact("a.out")

        # Create a target by the debugger.
        target = self.dbg.CreateTarget(exe)
        self.assertTrue(target, VALID_TARGET)

        # Retrieve the associated command interpreter from our debugger.
        ci = self.dbg.GetCommandInterpreter()
        self.assertTrue(ci, VALID_COMMAND_INTERPRETER)
        return ci

    def test_with_process_launch_api(self):
        """Test the SBCommandInterpreter APIs."""
        ci = self.buildAndCreateTarget()

        # Exercise some APIs....

        self.assertTrue(ci.HasCommands())
        self.assertTrue(ci.HasAliases())
        self.assertTrue(ci.HasAliasOptions())
        self.assertTrue(ci.CommandExists("breakpoint"))
        self.assertTrue(ci.CommandExists("target"))
        self.assertTrue(ci.CommandExists("platform"))
        self.assertTrue(ci.AliasExists("file"))
        self.assertTrue(ci.AliasExists("run"))
        self.assertTrue(ci.AliasExists("bt"))

        res = lldb.SBCommandReturnObject()
        ci.HandleCommand("breakpoint set -f main.c -l %d" % self.line, res)
        self.assertTrue(res.Succeeded())
        ci.HandleCommand("process launch", res)
        self.assertTrue(res.Succeeded())

        # Boundary conditions should not crash lldb!
        self.assertFalse(ci.CommandExists(None))
        self.assertFalse(ci.AliasExists(None))
        ci.HandleCommand(None, res)
        self.assertFalse(res.Succeeded())
        res.AppendMessage("Just appended a message.")
        res.AppendMessage(None)
        if self.TraceOn():
            print(res)

        process = ci.GetProcess()
        self.assertTrue(process)

        import lldbsuite.test.lldbutil as lldbutil

        if process.GetState() != lldb.eStateStopped:
            self.fail(
                "Process should be in the 'stopped' state, "
                "instead the actual state is: '%s'"
                % lldbutil.state_type_to_str(process.GetState())
            )

        if self.TraceOn():
            lldbutil.print_stacktraces(process)

    def test_command_output(self):
        """Test command output handling."""
        ci = self.dbg.GetCommandInterpreter()
        self.assertTrue(ci, VALID_COMMAND_INTERPRETER)

        # Test that a command which produces no output returns "" instead of
        # None.
        res = lldb.SBCommandReturnObject()
        ci.HandleCommand("settings set use-color false", res)
        self.assertTrue(res.Succeeded())
        self.assertIsNotNone(res.GetOutput())
        self.assertEqual(res.GetOutput(), "")
        self.assertIsNotNone(res.GetError())
        self.assertEqual(res.GetError(), "")

    def getTranscriptAsPythonObject(self, ci):
        """Retrieve the transcript and convert it into a Python object"""
        structured_data = ci.GetTranscript()
        self.assertTrue(structured_data.IsValid())

        stream = lldb.SBStream()
        self.assertTrue(stream)

        error = structured_data.GetAsJSON(stream)
        self.assertSuccess(error)

        return json.loads(stream.GetData())

    def test_get_transcript(self):
        """Test structured transcript generation and retrieval."""
        ci = self.buildAndCreateTarget()
        self.assertTrue(ci, VALID_COMMAND_INTERPRETER)

        # Make sure the "save-transcript" setting is on
        self.runCmd("settings set interpreter.save-transcript true")

        # Send a few commands through the command interpreter.
        #
        # Using `ci.HandleCommand` because some commands will fail so that we
        # can test the "error" field in the saved transcript.
        res = lldb.SBCommandReturnObject()
        ci.HandleCommand("version", res)
        ci.HandleCommand("an-unknown-command", res)
        ci.HandleCommand("br s -f main.c -l %d" % self.line, res)
        ci.HandleCommand("p a", res)
        ci.HandleCommand("statistics dump", res)
        total_number_of_commands = 6

        # Get transcript as python object
        transcript = self.getTranscriptAsPythonObject(ci)

        # All commands should have expected fields.
        for command in transcript:
            self.assertIn("command", command)
            # Unresolved commands don't have "commandName"/"commandArguments".
            # We will validate these fields below, instead of here.
            self.assertIn("output", command)
            self.assertIn("error", command)
            self.assertIn("durationInSeconds", command)
            self.assertIn("timestampInEpochSeconds", command)

        # The following validates individual commands in the transcript.
        #
        # Notes:
        # 1. Some of the asserts rely on the exact output format of the
        #    commands. Hopefully we are not changing them any time soon.
        # 2. We are removing the time-related fields from each command, so
        #    that some of the validations below can be easier / more readable.
        for command in transcript:
            del command["durationInSeconds"]
            del command["timestampInEpochSeconds"]

        # (lldb) version
        self.assertEqual(transcript[0]["command"], "version")
        self.assertEqual(transcript[0]["commandName"], "version")
        self.assertEqual(transcript[0]["commandArguments"], "")
        self.assertIn("lldb version", transcript[0]["output"])
        self.assertEqual(transcript[0]["error"], "")

        # (lldb) an-unknown-command
        self.assertEqual(transcript[1],
            {
                "command": "an-unknown-command",
                # Unresolved commands don't have "commandName"/"commandArguments"
                "output": "",
                "error": "error: 'an-unknown-command' is not a valid command.\n",
            })

        # (lldb) br s -f main.c -l <line>
        self.assertEqual(transcript[2]["command"], "br s -f main.c -l %d" % self.line)
        self.assertEqual(transcript[2]["commandName"], "breakpoint set")
        self.assertEqual(
            transcript[2]["commandArguments"], "-f main.c -l %d" % self.line
        )
        # Breakpoint 1: where = a.out`main + 29 at main.c:5:3, address = 0x0000000100000f7d
        self.assertIn("Breakpoint 1: where = a.out`main ", transcript[2]["output"])
        self.assertEqual(transcript[2]["error"], "")

        # (lldb) p a
        self.assertEqual(transcript[3],
            {
                "command": "p a",
                "commandName": "dwim-print",
                "commandArguments": "-- a",
                "output": "",
                "error": "error: <user expression 0>:1:1: use of undeclared identifier 'a'\n    1 | a\n      | ^\n",
            })

        # (lldb) statistics dump
        self.assertEqual(transcript[4]["command"], "statistics dump")
        self.assertEqual(transcript[4]["commandName"], "statistics dump")
        self.assertEqual(transcript[4]["commandArguments"], "")
        self.assertEqual(transcript[4]["error"], "")
        statistics_dump = json.loads(transcript[4]["output"])
        # Dump result should be valid JSON
        self.assertTrue(statistics_dump is not json.JSONDecodeError)
        # Dump result should contain expected fields
        self.assertIn("commands", statistics_dump)
        self.assertIn("memory", statistics_dump)
        self.assertIn("modules", statistics_dump)
        self.assertIn("targets", statistics_dump)

    def test_save_transcript_setting_default(self):
        ci = self.dbg.GetCommandInterpreter()
        self.assertTrue(ci, VALID_COMMAND_INTERPRETER)

        # The setting's default value should be "false"
        self.runCmd("settings show interpreter.save-transcript", "interpreter.save-transcript (boolean) = false\n")

    def test_save_transcript_setting_off(self):
        ci = self.dbg.GetCommandInterpreter()
        self.assertTrue(ci, VALID_COMMAND_INTERPRETER)

        # Make sure the setting is off
        self.runCmd("settings set interpreter.save-transcript false")

        # The transcript should be empty after running a command
        self.runCmd("version")
        transcript = self.getTranscriptAsPythonObject(ci)
        self.assertEqual(transcript, [])

    def test_save_transcript_setting_on(self):
        ci = self.dbg.GetCommandInterpreter()
        self.assertTrue(ci, VALID_COMMAND_INTERPRETER)

        # Make sure the setting is on
        self.runCmd("settings set interpreter.save-transcript true")

        # The transcript should contain one item after running a command
        self.runCmd("version")
        transcript = self.getTranscriptAsPythonObject(ci)
        self.assertEqual(len(transcript), 1)
        self.assertEqual(transcript[0]["command"], "version")

    def test_get_transcript_returns_copy(self):
        """
        Test that the returned structured data is *at least* a shallow copy.

        We believe that a deep copy *is* performed in `SBCommandInterpreter::GetTranscript`.
        However, the deep copy cannot be tested and doesn't need to be tested,
        because there is no logic in the command interpreter to modify a
        transcript item (representing a command) after it has been returned.
        """
        ci = self.dbg.GetCommandInterpreter()
        self.assertTrue(ci, VALID_COMMAND_INTERPRETER)

        # Make sure the setting is on
        self.runCmd("settings set interpreter.save-transcript true")

        # Run commands and get the transcript as structured data
        self.runCmd("version")
        structured_data_1 = ci.GetTranscript()
        self.assertTrue(structured_data_1.IsValid())
        self.assertEqual(structured_data_1.GetSize(), 1)
        self.assertEqual(structured_data_1.GetItemAtIndex(0).GetValueForKey("command").GetStringValue(100), "version")

        # Run some more commands and get the transcript as structured data again
        self.runCmd("help")
        structured_data_2 = ci.GetTranscript()
        self.assertTrue(structured_data_2.IsValid())
        self.assertEqual(structured_data_2.GetSize(), 2)
        self.assertEqual(structured_data_2.GetItemAtIndex(0).GetValueForKey("command").GetStringValue(100), "version")
        self.assertEqual(structured_data_2.GetItemAtIndex(1).GetValueForKey("command").GetStringValue(100), "help")

        # Now, the first structured data should remain unchanged
        self.assertTrue(structured_data_1.IsValid())
        self.assertEqual(structured_data_1.GetSize(), 1)
        self.assertEqual(structured_data_1.GetItemAtIndex(0).GetValueForKey("command").GetStringValue(100), "version")