[lldb] Make IR interpretation interruptible

Check the interrupt flag while interpreting IR expressions and allow the
user to interrupt them.

Differential revision: https://reviews.llvm.org/D156822
This commit is contained in:
Jonas Devlieghere 2023-08-02 11:11:32 -07:00
parent 6ee9de7a67
commit 61af957aea
No known key found for this signature in database
GPG Key ID: 49CC0BD90FDEED4D
2 changed files with 55 additions and 5 deletions

View File

@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "lldb/Expression/IRInterpreter.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/ModuleSpec.h"
#include "lldb/Core/ValueObject.h"
@ -464,6 +465,8 @@ static const char *unsupported_operand_error =
"Interpreter doesn't handle one of the expression's operands";
static const char *interpreter_internal_error =
"Interpreter encountered an internal error";
static const char *interrupt_error =
"Interrupted while interpreting expression";
static const char *bad_value_error =
"Interpreter couldn't resolve a value during execution";
static const char *memory_allocation_error =
@ -726,6 +729,9 @@ bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function,
frame.Jump(&function.front());
lldb_private::Process *process = exe_ctx.GetProcessPtr();
lldb_private::Target *target = exe_ctx.GetTargetPtr();
using clock = std::chrono::steady_clock;
// Compute the time at which the timeout has been exceeded.
@ -741,6 +747,16 @@ bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function,
return false;
}
// If we have access to the debugger we can honor an interrupt request.
if (target) {
if (INTERRUPT_REQUESTED(target->GetDebugger(),
"Interrupted in IR interpreting.")) {
error.SetErrorToGenericError();
error.SetErrorString(interrupt_error);
return false;
}
}
const Instruction *inst = &*frame.m_ii;
LLDB_LOGF(log, "Interpreting %s", PrintValue(inst).c_str());
@ -1432,7 +1448,7 @@ bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function,
}
// Make sure we have a valid process
if (!exe_ctx.GetProcessPtr()) {
if (!process) {
error.SetErrorToGenericError();
error.SetErrorString("unable to get the process");
return false;
@ -1538,11 +1554,11 @@ bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function,
return false;
}
exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
process->SetRunningUserExpression(true);
// Execute the actual function call thread plan
lldb::ExpressionResults res = exe_ctx.GetProcessRef().RunThreadPlan(
exe_ctx, call_plan_sp, options, diagnostics);
lldb::ExpressionResults res =
process->RunThreadPlan(exe_ctx, call_plan_sp, options, diagnostics);
// Check that the thread plan completed successfully
if (res != lldb::ExpressionResults::eExpressionCompleted) {
@ -1551,7 +1567,7 @@ bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function,
return false;
}
exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
process->SetRunningUserExpression(false);
// Void return type
if (returnType->isVoidTy()) {

View File

@ -47,6 +47,40 @@ class IRInterpreterTestCase(TestBase):
self.assertGreaterEqual(duration_sec, 1)
self.assertLess(duration_sec, 30)
def test_interpreter_interrupt(self):
"""Test interrupting the IRInterpreter."""
self.build()
self.target = self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
self.assertTrue(self.target, VALID_TARGET)
# A non-trivial infinite loop.
inf_loop = "for (unsigned i = 0; i < 100; ++i) --i; 1"
options = lldb.SBExpressionOptions()
# This is an IRInterpreter specific test, so disable the JIT.
options.SetAllowJIT(False)
# Make sure we have a pretty long (10s) timeout so we have a chance to
# interrupt the interpreted expression.
options.SetTimeoutInMicroSeconds(10000000)
self.dbg.RequestInterrupt()
self.dbg.SetAsync(True)
res = self.target.EvaluateExpression(inf_loop, options)
self.dbg.SetAsync(False)
# Be sure to turn this off again:
def cleanup():
if self.dbg.InterruptRequested():
self.dbg.CancelInterruptRequest()
self.addTearDownHook(cleanup)
interrupt_error = "Interrupted while interpreting expression"
self.assertIn(interrupt_error, str(res.GetError()))
def setUp(self):
# Call super's setUp().
TestBase.setUp(self)