Skip to content

Commit 61af957

Browse files
committed
[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
1 parent 6ee9de7 commit 61af957

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

lldb/source/Expression/IRInterpreter.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "lldb/Expression/IRInterpreter.h"
10+
#include "lldb/Core/Debugger.h"
1011
#include "lldb/Core/Module.h"
1112
#include "lldb/Core/ModuleSpec.h"
1213
#include "lldb/Core/ValueObject.h"
@@ -464,6 +465,8 @@ static const char *unsupported_operand_error =
464465
"Interpreter doesn't handle one of the expression's operands";
465466
static const char *interpreter_internal_error =
466467
"Interpreter encountered an internal error";
468+
static const char *interrupt_error =
469+
"Interrupted while interpreting expression";
467470
static const char *bad_value_error =
468471
"Interpreter couldn't resolve a value during execution";
469472
static const char *memory_allocation_error =
@@ -726,6 +729,9 @@ bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function,
726729

727730
frame.Jump(&function.front());
728731

732+
lldb_private::Process *process = exe_ctx.GetProcessPtr();
733+
lldb_private::Target *target = exe_ctx.GetTargetPtr();
734+
729735
using clock = std::chrono::steady_clock;
730736

731737
// Compute the time at which the timeout has been exceeded.
@@ -741,6 +747,16 @@ bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function,
741747
return false;
742748
}
743749

750+
// If we have access to the debugger we can honor an interrupt request.
751+
if (target) {
752+
if (INTERRUPT_REQUESTED(target->GetDebugger(),
753+
"Interrupted in IR interpreting.")) {
754+
error.SetErrorToGenericError();
755+
error.SetErrorString(interrupt_error);
756+
return false;
757+
}
758+
}
759+
744760
const Instruction *inst = &*frame.m_ii;
745761

746762
LLDB_LOGF(log, "Interpreting %s", PrintValue(inst).c_str());
@@ -1432,7 +1448,7 @@ bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function,
14321448
}
14331449

14341450
// Make sure we have a valid process
1435-
if (!exe_ctx.GetProcessPtr()) {
1451+
if (!process) {
14361452
error.SetErrorToGenericError();
14371453
error.SetErrorString("unable to get the process");
14381454
return false;
@@ -1538,11 +1554,11 @@ bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function,
15381554
return false;
15391555
}
15401556

1541-
exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
1557+
process->SetRunningUserExpression(true);
15421558

15431559
// Execute the actual function call thread plan
1544-
lldb::ExpressionResults res = exe_ctx.GetProcessRef().RunThreadPlan(
1545-
exe_ctx, call_plan_sp, options, diagnostics);
1560+
lldb::ExpressionResults res =
1561+
process->RunThreadPlan(exe_ctx, call_plan_sp, options, diagnostics);
15461562

15471563
// Check that the thread plan completed successfully
15481564
if (res != lldb::ExpressionResults::eExpressionCompleted) {
@@ -1551,7 +1567,7 @@ bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function,
15511567
return false;
15521568
}
15531569

1554-
exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
1570+
process->SetRunningUserExpression(false);
15551571

15561572
// Void return type
15571573
if (returnType->isVoidTy()) {

lldb/test/API/commands/expression/ir-interpreter/TestIRInterpreter.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,40 @@ def test_interpreter_timeout(self):
4747
self.assertGreaterEqual(duration_sec, 1)
4848
self.assertLess(duration_sec, 30)
4949

50+
def test_interpreter_interrupt(self):
51+
"""Test interrupting the IRInterpreter."""
52+
self.build()
53+
self.target = self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
54+
self.assertTrue(self.target, VALID_TARGET)
55+
56+
# A non-trivial infinite loop.
57+
inf_loop = "for (unsigned i = 0; i < 100; ++i) --i; 1"
58+
59+
options = lldb.SBExpressionOptions()
60+
61+
# This is an IRInterpreter specific test, so disable the JIT.
62+
options.SetAllowJIT(False)
63+
64+
# Make sure we have a pretty long (10s) timeout so we have a chance to
65+
# interrupt the interpreted expression.
66+
options.SetTimeoutInMicroSeconds(10000000)
67+
68+
self.dbg.RequestInterrupt()
69+
70+
self.dbg.SetAsync(True)
71+
res = self.target.EvaluateExpression(inf_loop, options)
72+
self.dbg.SetAsync(False)
73+
74+
# Be sure to turn this off again:
75+
def cleanup():
76+
if self.dbg.InterruptRequested():
77+
self.dbg.CancelInterruptRequest()
78+
79+
self.addTearDownHook(cleanup)
80+
81+
interrupt_error = "Interrupted while interpreting expression"
82+
self.assertIn(interrupt_error, str(res.GetError()))
83+
5084
def setUp(self):
5185
# Call super's setUp().
5286
TestBase.setUp(self)

0 commit comments

Comments
 (0)