69 lines · python
1import lldb2from lldbsuite.test.decorators import *3from lldbsuite.test.lldbtest import *4from lldbsuite.test import lldbutil5 6 7class CommandInterepterPrintCallbackTest(TestBase):8 NO_DEBUG_INFO_TESTCASE = True9 10 def run_command_interpreter_with_output_file(self, out_filename, input_str):11 with open(out_filename, "w") as f:12 self.dbg.SetOutputFileHandle(f, False)13 self.dbg.SetInputString(input_str)14 opts = lldb.SBCommandInterpreterRunOptions()15 self.dbg.RunCommandInterpreter(True, False, opts, 0, False, False)16 17 def test_command_interpreter_print_callback(self):18 """Test the command interpreter print callback."""19 self.build()20 exe = self.getBuildArtifact("a.out")21 22 target = self.dbg.CreateTarget(exe)23 self.assertTrue(target, VALID_TARGET)24 25 lldbutil.run_to_source_breakpoint(26 self, "// Break here", lldb.SBFileSpec("main.c")27 )28 29 out_filename = self.getBuildArtifact("output")30 ci = self.dbg.GetCommandInterpreter()31 called = False32 33 # The string we'll be looking for in the command output.34 needle = "Show a list of all debugger commands"35 36 # Test registering a callback that handles the printing. Make sure the37 # result is passed to the callback and that we don't print the result.38 def handling_callback(return_object):39 nonlocal called40 called = True41 self.assertEqual("help help", return_object.GetCommand())42 self.assertIn(needle, return_object.GetOutput())43 return lldb.eCommandReturnObjectPrintCallbackHandled44 45 ci.SetPrintCallback(handling_callback)46 self.assertFalse(called)47 self.run_command_interpreter_with_output_file(out_filename, "help help\n")48 with open(out_filename, "r") as f:49 self.assertNotIn(needle, f.read())50 51 # Test registering a callback that defers the printing to lldb. Make52 # sure the result is passed to the callback and that the result is53 # printed by lldb.54 def non_handling_callback(return_object):55 nonlocal called56 called = True57 self.assertEqual("he help", return_object.GetCommand())58 self.assertIn(needle, return_object.GetOutput())59 return lldb.eCommandReturnObjectPrintCallbackSkipped60 61 called = False62 ci.SetPrintCallback(non_handling_callback)63 self.assertFalse(called)64 self.run_command_interpreter_with_output_file(out_filename, "he help\n")65 self.assertTrue(called)66 67 with open(out_filename, "r") as f:68 self.assertIn(needle, f.read())69