blob: 34945fdcbfa191c206c14163853ed637226f1d30 [file] [log] [blame]
Jim Ingham151c0322015-09-15 21:13:50 +00001//===-- UserExpression.cpp ---------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include <stdio.h>
11#if HAVE_SYS_TYPES_H
Kate Stoneb9c1b512016-09-06 20:57:50 +000012#include <sys/types.h>
Jim Ingham151c0322015-09-15 21:13:50 +000013#endif
14
15#include <cstdlib>
Jim Ingham151c0322015-09-15 21:13:50 +000016#include <map>
Kate Stoneb9c1b512016-09-06 20:57:50 +000017#include <string>
Jim Ingham151c0322015-09-15 21:13:50 +000018
Sean Callanan579e70c2016-03-19 00:03:59 +000019#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
Jim Ingham151c0322015-09-15 21:13:50 +000020#include "lldb/Core/Module.h"
21#include "lldb/Core/StreamFile.h"
Jim Ingham151c0322015-09-15 21:13:50 +000022#include "lldb/Core/ValueObjectConstResult.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000023#include "lldb/Expression/DiagnosticManager.h"
Jim Ingham151c0322015-09-15 21:13:50 +000024#include "lldb/Expression/ExpressionSourceCode.h"
25#include "lldb/Expression/IRExecutionUnit.h"
26#include "lldb/Expression/IRInterpreter.h"
27#include "lldb/Expression/Materializer.h"
28#include "lldb/Expression/UserExpression.h"
29#include "lldb/Host/HostInfo.h"
30#include "lldb/Symbol/Block.h"
Jim Ingham151c0322015-09-15 21:13:50 +000031#include "lldb/Symbol/Function.h"
32#include "lldb/Symbol/ObjectFile.h"
33#include "lldb/Symbol/SymbolVendor.h"
34#include "lldb/Symbol/Type.h"
Sean Callanan8f1f9a12015-09-30 19:57:57 +000035#include "lldb/Symbol/TypeSystem.h"
Jim Ingham151c0322015-09-15 21:13:50 +000036#include "lldb/Symbol/VariableList.h"
37#include "lldb/Target/ExecutionContext.h"
38#include "lldb/Target/Process.h"
39#include "lldb/Target/StackFrame.h"
40#include "lldb/Target/Target.h"
41#include "lldb/Target/ThreadPlan.h"
42#include "lldb/Target/ThreadPlanCallUserExpression.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000043#include "lldb/Utility/ConstString.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000044#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000045#include "lldb/Utility/StreamString.h"
Jim Ingham151c0322015-09-15 21:13:50 +000046
Jim Ingham151c0322015-09-15 21:13:50 +000047using namespace lldb_private;
48
Kate Stoneb9c1b512016-09-06 20:57:50 +000049UserExpression::UserExpression(ExecutionContextScope &exe_scope,
Zachary Turnerc5d7df92016-11-08 04:52:16 +000050 llvm::StringRef expr, llvm::StringRef prefix,
Kate Stoneb9c1b512016-09-06 20:57:50 +000051 lldb::LanguageType language,
52 ResultType desired_type,
53 const EvaluateExpressionOptions &options)
Zachary Turnerc5d7df92016-11-08 04:52:16 +000054 : Expression(exe_scope), m_expr_text(expr), m_expr_prefix(prefix),
55 m_language(language), m_desired_type(desired_type), m_options(options) {}
Kate Stoneb9c1b512016-09-06 20:57:50 +000056
57UserExpression::~UserExpression() {}
58
59void UserExpression::InstallContext(ExecutionContext &exe_ctx) {
60 m_jit_process_wp = exe_ctx.GetProcessSP();
61
62 lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP();
63
64 if (frame_sp)
65 m_address = frame_sp->GetFrameCodeAddress();
Jim Ingham151c0322015-09-15 21:13:50 +000066}
67
Kate Stoneb9c1b512016-09-06 20:57:50 +000068bool UserExpression::LockAndCheckContext(ExecutionContext &exe_ctx,
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();
Jim Ingham151c0322015-09-15 21:13:50 +000074
Kate Stoneb9c1b512016-09-06 20:57:50 +000075 if (process_sp != expected_process_sp)
76 return false;
Jim Ingham151c0322015-09-15 21:13:50 +000077
Kate Stoneb9c1b512016-09-06 20:57:50 +000078 process_sp = exe_ctx.GetProcessSP();
79 target_sp = exe_ctx.GetTargetSP();
80 frame_sp = exe_ctx.GetFrameSP();
Jim Ingham151c0322015-09-15 21:13:50 +000081
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 if (m_address.IsValid()) {
Jim Ingham151c0322015-09-15 21:13:50 +000083 if (!frame_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 return false;
85 else
86 return (0 == Address::CompareLoadAddress(m_address,
87 frame_sp->GetFrameCodeAddress(),
88 target_sp.get()));
89 }
Jim Ingham151c0322015-09-15 21:13:50 +000090
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 return true;
Jim Ingham151c0322015-09-15 21:13:50 +000092}
93
Kate Stoneb9c1b512016-09-06 20:57:50 +000094bool UserExpression::MatchesContext(ExecutionContext &exe_ctx) {
95 lldb::TargetSP target_sp;
96 lldb::ProcessSP process_sp;
97 lldb::StackFrameSP frame_sp;
Jim Ingham151c0322015-09-15 21:13:50 +000098
Kate Stoneb9c1b512016-09-06 20:57:50 +000099 return LockAndCheckContext(exe_ctx, target_sp, process_sp, frame_sp);
100}
Jim Ingham151c0322015-09-15 21:13:50 +0000101
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102lldb::addr_t UserExpression::GetObjectPointer(lldb::StackFrameSP frame_sp,
103 ConstString &object_name,
Zachary Turner97206d52017-05-12 04:51:55 +0000104 Status &err) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105 err.Clear();
Jim Ingham151c0322015-09-15 21:13:50 +0000106
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107 if (!frame_sp) {
108 err.SetErrorStringWithFormat(
109 "Couldn't load '%s' because the context is incomplete",
110 object_name.AsCString());
111 return LLDB_INVALID_ADDRESS;
112 }
Jim Ingham151c0322015-09-15 21:13:50 +0000113
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114 lldb::VariableSP var_sp;
115 lldb::ValueObjectSP valobj_sp;
Jim Ingham151c0322015-09-15 21:13:50 +0000116
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117 valobj_sp = frame_sp->GetValueForVariableExpressionPath(
118 object_name.AsCString(), lldb::eNoDynamicValues,
119 StackFrame::eExpressionPathOptionCheckPtrVsMember |
120 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
121 StackFrame::eExpressionPathOptionsNoSyntheticChildren |
122 StackFrame::eExpressionPathOptionsNoSyntheticArrayRange,
123 var_sp, err);
Jim Ingham151c0322015-09-15 21:13:50 +0000124
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125 if (!err.Success() || !valobj_sp.get())
126 return LLDB_INVALID_ADDRESS;
Jim Ingham151c0322015-09-15 21:13:50 +0000127
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128 lldb::addr_t ret = valobj_sp->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
Jim Ingham151c0322015-09-15 21:13:50 +0000129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 if (ret == LLDB_INVALID_ADDRESS) {
131 err.SetErrorStringWithFormat(
132 "Couldn't load '%s' because its value couldn't be evaluated",
133 object_name.AsCString());
134 return LLDB_INVALID_ADDRESS;
135 }
Dawn Perchik1bbaede2015-10-07 22:01:12 +0000136
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137 return ret;
138}
Jim Ingham151c0322015-09-15 21:13:50 +0000139
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140lldb::ExpressionResults UserExpression::Evaluate(
141 ExecutionContext &exe_ctx, const EvaluateExpressionOptions &options,
Zachary Turnerc5d7df92016-11-08 04:52:16 +0000142 llvm::StringRef expr, llvm::StringRef prefix,
Zachary Turner97206d52017-05-12 04:51:55 +0000143 lldb::ValueObjectSP &result_valobj_sp, Status &error, uint32_t line_offset,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144 std::string *fixed_expression, lldb::ModuleSP *jit_module_sp_ptr) {
145 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EXPRESSIONS |
146 LIBLLDB_LOG_STEP));
147
148 lldb_private::ExecutionPolicy execution_policy = options.GetExecutionPolicy();
149 lldb::LanguageType language = options.GetLanguage();
150 const ResultType desired_type = options.DoesCoerceToId()
151 ? UserExpression::eResultTypeId
152 : UserExpression::eResultTypeAny;
153 lldb::ExpressionResults execution_results = lldb::eExpressionSetupError;
154
155 Target *target = exe_ctx.GetTargetPtr();
156 if (!target) {
Jim Ingham151c0322015-09-15 21:13:50 +0000157 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158 log->Printf("== [UserExpression::Evaluate] Passed a NULL target, can't "
159 "run expressions.");
Jim Ingham02980822016-11-07 22:47:01 +0000160 error.SetErrorString("expression passed a null target");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161 return lldb::eExpressionSetupError;
162 }
Jim Ingham151c0322015-09-15 21:13:50 +0000163
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164 Process *process = exe_ctx.GetProcessPtr();
Jim Ingham151c0322015-09-15 21:13:50 +0000165
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166 if (process == NULL || process->GetState() != lldb::eStateStopped) {
167 if (execution_policy == eExecutionPolicyAlways) {
168 if (log)
169 log->Printf("== [UserExpression::Evaluate] Expression may not run, but "
170 "is not constant ==");
171
172 error.SetErrorString("expression needed to run but couldn't");
173
174 return execution_results;
175 }
176 }
177
178 if (process == NULL || !process->CanJIT())
179 execution_policy = eExecutionPolicyNever;
180
181 // We need to set the expression execution thread here, turns out parse can
Adrian Prantl05097242018-04-30 16:49:04 +0000182 // call functions in the process of looking up symbols, which will escape the
183 // context set by exe_ctx passed to Execute.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 lldb::ThreadSP thread_sp = exe_ctx.GetThreadSP();
185 ThreadList::ExpressionExecutionThreadPusher execution_thread_pusher(
186 thread_sp);
187
Zachary Turnerc5d7df92016-11-08 04:52:16 +0000188 llvm::StringRef full_prefix;
189 llvm::StringRef option_prefix(options.GetPrefix());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190 std::string full_prefix_storage;
Zachary Turnerc5d7df92016-11-08 04:52:16 +0000191 if (!prefix.empty() && !option_prefix.empty()) {
192 full_prefix_storage = prefix;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193 full_prefix_storage.append(option_prefix);
Zachary Turnerc5d7df92016-11-08 04:52:16 +0000194 full_prefix = full_prefix_storage;
195 } else if (!prefix.empty())
196 full_prefix = prefix;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197 else
198 full_prefix = option_prefix;
199
Adrian Prantl05097242018-04-30 16:49:04 +0000200 // If the language was not specified in the expression command, set it to the
201 // language in the target's properties if specified, else default to the
202 // langage for the frame.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000203 if (language == lldb::eLanguageTypeUnknown) {
204 if (target->GetLanguage() != lldb::eLanguageTypeUnknown)
205 language = target->GetLanguage();
206 else if (StackFrame *frame = exe_ctx.GetFramePtr())
207 language = frame->GetLanguage();
208 }
209
210 lldb::UserExpressionSP user_expression_sp(
Zachary Turnerc5d7df92016-11-08 04:52:16 +0000211 target->GetUserExpressionForLanguage(expr, full_prefix, language,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212 desired_type, options, error));
213 if (error.Fail()) {
214 if (log)
215 log->Printf("== [UserExpression::Evaluate] Getting expression: %s ==",
216 error.AsCString());
217 return lldb::eExpressionSetupError;
218 }
219
220 if (log)
221 log->Printf("== [UserExpression::Evaluate] Parsing expression %s ==",
Zachary Turnerc5d7df92016-11-08 04:52:16 +0000222 expr.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223
224 const bool keep_expression_in_memory = true;
225 const bool generate_debug_info = options.GetGenerateDebugInfo();
226
227 if (options.InvokeCancelCallback(lldb::eExpressionEvaluationParse)) {
228 error.SetErrorString("expression interrupted by callback before parse");
229 result_valobj_sp = ValueObjectConstResult::Create(
230 exe_ctx.GetBestExecutionContextScope(), error);
231 return lldb::eExpressionInterrupted;
232 }
233
234 DiagnosticManager diagnostic_manager;
235
236 bool parse_success =
237 user_expression_sp->Parse(diagnostic_manager, exe_ctx, execution_policy,
238 keep_expression_in_memory, generate_debug_info);
239
240 // Calculate the fixed expression always, since we need it for errors.
241 std::string tmp_fixed_expression;
242 if (fixed_expression == nullptr)
243 fixed_expression = &tmp_fixed_expression;
244
245 const char *fixed_text = user_expression_sp->GetFixedText();
246 if (fixed_text != nullptr)
247 fixed_expression->append(fixed_text);
248
249 // If there is a fixed expression, try to parse it:
250 if (!parse_success) {
251 execution_results = lldb::eExpressionParseError;
252 if (fixed_expression && !fixed_expression->empty() &&
253 options.GetAutoApplyFixIts()) {
254 lldb::UserExpressionSP fixed_expression_sp(
255 target->GetUserExpressionForLanguage(fixed_expression->c_str(),
256 full_prefix, language,
257 desired_type, options, error));
258 DiagnosticManager fixed_diagnostic_manager;
259 parse_success = fixed_expression_sp->Parse(
260 fixed_diagnostic_manager, exe_ctx, execution_policy,
261 keep_expression_in_memory, generate_debug_info);
262 if (parse_success) {
263 diagnostic_manager.Clear();
264 user_expression_sp = fixed_expression_sp;
265 } else {
266 // If the fixed expression failed to parse, don't tell the user about,
267 // that won't help.
268 fixed_expression->clear();
269 }
Jim Ingham151c0322015-09-15 21:13:50 +0000270 }
271
Kate Stoneb9c1b512016-09-06 20:57:50 +0000272 if (!parse_success) {
273 if (!fixed_expression->empty() && target->GetEnableNotifyAboutFixIts()) {
274 error.SetExpressionErrorWithFormat(
275 execution_results,
276 "expression failed to parse, fixed expression suggested:\n %s",
277 fixed_expression->c_str());
278 } else {
279 if (!diagnostic_manager.Diagnostics().size())
280 error.SetExpressionError(execution_results,
281 "expression failed to parse, unknown error");
Jim Ingham151c0322015-09-15 21:13:50 +0000282 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000283 error.SetExpressionError(execution_results,
284 diagnostic_manager.GetString().c_str());
285 }
Jim Ingham151c0322015-09-15 21:13:50 +0000286 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000287 }
Jim Ingham151c0322015-09-15 21:13:50 +0000288
Kate Stoneb9c1b512016-09-06 20:57:50 +0000289 if (parse_success) {
290 // If a pointer to a lldb::ModuleSP was passed in, return the JIT'ed module
291 // if one was created
292 if (jit_module_sp_ptr)
293 *jit_module_sp_ptr = user_expression_sp->GetJITModule();
294
295 lldb::ExpressionVariableSP expr_result;
296
297 if (execution_policy == eExecutionPolicyNever &&
298 !user_expression_sp->CanInterpret()) {
299 if (log)
300 log->Printf("== [UserExpression::Evaluate] Expression may not run, but "
301 "is not constant ==");
302
303 if (!diagnostic_manager.Diagnostics().size())
304 error.SetExpressionError(lldb::eExpressionSetupError,
305 "expression needed to run but couldn't");
306 } else if (execution_policy == eExecutionPolicyTopLevel) {
Krasimir Georgieva35912d2018-10-18 03:10:43 +0000307 error.SetError(UserExpression::kNoResult, lldb::eErrorTypeGeneric);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000308 return lldb::eExpressionCompleted;
309 } else {
310 if (options.InvokeCancelCallback(lldb::eExpressionEvaluationExecution)) {
311 error.SetExpressionError(
312 lldb::eExpressionInterrupted,
313 "expression interrupted by callback before execution");
314 result_valobj_sp = ValueObjectConstResult::Create(
315 exe_ctx.GetBestExecutionContextScope(), error);
Jim Ingham151c0322015-09-15 21:13:50 +0000316 return lldb::eExpressionInterrupted;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000317 }
Jim Ingham151c0322015-09-15 21:13:50 +0000318
Kate Stoneb9c1b512016-09-06 20:57:50 +0000319 diagnostic_manager.Clear();
Jim Ingham151c0322015-09-15 21:13:50 +0000320
Kate Stoneb9c1b512016-09-06 20:57:50 +0000321 if (log)
322 log->Printf("== [UserExpression::Evaluate] Executing expression ==");
323
324 execution_results =
325 user_expression_sp->Execute(diagnostic_manager, exe_ctx, options,
326 user_expression_sp, expr_result);
327
328 if (execution_results != lldb::eExpressionCompleted) {
329 if (log)
330 log->Printf("== [UserExpression::Evaluate] Execution completed "
331 "abnormally ==");
332
333 if (!diagnostic_manager.Diagnostics().size())
334 error.SetExpressionError(
335 execution_results, "expression failed to execute, unknown error");
336 else
337 error.SetExpressionError(execution_results,
338 diagnostic_manager.GetString().c_str());
339 } else {
340 if (expr_result) {
341 result_valobj_sp = expr_result->GetValueObject();
342
343 if (log)
344 log->Printf("== [UserExpression::Evaluate] Execution completed "
345 "normally with result %s ==",
346 result_valobj_sp->GetValueAsCString());
347 } else {
348 if (log)
349 log->Printf("== [UserExpression::Evaluate] Execution completed "
350 "normally with no result ==");
351
Krasimir Georgieva35912d2018-10-18 03:10:43 +0000352 error.SetError(UserExpression::kNoResult, lldb::eErrorTypeGeneric);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 }
354 }
355 }
356 }
357
358 if (options.InvokeCancelCallback(lldb::eExpressionEvaluationComplete)) {
359 error.SetExpressionError(
360 lldb::eExpressionInterrupted,
361 "expression interrupted by callback after complete");
362 return lldb::eExpressionInterrupted;
363 }
364
365 if (result_valobj_sp.get() == NULL) {
366 result_valobj_sp = ValueObjectConstResult::Create(
367 exe_ctx.GetBestExecutionContextScope(), error);
368 }
369
370 return execution_results;
Jim Ingham151c0322015-09-15 21:13:50 +0000371}
Jim Inghamff7ac6a2016-04-12 17:17:35 +0000372
373lldb::ExpressionResults
374UserExpression::Execute(DiagnosticManager &diagnostic_manager,
375 ExecutionContext &exe_ctx,
376 const EvaluateExpressionOptions &options,
377 lldb::UserExpressionSP &shared_ptr_to_me,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378 lldb::ExpressionVariableSP &result_var) {
379 lldb::ExpressionResults expr_result = DoExecute(
380 diagnostic_manager, exe_ctx, options, shared_ptr_to_me, result_var);
381 Target *target = exe_ctx.GetTargetPtr();
382 if (options.GetResultIsInternal() && result_var && target) {
383 target->GetPersistentExpressionStateForLanguage(m_language)
384 ->RemovePersistentVariable(result_var);
385 }
386 return expr_result;
Jim Inghamff7ac6a2016-04-12 17:17:35 +0000387}