blob: 52ef6fa70033f784d67017d70352c46dcacda379 [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/ConstString.h"
21#include "lldb/Core/Log.h"
22#include "lldb/Core/Module.h"
23#include "lldb/Core/StreamFile.h"
24#include "lldb/Core/StreamString.h"
25#include "lldb/Core/ValueObjectConstResult.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000026#include "lldb/Expression/DiagnosticManager.h"
Jim Ingham151c0322015-09-15 21:13:50 +000027#include "lldb/Expression/ExpressionSourceCode.h"
28#include "lldb/Expression/IRExecutionUnit.h"
29#include "lldb/Expression/IRInterpreter.h"
30#include "lldb/Expression/Materializer.h"
31#include "lldb/Expression/UserExpression.h"
32#include "lldb/Host/HostInfo.h"
33#include "lldb/Symbol/Block.h"
Jim Ingham151c0322015-09-15 21:13:50 +000034#include "lldb/Symbol/Function.h"
35#include "lldb/Symbol/ObjectFile.h"
36#include "lldb/Symbol/SymbolVendor.h"
37#include "lldb/Symbol/Type.h"
Sean Callanan8f1f9a12015-09-30 19:57:57 +000038#include "lldb/Symbol/TypeSystem.h"
Jim Ingham151c0322015-09-15 21:13:50 +000039#include "lldb/Symbol/VariableList.h"
40#include "lldb/Target/ExecutionContext.h"
41#include "lldb/Target/Process.h"
42#include "lldb/Target/StackFrame.h"
43#include "lldb/Target/Target.h"
44#include "lldb/Target/ThreadPlan.h"
45#include "lldb/Target/ThreadPlanCallUserExpression.h"
46
Jim Ingham151c0322015-09-15 21:13:50 +000047using namespace lldb_private;
48
Kate Stoneb9c1b512016-09-06 20:57:50 +000049UserExpression::UserExpression(ExecutionContextScope &exe_scope,
50 const char *expr, const char *expr_prefix,
51 lldb::LanguageType language,
52 ResultType desired_type,
53 const EvaluateExpressionOptions &options)
54 : Expression(exe_scope), m_expr_text(expr),
55 m_expr_prefix(expr_prefix ? expr_prefix : ""), m_language(language),
56 m_desired_type(desired_type), m_options(options) {}
57
58UserExpression::~UserExpression() {}
59
60void UserExpression::InstallContext(ExecutionContext &exe_ctx) {
61 m_jit_process_wp = exe_ctx.GetProcessSP();
62
63 lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP();
64
65 if (frame_sp)
66 m_address = frame_sp->GetFrameCodeAddress();
Jim Ingham151c0322015-09-15 21:13:50 +000067}
68
Kate Stoneb9c1b512016-09-06 20:57:50 +000069bool UserExpression::LockAndCheckContext(ExecutionContext &exe_ctx,
70 lldb::TargetSP &target_sp,
71 lldb::ProcessSP &process_sp,
72 lldb::StackFrameSP &frame_sp) {
73 lldb::ProcessSP expected_process_sp = m_jit_process_wp.lock();
74 process_sp = exe_ctx.GetProcessSP();
Jim Ingham151c0322015-09-15 21:13:50 +000075
Kate Stoneb9c1b512016-09-06 20:57:50 +000076 if (process_sp != expected_process_sp)
77 return false;
Jim Ingham151c0322015-09-15 21:13:50 +000078
Kate Stoneb9c1b512016-09-06 20:57:50 +000079 process_sp = exe_ctx.GetProcessSP();
80 target_sp = exe_ctx.GetTargetSP();
81 frame_sp = exe_ctx.GetFrameSP();
Jim Ingham151c0322015-09-15 21:13:50 +000082
Kate Stoneb9c1b512016-09-06 20:57:50 +000083 if (m_address.IsValid()) {
Jim Ingham151c0322015-09-15 21:13:50 +000084 if (!frame_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 return false;
86 else
87 return (0 == Address::CompareLoadAddress(m_address,
88 frame_sp->GetFrameCodeAddress(),
89 target_sp.get()));
90 }
Jim Ingham151c0322015-09-15 21:13:50 +000091
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 return true;
Jim Ingham151c0322015-09-15 21:13:50 +000093}
94
Kate Stoneb9c1b512016-09-06 20:57:50 +000095bool UserExpression::MatchesContext(ExecutionContext &exe_ctx) {
96 lldb::TargetSP target_sp;
97 lldb::ProcessSP process_sp;
98 lldb::StackFrameSP frame_sp;
Jim Ingham151c0322015-09-15 21:13:50 +000099
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100 return LockAndCheckContext(exe_ctx, target_sp, process_sp, frame_sp);
101}
Jim Ingham151c0322015-09-15 21:13:50 +0000102
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103lldb::addr_t UserExpression::GetObjectPointer(lldb::StackFrameSP frame_sp,
104 ConstString &object_name,
105 Error &err) {
106 err.Clear();
Jim Ingham151c0322015-09-15 21:13:50 +0000107
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 if (!frame_sp) {
109 err.SetErrorStringWithFormat(
110 "Couldn't load '%s' because the context is incomplete",
111 object_name.AsCString());
112 return LLDB_INVALID_ADDRESS;
113 }
Jim Ingham151c0322015-09-15 21:13:50 +0000114
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115 lldb::VariableSP var_sp;
116 lldb::ValueObjectSP valobj_sp;
Jim Ingham151c0322015-09-15 21:13:50 +0000117
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118 valobj_sp = frame_sp->GetValueForVariableExpressionPath(
119 object_name.AsCString(), lldb::eNoDynamicValues,
120 StackFrame::eExpressionPathOptionCheckPtrVsMember |
121 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
122 StackFrame::eExpressionPathOptionsNoSyntheticChildren |
123 StackFrame::eExpressionPathOptionsNoSyntheticArrayRange,
124 var_sp, err);
Jim Ingham151c0322015-09-15 21:13:50 +0000125
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126 if (!err.Success() || !valobj_sp.get())
127 return LLDB_INVALID_ADDRESS;
Jim Ingham151c0322015-09-15 21:13:50 +0000128
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 lldb::addr_t ret = valobj_sp->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
Jim Ingham151c0322015-09-15 21:13:50 +0000130
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131 if (ret == LLDB_INVALID_ADDRESS) {
132 err.SetErrorStringWithFormat(
133 "Couldn't load '%s' because its value couldn't be evaluated",
134 object_name.AsCString());
135 return LLDB_INVALID_ADDRESS;
136 }
Dawn Perchik1bbaede2015-10-07 22:01:12 +0000137
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138 return ret;
139}
Jim Ingham151c0322015-09-15 21:13:50 +0000140
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141lldb::ExpressionResults UserExpression::Evaluate(
142 ExecutionContext &exe_ctx, const EvaluateExpressionOptions &options,
143 const char *expr_cstr, const char *expr_prefix,
144 lldb::ValueObjectSP &result_valobj_sp, Error &error, uint32_t line_offset,
145 std::string *fixed_expression, lldb::ModuleSP *jit_module_sp_ptr) {
146 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EXPRESSIONS |
147 LIBLLDB_LOG_STEP));
148
149 lldb_private::ExecutionPolicy execution_policy = options.GetExecutionPolicy();
150 lldb::LanguageType language = options.GetLanguage();
151 const ResultType desired_type = options.DoesCoerceToId()
152 ? UserExpression::eResultTypeId
153 : UserExpression::eResultTypeAny;
154 lldb::ExpressionResults execution_results = lldb::eExpressionSetupError;
155
156 Target *target = exe_ctx.GetTargetPtr();
157 if (!target) {
Jim Ingham151c0322015-09-15 21:13:50 +0000158 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000159 log->Printf("== [UserExpression::Evaluate] Passed a NULL target, can't "
160 "run expressions.");
Jim Ingham02980822016-11-07 22:47:01 +0000161 error.SetErrorString("expression passed a null target");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 return lldb::eExpressionSetupError;
163 }
Jim Ingham151c0322015-09-15 21:13:50 +0000164
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165 Process *process = exe_ctx.GetProcessPtr();
Jim Ingham151c0322015-09-15 21:13:50 +0000166
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167 if (process == NULL || process->GetState() != lldb::eStateStopped) {
168 if (execution_policy == eExecutionPolicyAlways) {
169 if (log)
170 log->Printf("== [UserExpression::Evaluate] Expression may not run, but "
171 "is not constant ==");
172
173 error.SetErrorString("expression needed to run but couldn't");
174
175 return execution_results;
176 }
177 }
178
179 if (process == NULL || !process->CanJIT())
180 execution_policy = eExecutionPolicyNever;
181
182 // We need to set the expression execution thread here, turns out parse can
183 // call functions in the process of
184 // looking up symbols, which will escape the context set by exe_ctx passed to
185 // Execute.
186 lldb::ThreadSP thread_sp = exe_ctx.GetThreadSP();
187 ThreadList::ExpressionExecutionThreadPusher execution_thread_pusher(
188 thread_sp);
189
190 const char *full_prefix = NULL;
191 const char *option_prefix = options.GetPrefix();
192 std::string full_prefix_storage;
193 if (expr_prefix && option_prefix) {
194 full_prefix_storage.assign(expr_prefix);
195 full_prefix_storage.append(option_prefix);
196 if (!full_prefix_storage.empty())
197 full_prefix = full_prefix_storage.c_str();
198 } else if (expr_prefix)
199 full_prefix = expr_prefix;
200 else
201 full_prefix = option_prefix;
202
203 // If the language was not specified in the expression command,
204 // set it to the language in the target's properties if
205 // specified, else default to the langage for the frame.
206 if (language == lldb::eLanguageTypeUnknown) {
207 if (target->GetLanguage() != lldb::eLanguageTypeUnknown)
208 language = target->GetLanguage();
209 else if (StackFrame *frame = exe_ctx.GetFramePtr())
210 language = frame->GetLanguage();
211 }
212
213 lldb::UserExpressionSP user_expression_sp(
214 target->GetUserExpressionForLanguage(expr_cstr, full_prefix, language,
215 desired_type, options, error));
216 if (error.Fail()) {
217 if (log)
218 log->Printf("== [UserExpression::Evaluate] Getting expression: %s ==",
219 error.AsCString());
220 return lldb::eExpressionSetupError;
221 }
222
223 if (log)
224 log->Printf("== [UserExpression::Evaluate] Parsing expression %s ==",
225 expr_cstr);
226
227 const bool keep_expression_in_memory = true;
228 const bool generate_debug_info = options.GetGenerateDebugInfo();
229
230 if (options.InvokeCancelCallback(lldb::eExpressionEvaluationParse)) {
231 error.SetErrorString("expression interrupted by callback before parse");
232 result_valobj_sp = ValueObjectConstResult::Create(
233 exe_ctx.GetBestExecutionContextScope(), error);
234 return lldb::eExpressionInterrupted;
235 }
236
237 DiagnosticManager diagnostic_manager;
238
239 bool parse_success =
240 user_expression_sp->Parse(diagnostic_manager, exe_ctx, execution_policy,
241 keep_expression_in_memory, generate_debug_info);
242
243 // Calculate the fixed expression always, since we need it for errors.
244 std::string tmp_fixed_expression;
245 if (fixed_expression == nullptr)
246 fixed_expression = &tmp_fixed_expression;
247
248 const char *fixed_text = user_expression_sp->GetFixedText();
249 if (fixed_text != nullptr)
250 fixed_expression->append(fixed_text);
251
252 // If there is a fixed expression, try to parse it:
253 if (!parse_success) {
254 execution_results = lldb::eExpressionParseError;
255 if (fixed_expression && !fixed_expression->empty() &&
256 options.GetAutoApplyFixIts()) {
257 lldb::UserExpressionSP fixed_expression_sp(
258 target->GetUserExpressionForLanguage(fixed_expression->c_str(),
259 full_prefix, language,
260 desired_type, options, error));
261 DiagnosticManager fixed_diagnostic_manager;
262 parse_success = fixed_expression_sp->Parse(
263 fixed_diagnostic_manager, exe_ctx, execution_policy,
264 keep_expression_in_memory, generate_debug_info);
265 if (parse_success) {
266 diagnostic_manager.Clear();
267 user_expression_sp = fixed_expression_sp;
268 } else {
269 // If the fixed expression failed to parse, don't tell the user about,
270 // that won't help.
271 fixed_expression->clear();
272 }
Jim Ingham151c0322015-09-15 21:13:50 +0000273 }
274
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275 if (!parse_success) {
276 if (!fixed_expression->empty() && target->GetEnableNotifyAboutFixIts()) {
277 error.SetExpressionErrorWithFormat(
278 execution_results,
279 "expression failed to parse, fixed expression suggested:\n %s",
280 fixed_expression->c_str());
281 } else {
282 if (!diagnostic_manager.Diagnostics().size())
283 error.SetExpressionError(execution_results,
284 "expression failed to parse, unknown error");
Jim Ingham151c0322015-09-15 21:13:50 +0000285 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000286 error.SetExpressionError(execution_results,
287 diagnostic_manager.GetString().c_str());
288 }
Jim Ingham151c0322015-09-15 21:13:50 +0000289 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000290 }
Jim Ingham151c0322015-09-15 21:13:50 +0000291
Kate Stoneb9c1b512016-09-06 20:57:50 +0000292 if (parse_success) {
293 // If a pointer to a lldb::ModuleSP was passed in, return the JIT'ed module
294 // if one was created
295 if (jit_module_sp_ptr)
296 *jit_module_sp_ptr = user_expression_sp->GetJITModule();
297
298 lldb::ExpressionVariableSP expr_result;
299
300 if (execution_policy == eExecutionPolicyNever &&
301 !user_expression_sp->CanInterpret()) {
302 if (log)
303 log->Printf("== [UserExpression::Evaluate] Expression may not run, but "
304 "is not constant ==");
305
306 if (!diagnostic_manager.Diagnostics().size())
307 error.SetExpressionError(lldb::eExpressionSetupError,
308 "expression needed to run but couldn't");
309 } else if (execution_policy == eExecutionPolicyTopLevel) {
310 error.SetError(UserExpression::kNoResult, lldb::eErrorTypeGeneric);
311 return lldb::eExpressionCompleted;
312 } else {
313 if (options.InvokeCancelCallback(lldb::eExpressionEvaluationExecution)) {
314 error.SetExpressionError(
315 lldb::eExpressionInterrupted,
316 "expression interrupted by callback before execution");
317 result_valobj_sp = ValueObjectConstResult::Create(
318 exe_ctx.GetBestExecutionContextScope(), error);
Jim Ingham151c0322015-09-15 21:13:50 +0000319 return lldb::eExpressionInterrupted;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000320 }
Jim Ingham151c0322015-09-15 21:13:50 +0000321
Kate Stoneb9c1b512016-09-06 20:57:50 +0000322 diagnostic_manager.Clear();
Jim Ingham151c0322015-09-15 21:13:50 +0000323
Kate Stoneb9c1b512016-09-06 20:57:50 +0000324 if (log)
325 log->Printf("== [UserExpression::Evaluate] Executing expression ==");
326
327 execution_results =
328 user_expression_sp->Execute(diagnostic_manager, exe_ctx, options,
329 user_expression_sp, expr_result);
330
331 if (execution_results != lldb::eExpressionCompleted) {
332 if (log)
333 log->Printf("== [UserExpression::Evaluate] Execution completed "
334 "abnormally ==");
335
336 if (!diagnostic_manager.Diagnostics().size())
337 error.SetExpressionError(
338 execution_results, "expression failed to execute, unknown error");
339 else
340 error.SetExpressionError(execution_results,
341 diagnostic_manager.GetString().c_str());
342 } else {
343 if (expr_result) {
344 result_valobj_sp = expr_result->GetValueObject();
345
346 if (log)
347 log->Printf("== [UserExpression::Evaluate] Execution completed "
348 "normally with result %s ==",
349 result_valobj_sp->GetValueAsCString());
350 } else {
351 if (log)
352 log->Printf("== [UserExpression::Evaluate] Execution completed "
353 "normally with no result ==");
354
355 error.SetError(UserExpression::kNoResult, lldb::eErrorTypeGeneric);
356 }
357 }
358 }
359 }
360
361 if (options.InvokeCancelCallback(lldb::eExpressionEvaluationComplete)) {
362 error.SetExpressionError(
363 lldb::eExpressionInterrupted,
364 "expression interrupted by callback after complete");
365 return lldb::eExpressionInterrupted;
366 }
367
368 if (result_valobj_sp.get() == NULL) {
369 result_valobj_sp = ValueObjectConstResult::Create(
370 exe_ctx.GetBestExecutionContextScope(), error);
371 }
372
373 return execution_results;
Jim Ingham151c0322015-09-15 21:13:50 +0000374}
Jim Inghamff7ac6a2016-04-12 17:17:35 +0000375
376lldb::ExpressionResults
377UserExpression::Execute(DiagnosticManager &diagnostic_manager,
378 ExecutionContext &exe_ctx,
379 const EvaluateExpressionOptions &options,
380 lldb::UserExpressionSP &shared_ptr_to_me,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000381 lldb::ExpressionVariableSP &result_var) {
382 lldb::ExpressionResults expr_result = DoExecute(
383 diagnostic_manager, exe_ctx, options, shared_ptr_to_me, result_var);
384 Target *target = exe_ctx.GetTargetPtr();
385 if (options.GetResultIsInternal() && result_var && target) {
386 target->GetPersistentExpressionStateForLanguage(m_language)
387 ->RemovePersistentVariable(result_var);
388 }
389 return expr_result;
Jim Inghamff7ac6a2016-04-12 17:17:35 +0000390}