File: TestCommandInterepterPrintCallback.py

package info (click to toggle)
llvm-toolchain-snapshot 1%3A22~%2B%2B20250731080150%2Bbe449d6b6587-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,250,472 kB
  • sloc: cpp: 7,641,756; ansic: 1,439,220; asm: 1,072,591; python: 253,178; f90: 95,173; objc: 70,722; lisp: 44,365; pascal: 18,720; sh: 10,051; ml: 5,111; perl: 4,720; awk: 3,523; makefile: 3,397; javascript: 2,272; xml: 892; fortran: 783
file content (68 lines) | stat: -rw-r--r-- 2,703 bytes parent folder | download | duplicates (10)
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
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.assertEqual("help help", return_object.GetCommand())
            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.assertEqual("he help", return_object.GetCommand())
            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, "he help\n")
        self.assertTrue(called)

        with open(out_filename, "r") as f:
            self.assertIn(needle, f.read())