46#include "llvm/BinaryFormat/Dwarf.h"
53 llvm::StringRef expr, llvm::StringRef prefix,
68 m_address = frame_sp->GetFrameCodeAddress();
78 if (process_sp != expected_process_sp)
89 frame_sp->GetFrameCodeAddress(),
90 target_sp.get()) == 0);
110 "Couldn't load '{0}' because the context is incomplete", object_name);
114 if (
auto var_list_sp = frame_sp->GetInScopeVariableList(
false))
116 var_list_sp->FindVariable(
ConstString(object_name),
false))
117 return frame_sp->GetValueObjectForFrameVariable(var_sp,
124 llvm::StringRef object_name,
129 if (!err.
Success() || !valobj_sp.get())
136 "Couldn't load '{0}' because its value couldn't be evaluated",
147 llvm::StringRef expr, llvm::StringRef prefix,
149 std::string *fixed_expression,
ValueObject *ctx_obj) {
157 LLDB_LOG(log,
"== [UserExpression::Evaluate] The current policy doesn't "
158 "allow evaluating expressions ==");
160 "expression evaluation is not allowed in this context"));
165 static unsigned const ctx_type_mask = lldb::TypeFlags::eTypeIsClass |
166 lldb::TypeFlags::eTypeIsStructUnion |
167 lldb::TypeFlags::eTypeIsReference;
169 LLDB_LOG(log,
"== [UserExpression::Evaluate] Passed a context object of "
170 "an invalid type, can't run expressions.");
171 set_error(
Status(
"a context object of an invalid type passed"));
176 if (ctx_obj && ctx_obj->
GetTypeInfo() & lldb::TypeFlags::eTypeIsReference) {
179 if (!
error.Success()) {
180 LLDB_LOG(log,
"== [UserExpression::Evaluate] Passed a context object of "
181 "a reference type that can't be dereferenced, can't run "
184 "passed context object of an reference type cannot be deferenced"));
188 ctx_obj = deref_ctx_sp.get();
198 LLDB_LOG(log,
"== [UserExpression::Evaluate] Passed a NULL target, can't "
200 set_error(
Status(
"expression passed a null target"));
207 LLDB_LOG(log,
"== [UserExpression::Evaluate] No process, but the policy is "
208 "eExecutionPolicyAlways");
210 set_error(
Status(
"expression needed to run but couldn't: no process"));
219 "unable to evaluate expression while the process is {0}: the process "
220 "must be stopped because the expression might require allocating "
231 (process ==
nullptr || !process->
CanJIT()))
241 llvm::StringRef full_prefix;
242 llvm::StringRef option_prefix(options.
GetPrefix());
243 std::string full_prefix_storage;
244 if (!prefix.empty() && !option_prefix.empty()) {
245 full_prefix_storage = std::string(prefix);
246 full_prefix_storage.append(std::string(option_prefix));
247 full_prefix = full_prefix_storage;
248 }
else if (!prefix.empty())
249 full_prefix = prefix;
251 full_prefix = option_prefix;
260 language = frame->GetLanguage();
266 expr, full_prefix, language, desired_type, options, ctx_obj,
error));
267 if (
error.Fail() || !user_expression_sp) {
268 LLDB_LOG(log,
"== [UserExpression::Evaluate] Getting expression: {0} ==",
270 set_error(std::move(
error));
274 LLDB_LOG(log,
"== [UserExpression::Evaluate] Parsing expression {0} ==",
277 const bool keep_expression_in_memory =
true;
281 set_error(
Status(
"expression interrupted by callback before parse"));
288 user_expression_sp->Parse(diagnostic_manager, exe_ctx, execution_policy,
289 keep_expression_in_memory, generate_debug_info);
292 std::string tmp_fixed_expression;
293 if (fixed_expression ==
nullptr)
294 fixed_expression = &tmp_fixed_expression;
296 *fixed_expression = user_expression_sp->GetFixedText().str();
300 if (!parse_success) {
304 for (uint64_t i = 0; i < max_fix_retries; ++i) {
307 fixed_expression->c_str(), full_prefix, language, desired_type,
308 options, ctx_obj,
error));
309 if (!user_expression_sp)
313 parse_success = user_expression_sp->Parse(
314 fixed_diagnostic_manager, exe_ctx, execution_policy,
315 keep_expression_in_memory, generate_debug_info);
317 diagnostic_manager.
Clear();
322 if (!user_expression_sp->GetFixedText().empty()) {
323 *fixed_expression = user_expression_sp->GetFixedText().str();
327 fixed_expression->clear();
333 if (!parse_success) {
334 if (user_expression_sp)
335 user_expression_sp->FixupParseErrorDiagnostics(diagnostic_manager);
338 !fixed_expression->empty()) {
340 "fixed expression suggested:\n " + *fixed_expression;
347 "expression failed to parse (no further compiler diagnostics)"));
358 !user_expression_sp->CanInterpret()) {
359 LLDB_LOG(log,
"== [UserExpression::Evaluate] Expression may not run, but "
360 "is not constant ==");
365 "expression needed to run but couldn't"));
373 "expression interrupted by callback before execution")));
377 diagnostic_manager.
Clear();
379 LLDB_LOG(log,
"== [UserExpression::Evaluate] Executing expression ==");
382 user_expression_sp->Execute(diagnostic_manager, exe_ctx, options,
383 user_expression_sp, expr_result);
386 LLDB_LOG(log,
"== [UserExpression::Evaluate] Execution completed "
392 "expression failed to execute, unknown error"));
395 diagnostic_manager.
GetAsError(execution_results));
398 result_valobj_sp = expr_result->GetValueObject();
399 result_valobj_sp->SetPreferredDisplayLanguage(
403 "== [UserExpression::Evaluate] Execution completed "
404 "normally with result {0} ==",
405 result_valobj_sp->GetValueAsCString());
407 LLDB_LOG(log,
"== [UserExpression::Evaluate] Execution completed "
408 "normally with no result ==");
419 "expression interrupted by callback after complete")));
424 set_error(std::move(
error));
425 return execution_results;
437 Progress progress(
"Running expression",
442 diagnostic_manager, exe_ctx, options, shared_ptr_to_me, result_var);
445 if (
auto *persistent_state =
448 persistent_state->RemovePersistentVariable(result_var);
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
static int CompareLoadAddress(const Address &lhs, const Address &rhs, Target *target)
A uniqued constant string class.
A class to manage flag bits.
const DiagnosticList & Diagnostics() const
llvm::Error GetAsError(lldb::ExpressionResults result, llvm::Twine message={}) const
Returns an ExpressionError with arg as error code.
void AddDiagnostic(llvm::StringRef message, lldb::Severity severity, DiagnosticOrigin origin, uint32_t compiler_id=LLDB_INVALID_COMPILER_ID)
const char * GetPrefix() const
uint64_t GetRetriesWithFixIts() const
SourceLanguage GetLanguage() const
bool GetAutoApplyFixIts() const
bool InvokeCancelCallback(lldb::ExpressionEvaluationPhase phase) const
ExecutionPolicy GetExecutionPolicy() const
bool GetSuppressPersistentResult() const
bool GetGenerateDebugInfo() const
bool DoesCoerceToId() const
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
ExecutionContextScope * GetBestExecutionContextScope() const
const lldb::TargetSP & GetTargetSP() const
Get accessor to get the target shared pointer.
const lldb::ProcessSP & GetProcessSP() const
Get accessor to get the process shared pointer.
StackFrame * GetFramePtr() const
Returns a pointer to the frame object.
const lldb::StackFrameSP & GetFrameSP() const
Get accessor to get the frame shared pointer.
Target * GetTargetPtr() const
Returns a pointer to the target object.
const lldb::ThreadSP & GetThreadSP() const
Get accessor to get the thread shared pointer.
Process * GetProcessPtr() const
Returns a pointer to the process object.
lldb::ProcessWP m_jit_process_wp
Expression's always have to have a target...
Expression(Target &target)
static PolicyStack & Get()
A plug-in interface definition class for debugging a process.
lldb::StateType GetState()
Get accessor for the current process state.
bool CanJIT()
Determines whether executing JIT-compiled code in this process is possible.
A Progress indicator helper class.
This base class provides an interface to stack frames.
void Clear()
Clear the object state.
static Status FromErrorString(const char *str)
static Status static Status FromErrorStringWithFormatv(const char *format, Args &&...args)
static Status FromError(llvm::Error error)
Avoid using this in new code. Migrate APIs to llvm::Expected instead.
bool Success() const
Test for success condition.
bool GetEnableNotifyAboutFixIts() const
SourceLanguage GetLanguage() const
Debugger & GetDebugger() const
PersistentExpressionState * GetPersistentExpressionStateForLanguage(lldb::LanguageType language)
UserExpression * GetUserExpressionForLanguage(llvm::StringRef expr, llvm::StringRef prefix, SourceLanguage language, Expression::ResultType desired_type, const EvaluateExpressionOptions &options, ValueObject *ctx_obj, Status &error)
Address m_address
The address the process is stopped in.
~UserExpression() override
Destructor.
static lldb::ValueObjectSP GetObjectPointerValueObject(lldb::StackFrameSP frame, llvm::StringRef object_name, Status &err)
Return ValueObject for a given variable name in the current stack frame.
SourceLanguage m_language
The language to use when parsing (unknown means use defaults).
virtual lldb::ExpressionResults DoExecute(DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx, const EvaluateExpressionOptions &options, lldb::UserExpressionSP &shared_ptr_to_me, lldb::ExpressionVariableSP &result)=0
ResultType m_desired_type
The type to coerce the expression's result to.
static lldb::ExpressionResults Evaluate(ExecutionContext &exe_ctx, const EvaluateExpressionOptions &options, llvm::StringRef expr_cstr, llvm::StringRef expr_prefix, lldb::ValueObjectSP &result_valobj_sp, std::string *fixed_expression=nullptr, ValueObject *ctx_obj=nullptr)
Evaluate one expression in the scratch context of the target passed in the exe_ctx and return its res...
void InstallContext(ExecutionContext &exe_ctx)
Populate m_in_cplusplus_method and m_in_objectivec_method based on the environment.
std::string m_expr_prefix
The text of the translation-level definitions, as provided by the user.
static const Status::ValueType kNoResult
ValueObject::GetError() returns this if there is no result from the expression.
std::string m_expr_text
The text of the expression, as typed by the user.
EvaluateExpressionOptions m_options
Additional options provided by the user.
bool LockAndCheckContext(ExecutionContext &exe_ctx, lldb::TargetSP &target_sp, lldb::ProcessSP &process_sp, lldb::StackFrameSP &frame_sp)
bool MatchesContext(ExecutionContext &exe_ctx)
static lldb::addr_t GetObjectPointer(lldb::StackFrameSP frame_sp, llvm::StringRef object_name, Status &err)
lldb::ExpressionResults Execute(DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx, const EvaluateExpressionOptions &options, lldb::UserExpressionSP &shared_ptr_to_me, lldb::ExpressionVariableSP &result)
Execute the parsed expression by callinng the derived class's DoExecute method.
UserExpression(ExecutionContextScope &exe_scope, llvm::StringRef expr, llvm::StringRef prefix, SourceLanguage language, ResultType desired_type, const EvaluateExpressionOptions &options)
Constructor.
static char ID
LLVM RTTI support.
static lldb::ValueObjectSP Create(ExecutionContextScope *exe_scope, lldb::ByteOrder byte_order, uint32_t addr_byte_size, lldb::addr_t address=LLDB_INVALID_ADDRESS, ValueObjectManager *manager=nullptr)
These routines create ValueObjectConstResult ValueObjects from various data sources.
uint32_t GetTypeInfo(CompilerType *pointee_or_element_compiler_type=nullptr)
virtual lldb::ValueObjectSP Dereference(Status &error)
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
ExecutionPolicy
Expression execution policies.
@ eExecutionPolicyTopLevel
const char * StateAsCString(lldb::StateType state)
Converts a StateType to a C string.
@ eExpressionEvaluationComplete
@ eExpressionEvaluationParse
@ eExpressionEvaluationExecution
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::Thread > ThreadSP
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
std::shared_ptr< lldb_private::ExpressionVariable > ExpressionVariableSP
@ eStateStopped
Process or thread is stopped and can be examined.
@ eErrorTypeGeneric
Generic errors that can be any value.
std::shared_ptr< lldb_private::UserExpression > UserExpressionSP
ExpressionResults
The results of expression evaluation.
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::Target > TargetSP
A type-erased pair of llvm::dwarf::SourceLanguageName and version.
lldb::LanguageType AsLanguageType() const