Files
clang-p2996/lldb/test/API/python_api/interpreter_callback/TestCommandInterepterPrintCallback.py
Jonas Devlieghere 97f6e53386 [lldb] Support CommandInterpreter print callbacks (#125006)
Xcode uses a pseudoterminal for the debugger console.

- The upside of this apporach is that it means that it can rely on
LLDB's IOHandlers for multiline and script input.
- The downside of this approach is that the command output is printed to
the PTY and you don't get a SBCommandReturnObject. Adrian added support
for inline diagnostics (#110901) and we'd like to access those from the
IDE.

This patch adds support for registering a callback in the command
interpreter that gives access to the `(SB)CommandReturnObject` right
before it will be printed. The callback implementation can choose
whether it likes to handle printing the result or defer to lldb. If the
callback indicated it handled the result, the command interpreter will
skip printing the result.

We considered a few other alternatives to solve this problem:

- The most obvious one is using `HandleCommand`, which returns a
`SBCommandReturnObject`. The problem with this approach is the multiline
input mentioned above. We would need a way to tell the IDE that it
should expect multiline input, which isn't known until LLDB starts
handling the command.
- To address the multiline issue,we considered exposing (some of the)
IOHandler machinery through the SB API. To solve this particular issue,
that would require reimplementing a ton of logic that already exists
today in the CommandInterpeter. Furthermore that seems like overkill
compared to the proposed solution.

rdar://141254310
2025-02-04 09:01:08 -08:00

67 lines
2.5 KiB
Python

import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil
class CommandInterepterPrintCallbackTest(TestBase):
NO_DEBUG_INFO_TESTCASE = True
def run_command_interpreter_with_output_file(self, out_filename, input_str):
with open(out_filename, "w") as f:
self.dbg.SetOutputFileHandle(f, False)
self.dbg.SetInputString(input_str)
opts = lldb.SBCommandInterpreterRunOptions()
self.dbg.RunCommandInterpreter(True, False, opts, 0, False, False)
def test_command_interpreter_print_callback(self):
"""Test the command interpreter print callback."""
self.build()
exe = self.getBuildArtifact("a.out")
target = self.dbg.CreateTarget(exe)
self.assertTrue(target, VALID_TARGET)
lldbutil.run_to_source_breakpoint(
self, "// Break here", lldb.SBFileSpec("main.c")
)
out_filename = self.getBuildArtifact("output")
ci = self.dbg.GetCommandInterpreter()
called = False
# The string we'll be looking for in the command output.
needle = "Show a list of all debugger commands"
# Test registering a callback that handles the printing. Make sure the
# result is passed to the callback and that we don't print the result.
def handling_callback(return_object):
nonlocal called
called = True
self.assertIn(needle, return_object.GetOutput())
return lldb.eCommandReturnObjectPrintCallbackHandled
ci.SetPrintCallback(handling_callback)
self.assertFalse(called)
self.run_command_interpreter_with_output_file(out_filename, "help help\n")
with open(out_filename, "r") as f:
self.assertNotIn(needle, f.read())
# Test registering a callback that defers the printing to lldb. Make
# sure the result is passed to the callback and that the result is
# printed by lldb.
def non_handling_callback(return_object):
nonlocal called
called = True
self.assertIn(needle, return_object.GetOutput())
return lldb.eCommandReturnObjectPrintCallbackSkipped
called = False
ci.SetPrintCallback(non_handling_callback)
self.assertFalse(called)
self.run_command_interpreter_with_output_file(out_filename, "help help\n")
self.assertTrue(called)
with open(out_filename, "r") as f:
self.assertIn(needle, f.read())