1cfca06d7SDimitry Andric //===-- UserExpression.cpp ------------------------------------------------===//
2e81d9d49SDimitry Andric //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e81d9d49SDimitry Andric //
7e81d9d49SDimitry Andric //===----------------------------------------------------------------------===//
8e81d9d49SDimitry Andric
9344a3780SDimitry Andric #include <cstdio>
10e81d9d49SDimitry Andric #include <sys/types.h>
11e81d9d49SDimitry Andric
12e81d9d49SDimitry Andric #include <cstdlib>
13e81d9d49SDimitry Andric #include <map>
1414f1b3e8SDimitry Andric #include <string>
15e81d9d49SDimitry Andric
16e81d9d49SDimitry Andric #include "lldb/Core/Module.h"
17e81d9d49SDimitry Andric #include "lldb/Core/ValueObjectConstResult.h"
18f3fbd1c0SDimitry Andric #include "lldb/Expression/DiagnosticManager.h"
195f29bb8aSDimitry Andric #include "lldb/Expression/ExpressionVariable.h"
20e81d9d49SDimitry Andric #include "lldb/Expression/IRExecutionUnit.h"
21e81d9d49SDimitry Andric #include "lldb/Expression/IRInterpreter.h"
22e81d9d49SDimitry Andric #include "lldb/Expression/Materializer.h"
23e81d9d49SDimitry Andric #include "lldb/Expression/UserExpression.h"
24e81d9d49SDimitry Andric #include "lldb/Host/HostInfo.h"
25e81d9d49SDimitry Andric #include "lldb/Symbol/Block.h"
26e81d9d49SDimitry Andric #include "lldb/Symbol/Function.h"
27e81d9d49SDimitry Andric #include "lldb/Symbol/ObjectFile.h"
28e81d9d49SDimitry Andric #include "lldb/Symbol/SymbolVendor.h"
29e81d9d49SDimitry Andric #include "lldb/Symbol/Type.h"
30e81d9d49SDimitry Andric #include "lldb/Symbol/TypeSystem.h"
31e81d9d49SDimitry Andric #include "lldb/Symbol/VariableList.h"
32e81d9d49SDimitry Andric #include "lldb/Target/ExecutionContext.h"
33e81d9d49SDimitry Andric #include "lldb/Target/Process.h"
34e81d9d49SDimitry Andric #include "lldb/Target/StackFrame.h"
35e81d9d49SDimitry Andric #include "lldb/Target/Target.h"
36e81d9d49SDimitry Andric #include "lldb/Target/ThreadPlan.h"
37e81d9d49SDimitry Andric #include "lldb/Target/ThreadPlanCallUserExpression.h"
38145449b1SDimitry Andric #include "lldb/Utility/LLDBLog.h"
3974a628f7SDimitry Andric #include "lldb/Utility/Log.h"
407fa27ce4SDimitry Andric #include "lldb/Utility/State.h"
4174a628f7SDimitry Andric #include "lldb/Utility/StreamString.h"
42ac9a064cSDimitry Andric #include "llvm/BinaryFormat/Dwarf.h"
43e81d9d49SDimitry Andric
44e81d9d49SDimitry Andric using namespace lldb_private;
45e81d9d49SDimitry Andric
46706b4fc4SDimitry Andric char UserExpression::ID;
47706b4fc4SDimitry Andric
UserExpression(ExecutionContextScope & exe_scope,llvm::StringRef expr,llvm::StringRef prefix,SourceLanguage language,ResultType desired_type,const EvaluateExpressionOptions & options)48e81d9d49SDimitry Andric UserExpression::UserExpression(ExecutionContextScope &exe_scope,
4914f1b3e8SDimitry Andric llvm::StringRef expr, llvm::StringRef prefix,
50ac9a064cSDimitry Andric SourceLanguage language, ResultType desired_type,
51706b4fc4SDimitry Andric const EvaluateExpressionOptions &options)
52cfca06d7SDimitry Andric : Expression(exe_scope), m_expr_text(std::string(expr)),
53cfca06d7SDimitry Andric m_expr_prefix(std::string(prefix)), m_language(language),
54cfca06d7SDimitry Andric m_desired_type(desired_type), m_options(options) {}
55e81d9d49SDimitry Andric
56344a3780SDimitry Andric UserExpression::~UserExpression() = default;
57e81d9d49SDimitry Andric
InstallContext(ExecutionContext & exe_ctx)5814f1b3e8SDimitry Andric void UserExpression::InstallContext(ExecutionContext &exe_ctx) {
59e81d9d49SDimitry Andric m_jit_process_wp = exe_ctx.GetProcessSP();
60e81d9d49SDimitry Andric
61e81d9d49SDimitry Andric lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP();
62e81d9d49SDimitry Andric
63e81d9d49SDimitry Andric if (frame_sp)
64e81d9d49SDimitry Andric m_address = frame_sp->GetFrameCodeAddress();
65e81d9d49SDimitry Andric }
66e81d9d49SDimitry Andric
LockAndCheckContext(ExecutionContext & exe_ctx,lldb::TargetSP & target_sp,lldb::ProcessSP & process_sp,lldb::StackFrameSP & frame_sp)6714f1b3e8SDimitry Andric bool UserExpression::LockAndCheckContext(ExecutionContext &exe_ctx,
68e81d9d49SDimitry Andric lldb::TargetSP &target_sp,
69e81d9d49SDimitry Andric lldb::ProcessSP &process_sp,
7014f1b3e8SDimitry Andric lldb::StackFrameSP &frame_sp) {
71e81d9d49SDimitry Andric lldb::ProcessSP expected_process_sp = m_jit_process_wp.lock();
72e81d9d49SDimitry Andric process_sp = exe_ctx.GetProcessSP();
73e81d9d49SDimitry Andric
74e81d9d49SDimitry Andric if (process_sp != expected_process_sp)
75e81d9d49SDimitry Andric return false;
76e81d9d49SDimitry Andric
77e81d9d49SDimitry Andric process_sp = exe_ctx.GetProcessSP();
78e81d9d49SDimitry Andric target_sp = exe_ctx.GetTargetSP();
79e81d9d49SDimitry Andric frame_sp = exe_ctx.GetFrameSP();
80e81d9d49SDimitry Andric
8114f1b3e8SDimitry Andric if (m_address.IsValid()) {
82e81d9d49SDimitry Andric if (!frame_sp)
83e81d9d49SDimitry Andric return false;
84706b4fc4SDimitry Andric return (Address::CompareLoadAddress(m_address,
8514f1b3e8SDimitry Andric frame_sp->GetFrameCodeAddress(),
86706b4fc4SDimitry Andric target_sp.get()) == 0);
87e81d9d49SDimitry Andric }
88e81d9d49SDimitry Andric
89e81d9d49SDimitry Andric return true;
90e81d9d49SDimitry Andric }
91e81d9d49SDimitry Andric
MatchesContext(ExecutionContext & exe_ctx)9214f1b3e8SDimitry Andric bool UserExpression::MatchesContext(ExecutionContext &exe_ctx) {
93e81d9d49SDimitry Andric lldb::TargetSP target_sp;
94e81d9d49SDimitry Andric lldb::ProcessSP process_sp;
95e81d9d49SDimitry Andric lldb::StackFrameSP frame_sp;
96e81d9d49SDimitry Andric
97e81d9d49SDimitry Andric return LockAndCheckContext(exe_ctx, target_sp, process_sp, frame_sp);
98e81d9d49SDimitry Andric }
99e81d9d49SDimitry Andric
GetObjectPointerValueObject(lldb::StackFrameSP frame_sp,llvm::StringRef object_name,Status & err)1004b4fe385SDimitry Andric lldb::ValueObjectSP UserExpression::GetObjectPointerValueObject(
101b1c73532SDimitry Andric lldb::StackFrameSP frame_sp, llvm::StringRef object_name, Status &err) {
102e81d9d49SDimitry Andric err.Clear();
103e81d9d49SDimitry Andric
10414f1b3e8SDimitry Andric if (!frame_sp) {
105b1c73532SDimitry Andric err.SetErrorStringWithFormatv(
106b1c73532SDimitry Andric "Couldn't load '{0}' because the context is incomplete", object_name);
1074b4fe385SDimitry Andric return {};
108e81d9d49SDimitry Andric }
109e81d9d49SDimitry Andric
110e81d9d49SDimitry Andric lldb::VariableSP var_sp;
111e81d9d49SDimitry Andric lldb::ValueObjectSP valobj_sp;
112e81d9d49SDimitry Andric
1134b4fe385SDimitry Andric return frame_sp->GetValueForVariableExpressionPath(
114b1c73532SDimitry Andric object_name, lldb::eNoDynamicValues,
115e81d9d49SDimitry Andric StackFrame::eExpressionPathOptionCheckPtrVsMember |
116e81d9d49SDimitry Andric StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
117e81d9d49SDimitry Andric StackFrame::eExpressionPathOptionsNoSyntheticChildren |
118e81d9d49SDimitry Andric StackFrame::eExpressionPathOptionsNoSyntheticArrayRange,
11914f1b3e8SDimitry Andric var_sp, err);
1204b4fe385SDimitry Andric }
1214b4fe385SDimitry Andric
GetObjectPointer(lldb::StackFrameSP frame_sp,llvm::StringRef object_name,Status & err)1224b4fe385SDimitry Andric lldb::addr_t UserExpression::GetObjectPointer(lldb::StackFrameSP frame_sp,
123b1c73532SDimitry Andric llvm::StringRef object_name,
1244b4fe385SDimitry Andric Status &err) {
1254b4fe385SDimitry Andric auto valobj_sp =
1264b4fe385SDimitry Andric GetObjectPointerValueObject(std::move(frame_sp), object_name, err);
127e81d9d49SDimitry Andric
128e81d9d49SDimitry Andric if (!err.Success() || !valobj_sp.get())
129e81d9d49SDimitry Andric return LLDB_INVALID_ADDRESS;
130e81d9d49SDimitry Andric
131e81d9d49SDimitry Andric lldb::addr_t ret = valobj_sp->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
132e81d9d49SDimitry Andric
13314f1b3e8SDimitry Andric if (ret == LLDB_INVALID_ADDRESS) {
134b1c73532SDimitry Andric err.SetErrorStringWithFormatv(
135b1c73532SDimitry Andric "Couldn't load '{0}' because its value couldn't be evaluated",
136b1c73532SDimitry Andric object_name);
137e81d9d49SDimitry Andric return LLDB_INVALID_ADDRESS;
138e81d9d49SDimitry Andric }
139e81d9d49SDimitry Andric
140e81d9d49SDimitry Andric return ret;
141e81d9d49SDimitry Andric }
142e81d9d49SDimitry Andric
143cfca06d7SDimitry Andric lldb::ExpressionResults
Evaluate(ExecutionContext & exe_ctx,const EvaluateExpressionOptions & options,llvm::StringRef expr,llvm::StringRef prefix,lldb::ValueObjectSP & result_valobj_sp,Status & error,std::string * fixed_expression,ValueObject * ctx_obj)144cfca06d7SDimitry Andric UserExpression::Evaluate(ExecutionContext &exe_ctx,
145cfca06d7SDimitry Andric const EvaluateExpressionOptions &options,
14614f1b3e8SDimitry Andric llvm::StringRef expr, llvm::StringRef prefix,
1475f29bb8aSDimitry Andric lldb::ValueObjectSP &result_valobj_sp, Status &error,
148cfca06d7SDimitry Andric std::string *fixed_expression, ValueObject *ctx_obj) {
149145449b1SDimitry Andric Log *log(GetLog(LLDBLog::Expressions | LLDBLog::Step));
150e81d9d49SDimitry Andric
1515f29bb8aSDimitry Andric if (ctx_obj) {
152145449b1SDimitry Andric static unsigned const ctx_type_mask = lldb::TypeFlags::eTypeIsClass |
153145449b1SDimitry Andric lldb::TypeFlags::eTypeIsStructUnion |
154145449b1SDimitry Andric lldb::TypeFlags::eTypeIsReference;
1555f29bb8aSDimitry Andric if (!(ctx_obj->GetTypeInfo() & ctx_type_mask)) {
1565f29bb8aSDimitry Andric LLDB_LOG(log, "== [UserExpression::Evaluate] Passed a context object of "
1575f29bb8aSDimitry Andric "an invalid type, can't run expressions.");
1585f29bb8aSDimitry Andric error.SetErrorString("a context object of an invalid type passed");
1595f29bb8aSDimitry Andric return lldb::eExpressionSetupError;
1605f29bb8aSDimitry Andric }
1615f29bb8aSDimitry Andric }
1625f29bb8aSDimitry Andric
163145449b1SDimitry Andric if (ctx_obj && ctx_obj->GetTypeInfo() & lldb::TypeFlags::eTypeIsReference) {
164145449b1SDimitry Andric Status error;
165145449b1SDimitry Andric lldb::ValueObjectSP deref_ctx_sp = ctx_obj->Dereference(error);
166145449b1SDimitry Andric if (!error.Success()) {
167145449b1SDimitry Andric LLDB_LOG(log, "== [UserExpression::Evaluate] Passed a context object of "
168145449b1SDimitry Andric "a reference type that can't be dereferenced, can't run "
169145449b1SDimitry Andric "expressions.");
170145449b1SDimitry Andric error.SetErrorString(
171145449b1SDimitry Andric "passed context object of an reference type cannot be deferenced");
172145449b1SDimitry Andric return lldb::eExpressionSetupError;
173145449b1SDimitry Andric }
174145449b1SDimitry Andric
175145449b1SDimitry Andric ctx_obj = deref_ctx_sp.get();
176145449b1SDimitry Andric }
177145449b1SDimitry Andric
178e81d9d49SDimitry Andric lldb_private::ExecutionPolicy execution_policy = options.GetExecutionPolicy();
179ac9a064cSDimitry Andric SourceLanguage language = options.GetLanguage();
18014f1b3e8SDimitry Andric const ResultType desired_type = options.DoesCoerceToId()
18114f1b3e8SDimitry Andric ? UserExpression::eResultTypeId
18214f1b3e8SDimitry Andric : UserExpression::eResultTypeAny;
183e81d9d49SDimitry Andric lldb::ExpressionResults execution_results = lldb::eExpressionSetupError;
184e81d9d49SDimitry Andric
185e81d9d49SDimitry Andric Target *target = exe_ctx.GetTargetPtr();
18614f1b3e8SDimitry Andric if (!target) {
187cfca06d7SDimitry Andric LLDB_LOG(log, "== [UserExpression::Evaluate] Passed a NULL target, can't "
18814f1b3e8SDimitry Andric "run expressions.");
18914f1b3e8SDimitry Andric error.SetErrorString("expression passed a null target");
190e81d9d49SDimitry Andric return lldb::eExpressionSetupError;
191e81d9d49SDimitry Andric }
192e81d9d49SDimitry Andric
193e81d9d49SDimitry Andric Process *process = exe_ctx.GetProcessPtr();
194e81d9d49SDimitry Andric
195e3b55780SDimitry Andric if (process == nullptr && execution_policy == eExecutionPolicyAlways) {
196e3b55780SDimitry Andric LLDB_LOG(log, "== [UserExpression::Evaluate] No process, but the policy is "
197e3b55780SDimitry Andric "eExecutionPolicyAlways");
198e81d9d49SDimitry Andric
199e3b55780SDimitry Andric error.SetErrorString("expression needed to run but couldn't: no process");
200e81d9d49SDimitry Andric
201e81d9d49SDimitry Andric return execution_results;
202e81d9d49SDimitry Andric }
2037fa27ce4SDimitry Andric
2047fa27ce4SDimitry Andric // Since we might need to allocate memory, we need to be stopped to run
2057fa27ce4SDimitry Andric // an expression.
206e3b55780SDimitry Andric if (process != nullptr && process->GetState() != lldb::eStateStopped) {
2077fa27ce4SDimitry Andric error.SetErrorStringWithFormatv(
2087fa27ce4SDimitry Andric "unable to evaluate expression while the process is {0}: the process "
2097fa27ce4SDimitry Andric "must be stopped because the expression might require allocating "
2107fa27ce4SDimitry Andric "memory.",
2117fa27ce4SDimitry Andric StateAsCString(process->GetState()));
212e3b55780SDimitry Andric return execution_results;
213e81d9d49SDimitry Andric }
214e81d9d49SDimitry Andric
215344a3780SDimitry Andric // Explicitly force the IR interpreter to evaluate the expression when the
216344a3780SDimitry Andric // there is no process that supports running the expression for us. Don't
217344a3780SDimitry Andric // change the execution policy if we have the special top-level policy that
218344a3780SDimitry Andric // doesn't contain any expression and there is nothing to interpret.
219344a3780SDimitry Andric if (execution_policy != eExecutionPolicyTopLevel &&
220344a3780SDimitry Andric (process == nullptr || !process->CanJIT()))
221e81d9d49SDimitry Andric execution_policy = eExecutionPolicyNever;
222e81d9d49SDimitry Andric
22314f1b3e8SDimitry Andric // We need to set the expression execution thread here, turns out parse can
224f73363f1SDimitry Andric // call functions in the process of looking up symbols, which will escape the
225f73363f1SDimitry Andric // context set by exe_ctx passed to Execute.
226f3fbd1c0SDimitry Andric lldb::ThreadSP thread_sp = exe_ctx.GetThreadSP();
22714f1b3e8SDimitry Andric ThreadList::ExpressionExecutionThreadPusher execution_thread_pusher(
22814f1b3e8SDimitry Andric thread_sp);
229f3fbd1c0SDimitry Andric
23014f1b3e8SDimitry Andric llvm::StringRef full_prefix;
23114f1b3e8SDimitry Andric llvm::StringRef option_prefix(options.GetPrefix());
232e81d9d49SDimitry Andric std::string full_prefix_storage;
23314f1b3e8SDimitry Andric if (!prefix.empty() && !option_prefix.empty()) {
234cfca06d7SDimitry Andric full_prefix_storage = std::string(prefix);
235cfca06d7SDimitry Andric full_prefix_storage.append(std::string(option_prefix));
23614f1b3e8SDimitry Andric full_prefix = full_prefix_storage;
23714f1b3e8SDimitry Andric } else if (!prefix.empty())
23814f1b3e8SDimitry Andric full_prefix = prefix;
239e81d9d49SDimitry Andric else
240e81d9d49SDimitry Andric full_prefix = option_prefix;
241e81d9d49SDimitry Andric
242f73363f1SDimitry Andric // If the language was not specified in the expression command, set it to the
243f73363f1SDimitry Andric // language in the target's properties if specified, else default to the
244f73363f1SDimitry Andric // langage for the frame.
245ac9a064cSDimitry Andric if (!language) {
246e81d9d49SDimitry Andric if (target->GetLanguage() != lldb::eLanguageTypeUnknown)
247e81d9d49SDimitry Andric language = target->GetLanguage();
248e81d9d49SDimitry Andric else if (StackFrame *frame = exe_ctx.GetFramePtr())
249e81d9d49SDimitry Andric language = frame->GetLanguage();
250e81d9d49SDimitry Andric }
251e81d9d49SDimitry Andric
25214f1b3e8SDimitry Andric lldb::UserExpressionSP user_expression_sp(
25314f1b3e8SDimitry Andric target->GetUserExpressionForLanguage(expr, full_prefix, language,
2545f29bb8aSDimitry Andric desired_type, options, ctx_obj,
2555f29bb8aSDimitry Andric error));
256b1c73532SDimitry Andric if (error.Fail() || !user_expression_sp) {
257cfca06d7SDimitry Andric LLDB_LOG(log, "== [UserExpression::Evaluate] Getting expression: {0} ==",
25814f1b3e8SDimitry Andric error.AsCString());
259e81d9d49SDimitry Andric return lldb::eExpressionSetupError;
260e81d9d49SDimitry Andric }
261e81d9d49SDimitry Andric
262cfca06d7SDimitry Andric LLDB_LOG(log, "== [UserExpression::Evaluate] Parsing expression {0} ==",
263cfca06d7SDimitry Andric expr.str());
264e81d9d49SDimitry Andric
265e81d9d49SDimitry Andric const bool keep_expression_in_memory = true;
266e81d9d49SDimitry Andric const bool generate_debug_info = options.GetGenerateDebugInfo();
267e81d9d49SDimitry Andric
26814f1b3e8SDimitry Andric if (options.InvokeCancelCallback(lldb::eExpressionEvaluationParse)) {
269e81d9d49SDimitry Andric error.SetErrorString("expression interrupted by callback before parse");
27014f1b3e8SDimitry Andric result_valobj_sp = ValueObjectConstResult::Create(
27114f1b3e8SDimitry Andric exe_ctx.GetBestExecutionContextScope(), error);
272e81d9d49SDimitry Andric return lldb::eExpressionInterrupted;
273e81d9d49SDimitry Andric }
274e81d9d49SDimitry Andric
275f3fbd1c0SDimitry Andric DiagnosticManager diagnostic_manager;
276f3fbd1c0SDimitry Andric
27714f1b3e8SDimitry Andric bool parse_success =
27814f1b3e8SDimitry Andric user_expression_sp->Parse(diagnostic_manager, exe_ctx, execution_policy,
27914f1b3e8SDimitry Andric keep_expression_in_memory, generate_debug_info);
280f3fbd1c0SDimitry Andric
281f3fbd1c0SDimitry Andric // Calculate the fixed expression always, since we need it for errors.
282f3fbd1c0SDimitry Andric std::string tmp_fixed_expression;
283f3fbd1c0SDimitry Andric if (fixed_expression == nullptr)
284f3fbd1c0SDimitry Andric fixed_expression = &tmp_fixed_expression;
285f3fbd1c0SDimitry Andric
28677fc4c14SDimitry Andric *fixed_expression = user_expression_sp->GetFixedText().str();
287f3fbd1c0SDimitry Andric
288f3fbd1c0SDimitry Andric // If there is a fixed expression, try to parse it:
28914f1b3e8SDimitry Andric if (!parse_success) {
290cfca06d7SDimitry Andric // Delete the expression that failed to parse before attempting to parse
291cfca06d7SDimitry Andric // the next expression.
292cfca06d7SDimitry Andric user_expression_sp.reset();
293cfca06d7SDimitry Andric
294e81d9d49SDimitry Andric execution_results = lldb::eExpressionParseError;
29577fc4c14SDimitry Andric if (!fixed_expression->empty() && options.GetAutoApplyFixIts()) {
296cfca06d7SDimitry Andric const uint64_t max_fix_retries = options.GetRetriesWithFixIts();
297cfca06d7SDimitry Andric for (uint64_t i = 0; i < max_fix_retries; ++i) {
298cfca06d7SDimitry Andric // Try parsing the fixed expression.
29914f1b3e8SDimitry Andric lldb::UserExpressionSP fixed_expression_sp(
300cfca06d7SDimitry Andric target->GetUserExpressionForLanguage(
301cfca06d7SDimitry Andric fixed_expression->c_str(), full_prefix, language, desired_type,
302cfca06d7SDimitry Andric options, ctx_obj, error));
303ac9a064cSDimitry Andric if (!fixed_expression_sp)
304ac9a064cSDimitry Andric break;
305f3fbd1c0SDimitry Andric DiagnosticManager fixed_diagnostic_manager;
30614f1b3e8SDimitry Andric parse_success = fixed_expression_sp->Parse(
30714f1b3e8SDimitry Andric fixed_diagnostic_manager, exe_ctx, execution_policy,
30814f1b3e8SDimitry Andric keep_expression_in_memory, generate_debug_info);
30914f1b3e8SDimitry Andric if (parse_success) {
310f3fbd1c0SDimitry Andric diagnostic_manager.Clear();
311f3fbd1c0SDimitry Andric user_expression_sp = fixed_expression_sp;
312cfca06d7SDimitry Andric break;
313ac9a064cSDimitry Andric }
314cfca06d7SDimitry Andric // The fixed expression also didn't parse. Let's check for any new
315ac9a064cSDimitry Andric // fixits we could try.
31677fc4c14SDimitry Andric if (!fixed_expression_sp->GetFixedText().empty()) {
31777fc4c14SDimitry Andric *fixed_expression = fixed_expression_sp->GetFixedText().str();
318cfca06d7SDimitry Andric } else {
319cfca06d7SDimitry Andric // Fixed expression didn't compile without a fixit, don't retry and
320cfca06d7SDimitry Andric // don't tell the user about it.
321f3fbd1c0SDimitry Andric fixed_expression->clear();
322cfca06d7SDimitry Andric break;
323cfca06d7SDimitry Andric }
324cfca06d7SDimitry Andric }
325f3fbd1c0SDimitry Andric }
326f3fbd1c0SDimitry Andric
32714f1b3e8SDimitry Andric if (!parse_success) {
328344a3780SDimitry Andric std::string msg;
329344a3780SDimitry Andric {
330344a3780SDimitry Andric llvm::raw_string_ostream os(msg);
331344a3780SDimitry Andric if (!diagnostic_manager.Diagnostics().empty())
332344a3780SDimitry Andric os << diagnostic_manager.GetString();
333f3fbd1c0SDimitry Andric else
3347fa27ce4SDimitry Andric os << "expression failed to parse (no further compiler diagnostics)";
335344a3780SDimitry Andric if (target->GetEnableNotifyAboutFixIts() && fixed_expression &&
336344a3780SDimitry Andric !fixed_expression->empty())
337344a3780SDimitry Andric os << "\nfixed expression suggested:\n " << *fixed_expression;
338f3fbd1c0SDimitry Andric }
339344a3780SDimitry Andric error.SetExpressionError(execution_results, msg.c_str());
340f3fbd1c0SDimitry Andric }
341f3fbd1c0SDimitry Andric }
342f3fbd1c0SDimitry Andric
34314f1b3e8SDimitry Andric if (parse_success) {
344e81d9d49SDimitry Andric lldb::ExpressionVariableSP expr_result;
345e81d9d49SDimitry Andric
346e81d9d49SDimitry Andric if (execution_policy == eExecutionPolicyNever &&
34714f1b3e8SDimitry Andric !user_expression_sp->CanInterpret()) {
348cfca06d7SDimitry Andric LLDB_LOG(log, "== [UserExpression::Evaluate] Expression may not run, but "
34914f1b3e8SDimitry Andric "is not constant ==");
350e81d9d49SDimitry Andric
351f3fbd1c0SDimitry Andric if (!diagnostic_manager.Diagnostics().size())
35214f1b3e8SDimitry Andric error.SetExpressionError(lldb::eExpressionSetupError,
35314f1b3e8SDimitry Andric "expression needed to run but couldn't");
35414f1b3e8SDimitry Andric } else if (execution_policy == eExecutionPolicyTopLevel) {
355f3fbd1c0SDimitry Andric error.SetError(UserExpression::kNoResult, lldb::eErrorTypeGeneric);
356f3fbd1c0SDimitry Andric return lldb::eExpressionCompleted;
35714f1b3e8SDimitry Andric } else {
35814f1b3e8SDimitry Andric if (options.InvokeCancelCallback(lldb::eExpressionEvaluationExecution)) {
35914f1b3e8SDimitry Andric error.SetExpressionError(
36014f1b3e8SDimitry Andric lldb::eExpressionInterrupted,
36114f1b3e8SDimitry Andric "expression interrupted by callback before execution");
36214f1b3e8SDimitry Andric result_valobj_sp = ValueObjectConstResult::Create(
36314f1b3e8SDimitry Andric exe_ctx.GetBestExecutionContextScope(), error);
364e81d9d49SDimitry Andric return lldb::eExpressionInterrupted;
365e81d9d49SDimitry Andric }
366e81d9d49SDimitry Andric
367f3fbd1c0SDimitry Andric diagnostic_manager.Clear();
368e81d9d49SDimitry Andric
369cfca06d7SDimitry Andric LLDB_LOG(log, "== [UserExpression::Evaluate] Executing expression ==");
370e81d9d49SDimitry Andric
371f3fbd1c0SDimitry Andric execution_results =
37214f1b3e8SDimitry Andric user_expression_sp->Execute(diagnostic_manager, exe_ctx, options,
37314f1b3e8SDimitry Andric user_expression_sp, expr_result);
374e81d9d49SDimitry Andric
37514f1b3e8SDimitry Andric if (execution_results != lldb::eExpressionCompleted) {
376cfca06d7SDimitry Andric LLDB_LOG(log, "== [UserExpression::Evaluate] Execution completed "
37714f1b3e8SDimitry Andric "abnormally ==");
378e81d9d49SDimitry Andric
379f3fbd1c0SDimitry Andric if (!diagnostic_manager.Diagnostics().size())
38014f1b3e8SDimitry Andric error.SetExpressionError(
38114f1b3e8SDimitry Andric execution_results, "expression failed to execute, unknown error");
382e81d9d49SDimitry Andric else
38314f1b3e8SDimitry Andric error.SetExpressionError(execution_results,
38414f1b3e8SDimitry Andric diagnostic_manager.GetString().c_str());
38514f1b3e8SDimitry Andric } else {
38614f1b3e8SDimitry Andric if (expr_result) {
387e81d9d49SDimitry Andric result_valobj_sp = expr_result->GetValueObject();
388ac9a064cSDimitry Andric result_valobj_sp->SetPreferredDisplayLanguage(
389ac9a064cSDimitry Andric language.AsLanguageType());
390e81d9d49SDimitry Andric
391cfca06d7SDimitry Andric LLDB_LOG(log,
392ead24645SDimitry Andric "== [UserExpression::Evaluate] Execution completed "
393344a3780SDimitry Andric "normally with result {0} ==",
394e81d9d49SDimitry Andric result_valobj_sp->GetValueAsCString());
39514f1b3e8SDimitry Andric } else {
396cfca06d7SDimitry Andric LLDB_LOG(log, "== [UserExpression::Evaluate] Execution completed "
39714f1b3e8SDimitry Andric "normally with no result ==");
398e81d9d49SDimitry Andric
399e81d9d49SDimitry Andric error.SetError(UserExpression::kNoResult, lldb::eErrorTypeGeneric);
400e81d9d49SDimitry Andric }
401e81d9d49SDimitry Andric }
402e81d9d49SDimitry Andric }
403e81d9d49SDimitry Andric }
404e81d9d49SDimitry Andric
40514f1b3e8SDimitry Andric if (options.InvokeCancelCallback(lldb::eExpressionEvaluationComplete)) {
40614f1b3e8SDimitry Andric error.SetExpressionError(
40714f1b3e8SDimitry Andric lldb::eExpressionInterrupted,
40814f1b3e8SDimitry Andric "expression interrupted by callback after complete");
409e81d9d49SDimitry Andric return lldb::eExpressionInterrupted;
410e81d9d49SDimitry Andric }
411e81d9d49SDimitry Andric
4125f29bb8aSDimitry Andric if (result_valobj_sp.get() == nullptr) {
41314f1b3e8SDimitry Andric result_valobj_sp = ValueObjectConstResult::Create(
41414f1b3e8SDimitry Andric exe_ctx.GetBestExecutionContextScope(), error);
415e81d9d49SDimitry Andric }
416e81d9d49SDimitry Andric
417e81d9d49SDimitry Andric return execution_results;
418e81d9d49SDimitry Andric }
419f3fbd1c0SDimitry Andric
420f3fbd1c0SDimitry Andric lldb::ExpressionResults
Execute(DiagnosticManager & diagnostic_manager,ExecutionContext & exe_ctx,const EvaluateExpressionOptions & options,lldb::UserExpressionSP & shared_ptr_to_me,lldb::ExpressionVariableSP & result_var)421f3fbd1c0SDimitry Andric UserExpression::Execute(DiagnosticManager &diagnostic_manager,
422f3fbd1c0SDimitry Andric ExecutionContext &exe_ctx,
423f3fbd1c0SDimitry Andric const EvaluateExpressionOptions &options,
424f3fbd1c0SDimitry Andric lldb::UserExpressionSP &shared_ptr_to_me,
42514f1b3e8SDimitry Andric lldb::ExpressionVariableSP &result_var) {
42614f1b3e8SDimitry Andric lldb::ExpressionResults expr_result = DoExecute(
42714f1b3e8SDimitry Andric diagnostic_manager, exe_ctx, options, shared_ptr_to_me, result_var);
428f3fbd1c0SDimitry Andric Target *target = exe_ctx.GetTargetPtr();
4297fa27ce4SDimitry Andric if (options.GetSuppressPersistentResult() && result_var && target) {
430706b4fc4SDimitry Andric if (auto *persistent_state =
431ac9a064cSDimitry Andric target->GetPersistentExpressionStateForLanguage(
432ac9a064cSDimitry Andric m_language.AsLanguageType()))
433706b4fc4SDimitry Andric persistent_state->RemovePersistentVariable(result_var);
434f3fbd1c0SDimitry Andric }
435f3fbd1c0SDimitry Andric return expr_result;
436f3fbd1c0SDimitry Andric }
437