diff --git a/lldb/source/Expression/IRInterpreter.cpp b/lldb/source/Expression/IRInterpreter.cpp --- a/lldb/source/Expression/IRInterpreter.cpp +++ b/lldb/source/Expression/IRInterpreter.cpp @@ -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 @@ "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 @@ 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 @@ 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 @@ } // 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 @@ 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 @@ return false; } - exe_ctx.GetProcessPtr()->SetRunningUserExpression(false); + process->SetRunningUserExpression(false); // Void return type if (returnType->isVoidTy()) { diff --git a/lldb/test/API/commands/expression/ir-interpreter/TestIRInterpreter.py b/lldb/test/API/commands/expression/ir-interpreter/TestIRInterpreter.py --- a/lldb/test/API/commands/expression/ir-interpreter/TestIRInterpreter.py +++ b/lldb/test/API/commands/expression/ir-interpreter/TestIRInterpreter.py @@ -47,6 +47,40 @@ 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)