Skip to content

Commit 42a9e45

Browse files
committed
[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 (cherry picked from commit e31f994)
1 parent ff91677 commit 42a9e45

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

lldb/source/Expression/UserExpression.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "lldb/Utility/ConstString.h"
4040
#include "lldb/Utility/LLDBLog.h"
4141
#include "lldb/Utility/Log.h"
42+
#include "lldb/Utility/State.h"
4243
#include "lldb/Utility/StreamString.h"
4344

4445
#ifdef LLDB_ENABLE_SWIFT
@@ -206,15 +207,18 @@ UserExpression::Evaluate(ExecutionContext &exe_ctx,
206207

207208
return execution_results;
208209
}
209-
// Since we might need to call allocate memory and maybe call code to make
210-
// the caller, we need to be stopped.
210+
211+
// Since we might need to allocate memory, we need to be stopped to run
212+
// an expression.
211213
if (process != nullptr && process->GetState() != lldb::eStateStopped) {
212-
error.SetErrorString("Can't make a function caller while the process is "
213-
"running");
214+
error.SetErrorStringWithFormatv(
215+
"unable to evaluate expression while the process is {0}: the process "
216+
"must be stopped because the expression might require allocating "
217+
"memory.",
218+
StateAsCString(process->GetState()));
214219
return execution_results;
215220
}
216221

217-
218222
// Explicitly force the IR interpreter to evaluate the expression when the
219223
// there is no process that supports running the expression for us. Don't
220224
// change the execution policy if we have the special top-level policy that

lldb/source/Expression/UtilityFunction.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "lldb/Target/Target.h"
2222
#include "lldb/Utility/ConstString.h"
2323
#include "lldb/Utility/Log.h"
24+
#include "lldb/Utility/State.h"
2425
#include "lldb/Utility/Stream.h"
2526

2627
using namespace lldb_private;
@@ -64,11 +65,13 @@ FunctionCaller *UtilityFunction::MakeFunctionCaller(
6465
error.SetErrorString("Can't make a function caller without a process.");
6566
return nullptr;
6667
}
67-
// Since we might need to call allocate memory and maybe call code to make
68+
// Since we might need to allocate memory and maybe call code to make
6869
// the caller, we need to be stopped.
6970
if (process_sp->GetState() != lldb::eStateStopped) {
70-
error.SetErrorString("Can't make a function caller while the process is "
71-
"running");
71+
error.SetErrorStringWithFormatv(
72+
"Can't make a function caller while the process is {0}: the process "
73+
"must be stopped to allocate memory.",
74+
StateAsCString(process_sp->GetState()));
7275
return nullptr;
7376
}
7477

lldb/test/Shell/Expr/TestExited.test

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# RUN: %clangxx_host %p/Inputs/call-function.cpp -g -o %t
2+
# RUN: %lldb %t -o 'r' -o 'expr strlen("")' 2>&1 | FileCheck %s
3+
# CHECK: error: unable to evaluate expression while the process is exited: the process must be stopped because the expression might require allocating memory.

0 commit comments

Comments
 (0)