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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
"""
Test SBDebugger.InterruptRequested and SBCommandInterpreter.WasInterrupted.
"""
import lldb
import lldbsuite.test.lldbutil as lldbutil
from lldbsuite.test.lldbtest import *
import threading
import os
class TestDebuggerInterruption(TestBase):
"""This test runs a command that starts up, rendevous with the test thread
using threading barriers, then checks whether it has been interrupted.
The command's first argument is either 'interp' or 'debugger', to test
InterruptRequested and WasInterrupted respectively.
The command has two modes, interrupt and check, the former is the one that
waits for an interrupt. Then latter just returns whether an interrupt was
requested. We use the latter to make sure we took down the flag correctly."""
NO_DEBUG_INFO_TESTCASE = True
class CommandRunner(threading.Thread):
"""This class is for running a command, and for making a thread to run the command on.
It gets passed the test it is working on behalf of, and most of the important
objects come from the test."""
def __init__(self, test):
super().__init__()
self.test = test
def rendevous(self):
# We smuggle out barriers and event to the runner thread using thread local data:
import interruptible
interruptible.local_data = interruptible.BarrierContainer(
self.test.before_interrupt_barrier,
self.test.after_interrupt_barrier,
self.test.event,
)
class DirectCommandRunner(CommandRunner):
""" "This version runs a single command using HandleCommand."""
def __init__(self, test, command):
super().__init__(test)
self.command = command
def run(self):
self.rendevous()
result = self.test.dbg.GetCommandInterpreter().HandleCommand(
self.command, self.test.result
)
if self.test.result_barrier:
self.test.result_barrier.wait()
class CommandInterpreterRunner(CommandRunner):
"""This version runs the CommandInterpreter and feeds the command to it."""
def __init__(self, test):
super().__init__(test)
def run(self):
self.rendevous()
test = self.test
# We will use files for debugger input and output:
# First write down the command:
with open(test.getBuildArtifact(test.in_filename), "w") as f:
f.write(f"{test.command}\n")
# Now set the debugger's stdout & stdin to our files, and run
# the CommandInterpreter:
with open(test.out_filename, "w") as outf, open(
test.in_filename, "r"
) as inf:
outsbf = lldb.SBFile(outf.fileno(), "w", False)
orig_outf = test.dbg.GetOutputFile()
error = test.dbg.SetOutputFile(outsbf)
test.assertSuccess(error, "Could not set outfile")
insbf = lldb.SBFile(inf.fileno(), "r", False)
orig_inf = test.dbg.GetOutputFile()
error = test.dbg.SetInputFile(insbf)
test.assertSuccess(error, "Could not set infile")
options = lldb.SBCommandInterpreterRunOptions()
options.SetPrintResults(True)
options.SetEchoCommands(False)
test.dbg.RunCommandInterpreter(True, False, options, 0, False, False)
test.dbg.GetOutputFile().Flush()
error = test.dbg.SetOutputFile(orig_outf)
test.assertSuccess(error, "Restored outfile")
test.dbg.SetInputFile(orig_inf)
test.assertSuccess(error, "Restored infile")
def command_setup(self, args):
"""Insert our command, if needed. Then set up event and barriers if needed.
Then return the command to run."""
self.interp = self.dbg.GetCommandInterpreter()
self.command_name = "interruptible_command"
self.cmd_result = lldb.SBCommandReturnObject()
if not "check" in args:
self.event = threading.Event()
self.result_barrier = threading.Barrier(2, timeout=10)
self.before_interrupt_barrier = threading.Barrier(2, timeout=10)
self.after_interrupt_barrier = threading.Barrier(2, timeout=10)
else:
self.event = None
self.result_barrier = None
self.before_interrupt_barrier = None
self.after_interrupt_barrier = None
if not self.interp.UserCommandExists(self.command_name):
# Make the command we're going to use - it spins calling WasInterrupted:
cmd_filename = "interruptible"
cmd_filename = os.path.join(self.getSourceDir(), "interruptible.py")
self.runCmd(f"command script import {cmd_filename}")
cmd_string = f"command script add {self.command_name} --class interruptible.WelcomeCommand"
self.runCmd(cmd_string)
if len(args) == 0:
command = self.command_name
else:
command = self.command_name + " " + args
return command
def run_single_command(self, command):
# Now start up a thread to run the command:
self.result.Clear()
self.runner = TestDebuggerInterruption.DirectCommandRunner(self, command)
self.runner.start()
def start_command_interp(self):
self.runner = TestDebuggerInterruption.CommandInterpreterRunner(self)
self.runner.start()
def check_text(self, result_text, interrupted):
if interrupted:
self.assertIn(
"Command was interrupted", result_text, "Got the interrupted message"
)
else:
self.assertIn(
"Command was not interrupted",
result_text,
"Got the not interrupted message",
)
def gather_output(self):
# Now wait for the interrupt to interrupt the command:
self.runner.join(10.0)
finished = not self.runner.is_alive()
# Don't leave the runner thread stranded if the interrupt didn't work.
if not finished:
self.event.set()
self.runner.join(10.0)
self.assertTrue(finished, "We did finish the command")
def check_result(self, interrupted=True):
self.gather_output()
self.check_text(self.result.GetOutput(), interrupted)
def check_result_output(self, interrupted=True):
self.gather_output()
buffer = ""
# Okay, now open the file for reading, and read.
with open(self.out_filename, "r") as f:
buffer = f.read()
self.assertNotEqual(len(buffer), 0, "No command data")
self.check_text(buffer, interrupted)
def debugger_interrupt_test(self, use_interrupt_requested):
"""Test that debugger interruption interrupts a command
running directly through HandleCommand.
If use_interrupt_requested is true, we'll check that API,
otherwise we'll check WasInterrupted. They should both do
the same thing."""
if use_interrupt_requested:
command = self.command_setup("debugger")
else:
command = self.command_setup("interp")
self.result = lldb.SBCommandReturnObject()
self.run_single_command(command)
# Okay now wait till the command has gotten started to issue the interrupt:
self.before_interrupt_barrier.wait()
# I'm going to do it twice here to test that it works as a counter:
self.dbg.RequestInterrupt()
self.dbg.RequestInterrupt()
def cleanup():
self.dbg.CancelInterruptRequest()
self.addTearDownHook(cleanup)
# Okay, now set both sides going:
self.after_interrupt_barrier.wait()
# Check that the command was indeed interrupted. First rendevous
# after the runner thread had a chance to execute the command:
self.result_barrier.wait()
self.assertTrue(self.result.Succeeded(), "Our command succeeded")
result_output = self.result.GetOutput()
self.check_result(True)
# Do it again to make sure that the counter is counting:
self.dbg.CancelInterruptRequest()
command = self.command_setup("debugger")
self.run_single_command(command)
# This time we won't even get to run the command, since HandleCommand
# checks for the interrupt state on entry, so we don't wait on the command
# barriers.
self.result_barrier.wait()
# Again check that we were
self.assertFalse(self.result.Succeeded(), "Our command was not allowed to run")
error_output = self.result.GetError()
self.assertIn(
"... Interrupted", error_output, "Command was cut short by interrupt"
)
# Now take down the flag, and make sure that we aren't interrupted:
self.dbg.CancelInterruptRequest()
# Now make sure that we really did take down the flag:
command = self.command_setup("debugger check")
self.run_single_command(command)
result_output = self.result.GetOutput()
self.check_result(False)
def test_debugger_interrupt_use_dbg(self):
self.debugger_interrupt_test(True)
def test_debugger_interrupt_use_interp(self):
self.debugger_interrupt_test(False)
def test_interp_doesnt_interrupt_debugger(self):
"""Test that interpreter interruption does not interrupt a command
running directly through HandleCommand.
If use_interrupt_requested is true, we'll check that API,
otherwise we'll check WasInterrupted. They should both do
the same thing."""
command = self.command_setup("debugger poll")
self.result = lldb.SBCommandReturnObject()
self.run_single_command(command)
# Now raise the debugger interrupt flag. It will also interrupt the command:
self.before_interrupt_barrier.wait()
self.dbg.GetCommandInterpreter().InterruptCommand()
self.after_interrupt_barrier.wait()
# Check that the command was indeed interrupted:
self.result_barrier.wait()
self.assertTrue(self.result.Succeeded(), "Our command succeeded")
result_output = self.result.GetOutput()
self.check_result(False)
def interruptible_command_test(self, use_interrupt_requested):
"""Test that interpreter interruption interrupts a command
running in the RunCommandInterpreter loop.
If use_interrupt_requested is true, we'll check that API,
otherwise we'll check WasInterrupted. They should both do
the same thing."""
self.out_filename = self.getBuildArtifact("output")
self.in_filename = self.getBuildArtifact("input")
# We're going to overwrite the input file, but we
# don't want data accumulating in the output file.
if os.path.exists(self.out_filename):
os.unlink(self.out_filename)
# You should be able to use either check method interchangeably:
if use_interrupt_requested:
self.command = self.command_setup("debugger") + "\n"
else:
self.command = self.command_setup("interp") + "\n"
self.start_command_interp()
# Now give the interpreter a chance to run this command up
# to the first barrier
self.before_interrupt_barrier.wait()
# Then issue the interrupt:
sent_interrupt = self.dbg.GetCommandInterpreter().InterruptCommand()
self.assertTrue(sent_interrupt, "Did send command interrupt.")
# Now give the command a chance to finish:
self.after_interrupt_barrier.wait()
self.check_result_output(True)
os.unlink(self.out_filename)
# Now send the check command, and make sure the flag is now down.
self.command = self.command_setup("interp check") + "\n"
self.start_command_interp()
self.check_result_output(False)
def test_interruptible_command_check_dbg(self):
self.interruptible_command_test(True)
def test_interruptible_command_check_interp(self):
self.interruptible_command_test(False)
def test_debugger_doesnt_interrupt_command(self):
"""Test that debugger interruption doesn't interrupt a command
running in the RunCommandInterpreter loop."""
self.out_filename = self.getBuildArtifact("output")
self.in_filename = self.getBuildArtifact("input")
# We're going to overwrite the input file, but we
# don't want data accumulating in the output file.
if os.path.exists(self.out_filename):
os.unlink(self.out_filename)
self.command = self.command_setup("interp poll") + "\n"
self.start_command_interp()
self.before_interrupt_barrier.wait()
self.dbg.RequestInterrupt()
def cleanup():
self.dbg.CancelInterruptRequest()
self.addTearDownHook(cleanup)
self.after_interrupt_barrier.wait()
self.check_result_output(False)
os.unlink(self.out_filename)
|