[Lldb-commits] [lldb] Add new Python API `SBCommandInterpreter::GetTranscript()` (PR #90703)

via lldb-commits lldb-commits at lists.llvm.org
Mon May 6 10:38:50 PDT 2024


================
@@ -85,3 +86,84 @@ def test_command_output(self):
         self.assertEqual(res.GetOutput(), "")
         self.assertIsNotNone(res.GetError())
         self.assertEqual(res.GetError(), "")
+
+    def test_structured_transcript(self):
+        """Test structured transcript generation and retrieval."""
+        # Get command interpreter and create a target
+        self.build()
+        exe = self.getBuildArtifact("a.out")
+
+        target = self.dbg.CreateTarget(exe)
+        self.assertTrue(target, VALID_TARGET)
+
+        ci = self.dbg.GetCommandInterpreter()
+        self.assertTrue(ci, VALID_COMMAND_INTERPRETER)
+
+        # Send a few commands through the command interpreter
+        res = lldb.SBCommandReturnObject()
+        ci.HandleCommand("version", res)
+        ci.HandleCommand("an-unknown-command", res)
+        ci.HandleCommand("breakpoint set -f main.c -l %d" % self.line, res)
+        ci.HandleCommand("r", res)
+        ci.HandleCommand("p a", res)
+        total_number_of_commands = 5
+
+        # Retrieve the transcript and convert it into a Python object
+        transcript = ci.GetTranscript()
+        self.assertTrue(transcript.IsValid())
+
+        stream = lldb.SBStream()
+        self.assertTrue(stream)
+
+        error = transcript.GetAsJSON(stream)
+        self.assertSuccess(error)
+
+        transcript = json.loads(stream.GetData())
+
+        # The transcript will contain a bunch of commands that are run
+        # automatically. We only want to validate for the ones that are
+        # listed above, hence trimming to the last parts.
+        transcript = transcript[-total_number_of_commands:]
----------------
royitaqi wrote:

Hi @bulbazord ,

> Or is there a reason there would be more? Perhaps from some setup code?
I have verified that that's indeed the case.

See the [full transcript](https://pastebin.com/Gxj1Lhid). I believe the commands that are before "version" are coming from [this general setup code](https://github.com/llvm/llvm-project/blob/main/lldb/packages/Python/lldbsuite/test/lldbtest.py#L744-L767)

So I think trimming make sense here, in order to isolate this test case from the setup code, which may be changed in the future. Let me add this to the comment so that it's clear to future readers.

https://github.com/llvm/llvm-project/pull/90703


More information about the lldb-commits mailing list