LLDB mainline
UserExpression.cpp
Go to the documentation of this file.
1//===-- UserExpression.cpp ------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include <cstdio>
10#include <sys/types.h>
11
12#include <cstdlib>
13#include <map>
14#include <string>
15
16#include "lldb/Core/Module.h"
17#include "lldb/Core/Progress.h"
24#include "lldb/Host/HostInfo.h"
25#include "lldb/Symbol/Block.h"
29#include "lldb/Symbol/Type.h"
33#include "lldb/Target/Process.h"
35#include "lldb/Target/Target.h"
39#include "lldb/Utility/Log.h"
40#include "lldb/Utility/State.h"
43#include "llvm/BinaryFormat/Dwarf.h"
44
45using namespace lldb_private;
46
48
50 llvm::StringRef expr, llvm::StringRef prefix,
51 SourceLanguage language, ResultType desired_type,
52 const EvaluateExpressionOptions &options)
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) {}
56
58
61
62 lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP();
63
64 if (frame_sp)
65 m_address = frame_sp->GetFrameCodeAddress();
66}
67
69 lldb::TargetSP &target_sp,
70 lldb::ProcessSP &process_sp,
71 lldb::StackFrameSP &frame_sp) {
72 lldb::ProcessSP expected_process_sp = m_jit_process_wp.lock();
73 process_sp = exe_ctx.GetProcessSP();
74
75 if (process_sp != expected_process_sp)
76 return false;
77
78 process_sp = exe_ctx.GetProcessSP();
79 target_sp = exe_ctx.GetTargetSP();
80 frame_sp = exe_ctx.GetFrameSP();
81
82 if (m_address.IsValid()) {
83 if (!frame_sp)
84 return false;
86 frame_sp->GetFrameCodeAddress(),
87 target_sp.get()) == 0);
88 }
89
90 return true;
91}
92
94 lldb::TargetSP target_sp;
95 lldb::ProcessSP process_sp;
96 lldb::StackFrameSP frame_sp;
97
98 return LockAndCheckContext(exe_ctx, target_sp, process_sp, frame_sp);
99}
100
102 lldb::StackFrameSP frame_sp, llvm::StringRef object_name, Status &err) {
103 err.Clear();
104
105 if (!frame_sp) {
107 "Couldn't load '{0}' because the context is incomplete", object_name);
108 return {};
109 }
110
111 lldb::VariableSP var_sp;
112 lldb::ValueObjectSP valobj_sp;
113
114 return frame_sp->GetValueForVariableExpressionPath(
115 object_name, lldb::eNoDynamicValues,
120 var_sp, err);
121}
122
124 llvm::StringRef object_name,
125 Status &err) {
126 auto valobj_sp =
127 GetObjectPointerValueObject(std::move(frame_sp), object_name, err);
128
129 if (!err.Success() || !valobj_sp.get())
131
132 lldb::addr_t ret = valobj_sp->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
133
134 if (ret == LLDB_INVALID_ADDRESS) {
136 "Couldn't load '{0}' because its value couldn't be evaluated",
137 object_name);
139 }
140
141 return ret;
142}
143
146 const EvaluateExpressionOptions &options,
147 llvm::StringRef expr, llvm::StringRef prefix,
148 lldb::ValueObjectSP &result_valobj_sp,
149 std::string *fixed_expression, ValueObject *ctx_obj) {
151 auto set_error = [&](Status error) {
152 result_valobj_sp = ValueObjectConstResult::Create(
153 exe_ctx.GetBestExecutionContextScope(), std::move(error));
154 };
155
156 if (ctx_obj) {
157 static unsigned const ctx_type_mask = lldb::TypeFlags::eTypeIsClass |
158 lldb::TypeFlags::eTypeIsStructUnion |
159 lldb::TypeFlags::eTypeIsReference;
160 if (!(ctx_obj->GetTypeInfo() & ctx_type_mask)) {
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"));
165 }
166 }
167
168 if (ctx_obj && ctx_obj->GetTypeInfo() & lldb::TypeFlags::eTypeIsReference) {
170 lldb::ValueObjectSP deref_ctx_sp = ctx_obj->Dereference(error);
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 "
174 "expressions.");
175 set_error(Status(
176 "passed context object of an reference type cannot be deferenced"));
178 }
179
180 ctx_obj = deref_ctx_sp.get();
181 }
182
183 lldb_private::ExecutionPolicy execution_policy = options.GetExecutionPolicy();
184 SourceLanguage language = options.GetLanguage();
185 const ResultType desired_type = options.DoesCoerceToId()
188 Target *target = exe_ctx.GetTargetPtr();
189 if (!target) {
190 LLDB_LOG(log, "== [UserExpression::Evaluate] Passed a NULL target, can't "
191 "run expressions.");
192 set_error(Status("expression passed a null target"));
194 }
195
196 Process *process = exe_ctx.GetProcessPtr();
197
198 if (!process && execution_policy == eExecutionPolicyAlways) {
199 LLDB_LOG(log, "== [UserExpression::Evaluate] No process, but the policy is "
200 "eExecutionPolicyAlways");
201
202 set_error(Status("expression needed to run but couldn't: no process"));
203
205 }
206
207 // Since we might need to allocate memory, we need to be stopped to run
208 // an expression.
209 if (process && process->GetState() != lldb::eStateStopped) {
211 "unable to evaluate expression while the process is {0}: the process "
212 "must be stopped because the expression might require allocating "
213 "memory.",
214 StateAsCString(process->GetState())));
216 }
217
218 // Explicitly force the IR interpreter to evaluate the expression when the
219 // there is no process that supports running the expression for us. Don't
220 // change the execution policy if we have the special top-level policy that
221 // doesn't contain any expression and there is nothing to interpret.
222 if (execution_policy != eExecutionPolicyTopLevel &&
223 (process == nullptr || !process->CanJIT()))
224 execution_policy = eExecutionPolicyNever;
225
226 // We need to set the expression execution thread here, turns out parse can
227 // call functions in the process of looking up symbols, which will escape the
228 // context set by exe_ctx passed to Execute.
229 lldb::ThreadSP thread_sp = exe_ctx.GetThreadSP();
230 ThreadList::ExpressionExecutionThreadPusher execution_thread_pusher(
231 thread_sp);
232
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;
242 else
243 full_prefix = option_prefix;
244
245 // If the language was not specified in the expression command, set it to the
246 // language in the target's properties if specified, else default to the
247 // langage for the frame.
248 if (!language) {
250 language = target->GetLanguage();
251 else if (StackFrame *frame = exe_ctx.GetFramePtr())
252 language = frame->GetLanguage();
253 }
254
256 lldb::UserExpressionSP user_expression_sp(
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} ==",
261 error.AsCString());
262 set_error(std::move(error));
264 }
265
266 LLDB_LOG(log, "== [UserExpression::Evaluate] Parsing expression {0} ==",
267 expr.str());
268
269 const bool keep_expression_in_memory = true;
270 const bool generate_debug_info = options.GetGenerateDebugInfo();
271
273 set_error(Status("expression interrupted by callback before parse"));
275 }
276
277 DiagnosticManager diagnostic_manager;
278
279 bool parse_success =
280 user_expression_sp->Parse(diagnostic_manager, exe_ctx, execution_policy,
281 keep_expression_in_memory, generate_debug_info);
282
283 // Calculate the fixed expression always, since we need it for errors.
284 std::string tmp_fixed_expression;
285 if (fixed_expression == nullptr)
286 fixed_expression = &tmp_fixed_expression;
287
288 *fixed_expression = user_expression_sp->GetFixedText().str();
290
291 // If there is a fixed expression, try to parse it:
292 if (!parse_success) {
293 // Delete the expression that failed to parse before attempting to parse
294 // the next expression.
295 user_expression_sp.reset();
296
297 execution_results = lldb::eExpressionParseError;
298 if (!fixed_expression->empty() && options.GetAutoApplyFixIts()) {
299 const uint64_t max_fix_retries = options.GetRetriesWithFixIts();
300 for (uint64_t i = 0; i < max_fix_retries; ++i) {
301 // Try parsing the fixed expression.
302 lldb::UserExpressionSP fixed_expression_sp(
304 fixed_expression->c_str(), full_prefix, language, desired_type,
305 options, ctx_obj, error));
306 if (!fixed_expression_sp)
307 break;
308 DiagnosticManager fixed_diagnostic_manager;
309 parse_success = fixed_expression_sp->Parse(
310 fixed_diagnostic_manager, exe_ctx, execution_policy,
311 keep_expression_in_memory, generate_debug_info);
312 if (parse_success) {
313 diagnostic_manager.Clear();
314 user_expression_sp = fixed_expression_sp;
315 break;
316 }
317 // The fixed expression also didn't parse. Let's check for any new
318 // fixits we could try.
319 if (!fixed_expression_sp->GetFixedText().empty()) {
320 *fixed_expression = fixed_expression_sp->GetFixedText().str();
321 } else {
322 // Fixed expression didn't compile without a fixit, don't retry and
323 // don't tell the user about it.
324 fixed_expression->clear();
325 break;
326 }
327 }
328 }
329
330 if (!parse_success) {
331 if (target->GetEnableNotifyAboutFixIts() && fixed_expression &&
332 !fixed_expression->empty()) {
333 std::string fixit =
334 "fixed expression suggested:\n " + *fixed_expression;
335 diagnostic_manager.AddDiagnostic(fixit, lldb::eSeverityInfo,
337 }
338 if (diagnostic_manager.Diagnostics().empty())
339 error = Status::FromError(llvm::make_error<ExpressionError>(
340 execution_results,
341 "expression failed to parse (no further compiler diagnostics)"));
342 else
343 error =
344 Status::FromError(diagnostic_manager.GetAsError(execution_results));
345 }
346 }
347
348 if (parse_success) {
349 lldb::ExpressionVariableSP expr_result;
350
351 if (execution_policy == eExecutionPolicyNever &&
352 !user_expression_sp->CanInterpret()) {
353 LLDB_LOG(log, "== [UserExpression::Evaluate] Expression may not run, but "
354 "is not constant ==");
355
356 if (diagnostic_manager.Diagnostics().empty())
357 error = Status::FromError(llvm::make_error<ExpressionError>(
359 "expression needed to run but couldn't"));
360 } else if (execution_policy == eExecutionPolicyTopLevel) {
363 } else {
365 set_error(Status::FromError(llvm::make_error<ExpressionError>(
367 "expression interrupted by callback before execution")));
369 }
370
371 diagnostic_manager.Clear();
372
373 LLDB_LOG(log, "== [UserExpression::Evaluate] Executing expression ==");
374
375 execution_results =
376 user_expression_sp->Execute(diagnostic_manager, exe_ctx, options,
377 user_expression_sp, expr_result);
378
379 if (execution_results != lldb::eExpressionCompleted) {
380 LLDB_LOG(log, "== [UserExpression::Evaluate] Execution completed "
381 "abnormally ==");
382
383 if (diagnostic_manager.Diagnostics().empty())
384 error = Status::FromError(llvm::make_error<ExpressionError>(
385 execution_results,
386 "expression failed to execute, unknown error"));
387 else
389 diagnostic_manager.GetAsError(execution_results));
390 } else {
391 if (expr_result) {
392 result_valobj_sp = expr_result->GetValueObject();
393 result_valobj_sp->SetPreferredDisplayLanguage(
394 language.AsLanguageType());
395
396 LLDB_LOG(log,
397 "== [UserExpression::Evaluate] Execution completed "
398 "normally with result {0} ==",
399 result_valobj_sp->GetValueAsCString());
400 } else {
401 LLDB_LOG(log, "== [UserExpression::Evaluate] Execution completed "
402 "normally with no result ==");
403
405 }
406 }
407 }
408 }
409
411 set_error(Status::FromError(llvm::make_error<ExpressionError>(
413 "expression interrupted by callback after complete")));
415 }
416
417 if (error.Fail())
418 set_error(std::move(error));
419 return execution_results;
420}
421
424 ExecutionContext &exe_ctx,
425 const EvaluateExpressionOptions &options,
426 lldb::UserExpressionSP &shared_ptr_to_me,
427 lldb::ExpressionVariableSP &result_var) {
428 Debugger *debugger =
429 exe_ctx.GetTargetPtr() ? &exe_ctx.GetTargetPtr()->GetDebugger() : nullptr;
430 std::string details;
432 details = "LLDB utility";
433 else if (m_expr_text.size() > 15)
434 details = m_expr_text.substr(0, 14) + "…";
435 else
436 details = m_expr_text;
437
438 Progress progress("Running expression", details, {}, debugger);
439
441 diagnostic_manager, exe_ctx, options, shared_ptr_to_me, result_var);
442 Target *target = exe_ctx.GetTargetPtr();
443 if (options.GetSuppressPersistentResult() && result_var && target) {
444 if (auto *persistent_state =
447 persistent_state->RemovePersistentVariable(result_var);
448 }
449 return expr_result;
450}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:369
static int CompareLoadAddress(const Address &lhs, const Address &rhs, Target *target)
Definition: Address.cpp:942
bool IsValid() const
Check if the object state is valid.
Definition: Address.h:355
A class to manage flag bits.
Definition: Debugger.h:80
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
Definition: Target.h:344
uint64_t GetRetriesWithFixIts() const
Definition: Target.h:465
SourceLanguage GetLanguage() const
Definition: Target.h:329
bool InvokeCancelCallback(lldb::ExpressionEvaluationPhase phase) const
Definition: Target.h:427
ExecutionPolicy GetExecutionPolicy() const
Definition: Target.h:323
"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.
Definition: Expression.h:31
lldb::ProcessWP m_jit_process_wp
Expression's always have to have a target...
Definition: Expression.h:88
A plug-in interface definition class for debugging a process.
Definition: Process.h:343
lldb::StateType GetState()
Get accessor for the current process state.
Definition: Process.cpp:1308
bool CanJIT()
Determines whether executing JIT-compiled code in this process is possible.
Definition: Process.cpp:2451
A Progress indicator helper class.
Definition: Progress.h:60
This base class provides an interface to stack frames.
Definition: StackFrame.h:44
@ eExpressionPathOptionsNoSyntheticArrayRange
Definition: StackFrame.h:50
@ eExpressionPathOptionsNoSyntheticChildren
Definition: StackFrame.h:49
An error handling class.
Definition: Status.h:115
void Clear()
Clear the object state.
Definition: Status.cpp:215
static Status static Status FromErrorStringWithFormatv(const char *format, Args &&...args)
Definition: Status.h:148
static Status FromError(llvm::Error error)
Avoid using this in new code. Migrate APIs to llvm::Expected instead.
Definition: Status.cpp:137
bool Success() const
Test for success condition.
Definition: Status.cpp:280
bool GetEnableNotifyAboutFixIts() const
Definition: Target.cpp:4721
SourceLanguage GetLanguage() const
Definition: Target.cpp:4840
PersistentExpressionState * GetPersistentExpressionStateForLanguage(lldb::LanguageType language)
Definition: Target.cpp:2623
Debugger & GetDebugger()
Definition: Target.h:1080
UserExpression * GetUserExpressionForLanguage(llvm::StringRef expr, llvm::StringRef prefix, SourceLanguage language, Expression::ResultType desired_type, const EvaluateExpressionOptions &options, ValueObject *ctx_obj, Status &error)
Definition: Target.cpp:2643
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)
Definition: ValueObject.h:378
virtual lldb::ValueObjectSP Dereference(Status &error)
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
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.
Definition: Log.h:332
ExecutionPolicy
Expression execution policies.
const char * StateAsCString(lldb::StateType state)
Converts a StateType to a C string.
Definition: State.cpp:14
@ eExpressionEvaluationComplete
@ eExpressionEvaluationParse
@ eExpressionEvaluationExecution
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
Definition: lldb-forward.h:424
std::shared_ptr< lldb_private::Thread > ThreadSP
Definition: lldb-forward.h:450
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
Definition: lldb-forward.h:484
std::shared_ptr< lldb_private::ExpressionVariable > ExpressionVariableSP
Definition: lldb-forward.h:351
@ 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
Definition: lldb-forward.h:332
ExpressionResults
The results of expression evaluation.
@ eExpressionCompleted
@ eExpressionInterrupted
@ eExpressionParseError
@ eExpressionSetupError
std::shared_ptr< lldb_private::Process > ProcessSP
Definition: lldb-forward.h:389
std::shared_ptr< lldb_private::Variable > VariableSP
Definition: lldb-forward.h:486
uint64_t addr_t
Definition: lldb-types.h:80
std::shared_ptr< lldb_private::Target > TargetSP
Definition: lldb-forward.h:448
@ eNoDynamicValues
A type-erased pair of llvm::dwarf::SourceLanguageName and version.
lldb::LanguageType AsLanguageType() const
Definition: Language.cpp:554