[Lldb-commits] [lldb] e31f994 - [lldb] Improve error message when evaluating expression when not stopped

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Fri May 26 08:50:05 PDT 2023


Author: Jonas Devlieghere
Date: 2023-05-26T08:49:58-07:00
New Revision: e31f99464216ef4d04ffa821e174522b699780ee

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

LOG: [lldb] Improve error message when evaluating expression when not stopped

When trying to run an expression after a process has existed, you
currently are shown the following error message:

  (lldb) p strlen("")
  error: Can't make a function caller while the process is running

This error is wrong and pretty uninformative. After this patch, the
following error message is shown:

  (lldb) p strlen("")
  error: unable to evaluate expression while the process is exited: the
  process must be stopped because the expression might require
  allocating memory.

rdar://109731325

Differential revision: https://reviews.llvm.org/D151497

Added: 
    lldb/test/Shell/Expr/TestExited.test

Modified: 
    lldb/source/Expression/UserExpression.cpp
    lldb/source/Expression/UtilityFunction.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/source/Expression/UserExpression.cpp b/lldb/source/Expression/UserExpression.cpp
index 0c2d159d144a1..b250177db9979 100644
--- a/lldb/source/Expression/UserExpression.cpp
+++ b/lldb/source/Expression/UserExpression.cpp
@@ -39,6 +39,7 @@
 #include "lldb/Utility/ConstString.h"
 #include "lldb/Utility/LLDBLog.h"
 #include "lldb/Utility/Log.h"
+#include "lldb/Utility/State.h"
 #include "lldb/Utility/StreamString.h"
 
 using namespace lldb_private;
@@ -202,15 +203,18 @@ UserExpression::Evaluate(ExecutionContext &exe_ctx,
 
     return execution_results;
   }
-  // Since we might need to call allocate memory and maybe call code to make
-  // the caller, we need to be stopped.
+
+  // Since we might need to allocate memory, we need to be stopped to run
+  // an expression.
   if (process != nullptr && process->GetState() != lldb::eStateStopped) {
-    error.SetErrorString("Can't make a function caller while the process is " 
-                          "running");
+    error.SetErrorStringWithFormatv(
+        "unable to evaluate expression while the process is {0}: the process "
+        "must be stopped because the expression might require allocating "
+        "memory.",
+        StateAsCString(process->GetState()));
     return execution_results;
   }
 
-
   // Explicitly force the IR interpreter to evaluate the expression when the
   // there is no process that supports running the expression for us. Don't
   // change the execution policy if we have the special top-level policy that

diff  --git a/lldb/source/Expression/UtilityFunction.cpp b/lldb/source/Expression/UtilityFunction.cpp
index 5d55d9a5c2c1d..d317c1cd6c154 100644
--- a/lldb/source/Expression/UtilityFunction.cpp
+++ b/lldb/source/Expression/UtilityFunction.cpp
@@ -21,6 +21,7 @@
 #include "lldb/Target/Target.h"
 #include "lldb/Utility/ConstString.h"
 #include "lldb/Utility/Log.h"
+#include "lldb/Utility/State.h"
 #include "lldb/Utility/Stream.h"
 
 using namespace lldb_private;
@@ -64,11 +65,13 @@ FunctionCaller *UtilityFunction::MakeFunctionCaller(
     error.SetErrorString("Can't make a function caller without a process.");
     return nullptr;
   }
-  // Since we might need to call allocate memory and maybe call code to make
+  // Since we might need to allocate memory and maybe call code to make
   // the caller, we need to be stopped.
   if (process_sp->GetState() != lldb::eStateStopped) {
-    error.SetErrorString("Can't make a function caller while the process is " 
-                         "running");
+    error.SetErrorStringWithFormatv(
+        "Can't make a function caller while the process is {0}: the process "
+        "must be stopped to allocate memory.",
+        StateAsCString(process_sp->GetState()));
     return nullptr;
   }
 

diff  --git a/lldb/test/Shell/Expr/TestExited.test b/lldb/test/Shell/Expr/TestExited.test
new file mode 100644
index 0000000000000..ca83db06ff739
--- /dev/null
+++ b/lldb/test/Shell/Expr/TestExited.test
@@ -0,0 +1,3 @@
+# RUN: %clangxx_host %p/Inputs/call-function.cpp -g -o %t
+# RUN: %lldb %t -o 'r' -o 'expr strlen("")' 2>&1 | FileCheck %s
+# CHECK: error: unable to evaluate expression while the process is exited: the process must be stopped because the expression might require allocating memory.


        


More information about the lldb-commits mailing list