43#include "llvm/BinaryFormat/Dwarf.h"
50 llvm::StringRef expr, llvm::StringRef prefix,
53 :
Expression(exe_scope), m_expr_text(std::string(expr)),
54 m_expr_prefix(std::string(prefix)), m_language(language),
55 m_desired_type(desired_type), m_options(options) {}
65 m_address = frame_sp->GetFrameCodeAddress();
75 if (process_sp != expected_process_sp)
86 frame_sp->GetFrameCodeAddress(),
87 target_sp.get()) == 0);
107 "Couldn't load '{0}' because the context is incomplete", object_name);
114 return frame_sp->GetValueForVariableExpressionPath(
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 static unsigned const ctx_type_mask = lldb::TypeFlags::eTypeIsClass |
158 lldb::TypeFlags::eTypeIsStructUnion |
159 lldb::TypeFlags::eTypeIsReference;
161 LLDB_LOG(log,
"== [UserExpression::Evaluate] Passed a context object of "
162 "an invalid type, can't run expressions.");
163 set_error(
Status(
"a context object of an invalid type passed"));
168 if (ctx_obj && ctx_obj->
GetTypeInfo() & lldb::TypeFlags::eTypeIsReference) {
171 if (!
error.Success()) {
172 LLDB_LOG(log,
"== [UserExpression::Evaluate] Passed a context object of "
173 "a reference type that can't be dereferenced, can't run "
176 "passed context object of an reference type cannot be deferenced"));
180 ctx_obj = deref_ctx_sp.get();
190 LLDB_LOG(log,
"== [UserExpression::Evaluate] Passed a NULL target, can't "
192 set_error(
Status(
"expression passed a null target"));
199 LLDB_LOG(log,
"== [UserExpression::Evaluate] No process, but the policy is "
200 "eExecutionPolicyAlways");
202 set_error(
Status(
"expression needed to run but couldn't: no process"));
211 "unable to evaluate expression while the process is {0}: the process "
212 "must be stopped because the expression might require allocating "
223 (process ==
nullptr || !process->
CanJIT()))
233 llvm::StringRef full_prefix;
234 llvm::StringRef option_prefix(options.
GetPrefix());
235 std::string full_prefix_storage;
236 if (!prefix.empty() && !option_prefix.empty()) {
237 full_prefix_storage = std::string(prefix);
238 full_prefix_storage.append(std::string(option_prefix));
239 full_prefix = full_prefix_storage;
240 }
else if (!prefix.empty())
241 full_prefix = prefix;
243 full_prefix = option_prefix;
252 language = frame->GetLanguage();
258 expr, full_prefix, language, desired_type, options, ctx_obj,
error));
259 if (
error.Fail() || !user_expression_sp) {
260 LLDB_LOG(log,
"== [UserExpression::Evaluate] Getting expression: {0} ==",
262 set_error(std::move(
error));
266 LLDB_LOG(log,
"== [UserExpression::Evaluate] Parsing expression {0} ==",
269 const bool keep_expression_in_memory =
true;
273 set_error(
Status(
"expression interrupted by callback before parse"));
280 user_expression_sp->Parse(diagnostic_manager, exe_ctx, execution_policy,
281 keep_expression_in_memory, generate_debug_info);
284 std::string tmp_fixed_expression;
285 if (fixed_expression ==
nullptr)
286 fixed_expression = &tmp_fixed_expression;
288 *fixed_expression = user_expression_sp->GetFixedText().str();
292 if (!parse_success) {
295 user_expression_sp.reset();
300 for (uint64_t i = 0; i < max_fix_retries; ++i) {
304 fixed_expression->c_str(), full_prefix, language, desired_type,
305 options, ctx_obj,
error));
306 if (!fixed_expression_sp)
309 parse_success = fixed_expression_sp->Parse(
310 fixed_diagnostic_manager, exe_ctx, execution_policy,
311 keep_expression_in_memory, generate_debug_info);
313 diagnostic_manager.
Clear();
314 user_expression_sp = fixed_expression_sp;
319 if (!fixed_expression_sp->GetFixedText().empty()) {
320 *fixed_expression = fixed_expression_sp->GetFixedText().str();
324 fixed_expression->clear();
330 if (!parse_success) {
332 !fixed_expression->empty()) {
334 "fixed expression suggested:\n " + *fixed_expression;
341 "expression failed to parse (no further compiler diagnostics)"));
352 !user_expression_sp->CanInterpret()) {
353 LLDB_LOG(log,
"== [UserExpression::Evaluate] Expression may not run, but "
354 "is not constant ==");
359 "expression needed to run but couldn't"));
367 "expression interrupted by callback before execution")));
371 diagnostic_manager.
Clear();
373 LLDB_LOG(log,
"== [UserExpression::Evaluate] Executing expression ==");
376 user_expression_sp->Execute(diagnostic_manager, exe_ctx, options,
377 user_expression_sp, expr_result);
380 LLDB_LOG(log,
"== [UserExpression::Evaluate] Execution completed "
386 "expression failed to execute, unknown error"));
389 diagnostic_manager.
GetAsError(execution_results));
392 result_valobj_sp = expr_result->GetValueObject();
393 result_valobj_sp->SetPreferredDisplayLanguage(
397 "== [UserExpression::Evaluate] Execution completed "
398 "normally with result {0} ==",
399 result_valobj_sp->GetValueAsCString());
401 LLDB_LOG(log,
"== [UserExpression::Evaluate] Execution completed "
402 "normally with no result ==");
413 "expression interrupted by callback after complete")));
418 set_error(std::move(
error));
419 return execution_results;
432 details =
"LLDB utility";
438 Progress progress(
"Running expression", details, {}, debugger);
441 diagnostic_manager, exe_ctx, options, shared_ptr_to_me, result_var);
444 if (
auto *persistent_state =
447 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)
bool IsValid() const
Check if the object state is valid.
A class to manage flag bits.
llvm::Error GetAsError(lldb::ExpressionResults result, llvm::Twine message={}) const
Returns an ExpressionError with arg as error code.
const DiagnosticList & Diagnostics()
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
bool IsForUtilityExpr() 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.
Encapsulates a single expression for use in lldb.
lldb::ProcessWP m_jit_process_wp
Expression's always have to have a target...
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.
@ eExpressionPathOptionsNoFragileObjcIvar
@ eExpressionPathOptionCheckPtrVsMember
@ eExpressionPathOptionsNoSyntheticArrayRange
@ eExpressionPathOptionsNoSyntheticChildren
void Clear()
Clear the object state.
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
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
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.
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)
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.
@ eLanguageTypeUnknown
Unknown or invalid language value.
@ 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::Variable > VariableSP
std::shared_ptr< lldb_private::Target > TargetSP
A type-erased pair of llvm::dwarf::SourceLanguageName and version.
lldb::LanguageType AsLanguageType() const