[Lldb-commits] [lldb] 61af957 - [lldb] Make IR interpretation interruptible

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Wed Aug 2 13:56:33 PDT 2023


Author: Jonas Devlieghere
Date: 2023-08-02T13:56:24-07:00
New Revision: 61af957aeaa3ba69a64598966525af6a40280fa0

URL: https://github.com/llvm/llvm-project/commit/61af957aeaa3ba69a64598966525af6a40280fa0
DIFF: https://github.com/llvm/llvm-project/commit/61af957aeaa3ba69a64598966525af6a40280fa0.diff

LOG: [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

Added: 
    

Modified: 
    lldb/source/Expression/IRInterpreter.cpp
    lldb/test/API/commands/expression/ir-interpreter/TestIRInterpreter.py

Removed: 
    


################################################################################
diff  --git a/lldb/source/Expression/IRInterpreter.cpp b/lldb/source/Expression/IRInterpreter.cpp
index 36d9980f73e9c0..10457f51a95d8b 100644
--- 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 @@ 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()) {

diff  --git a/lldb/test/API/commands/expression/ir-interpreter/TestIRInterpreter.py b/lldb/test/API/commands/expression/ir-interpreter/TestIRInterpreter.py
index 3528f1636acd17..1b11d8e5ba054b 100644
--- a/lldb/test/API/commands/expression/ir-interpreter/TestIRInterpreter.py
+++ b/lldb/test/API/commands/expression/ir-interpreter/TestIRInterpreter.py
@@ -47,6 +47,40 @@ def test_interpreter_timeout(self):
         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)


        


More information about the lldb-commits mailing list