blob: 6b97eb68b1173e203498a256399cdfc8009db0b5 [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
12# include <sys/types.h>
13#endif
14
15#include <cstdlib>
16#include <string>
17#include <map>
18
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
Jim Ingham19a63fc2015-11-03 02:11:24 +000049UserExpression::UserExpression (ExecutionContextScope &exe_scope,
50 const char *expr,
51 const char *expr_prefix,
52 lldb::LanguageType language,
53 ResultType desired_type,
54 const EvaluateExpressionOptions &options) :
55 Expression(exe_scope),
Ryan Brown998c8a1c12015-11-02 19:30:40 +000056 m_expr_text(expr),
57 m_expr_prefix(expr_prefix ? expr_prefix : ""),
58 m_language(language),
Jim Ingham19a63fc2015-11-03 02:11:24 +000059 m_desired_type(desired_type),
60 m_options (options)
Jim Ingham151c0322015-09-15 21:13:50 +000061{
62}
63
64UserExpression::~UserExpression ()
65{
Jim Ingham151c0322015-09-15 21:13:50 +000066}
67
68void
69UserExpression::InstallContext (ExecutionContext &exe_ctx)
70{
71 m_jit_process_wp = exe_ctx.GetProcessSP();
72
73 lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP();
74
75 if (frame_sp)
76 m_address = frame_sp->GetFrameCodeAddress();
77}
78
79bool
80UserExpression::LockAndCheckContext (ExecutionContext &exe_ctx,
81 lldb::TargetSP &target_sp,
82 lldb::ProcessSP &process_sp,
83 lldb::StackFrameSP &frame_sp)
84{
85 lldb::ProcessSP expected_process_sp = m_jit_process_wp.lock();
86 process_sp = exe_ctx.GetProcessSP();
87
88 if (process_sp != expected_process_sp)
89 return false;
90
91 process_sp = exe_ctx.GetProcessSP();
92 target_sp = exe_ctx.GetTargetSP();
93 frame_sp = exe_ctx.GetFrameSP();
94
95 if (m_address.IsValid())
96 {
97 if (!frame_sp)
98 return false;
99 else
100 return (0 == Address::CompareLoadAddress(m_address, frame_sp->GetFrameCodeAddress(), target_sp.get()));
101 }
102
103 return true;
104}
105
106bool
107UserExpression::MatchesContext (ExecutionContext &exe_ctx)
108{
109 lldb::TargetSP target_sp;
110 lldb::ProcessSP process_sp;
111 lldb::StackFrameSP frame_sp;
112
113 return LockAndCheckContext(exe_ctx, target_sp, process_sp, frame_sp);
114}
115
116lldb::addr_t
117UserExpression::GetObjectPointer (lldb::StackFrameSP frame_sp,
118 ConstString &object_name,
119 Error &err)
120{
121 err.Clear();
122
123 if (!frame_sp)
124 {
125 err.SetErrorStringWithFormat("Couldn't load '%s' because the context is incomplete", object_name.AsCString());
126 return LLDB_INVALID_ADDRESS;
127 }
128
129 lldb::VariableSP var_sp;
130 lldb::ValueObjectSP valobj_sp;
131
132 valobj_sp = frame_sp->GetValueForVariableExpressionPath(object_name.AsCString(),
133 lldb::eNoDynamicValues,
134 StackFrame::eExpressionPathOptionCheckPtrVsMember |
135 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
136 StackFrame::eExpressionPathOptionsNoSyntheticChildren |
137 StackFrame::eExpressionPathOptionsNoSyntheticArrayRange,
138 var_sp,
139 err);
140
141 if (!err.Success() || !valobj_sp.get())
142 return LLDB_INVALID_ADDRESS;
143
144 lldb::addr_t ret = valobj_sp->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
145
146 if (ret == LLDB_INVALID_ADDRESS)
147 {
148 err.SetErrorStringWithFormat("Couldn't load '%s' because its value couldn't be evaluated", object_name.AsCString());
149 return LLDB_INVALID_ADDRESS;
150 }
151
152 return ret;
153}
154
Jim Ingham151c0322015-09-15 21:13:50 +0000155lldb::ExpressionResults
156UserExpression::Evaluate (ExecutionContext &exe_ctx,
157 const EvaluateExpressionOptions& options,
158 const char *expr_cstr,
159 const char *expr_prefix,
160 lldb::ValueObjectSP &result_valobj_sp,
Sean Callanan66810412015-10-19 23:11:07 +0000161 Error &error,
162 uint32_t line_offset,
163 lldb::ModuleSP *jit_module_sp_ptr)
Jim Ingham151c0322015-09-15 21:13:50 +0000164{
165 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
166
167 lldb_private::ExecutionPolicy execution_policy = options.GetExecutionPolicy();
Dawn Perchik1bbaede2015-10-07 22:01:12 +0000168 lldb::LanguageType language = options.GetLanguage();
Jim Ingham151c0322015-09-15 21:13:50 +0000169 const ResultType desired_type = options.DoesCoerceToId() ? UserExpression::eResultTypeId : UserExpression::eResultTypeAny;
170 lldb::ExpressionResults execution_results = lldb::eExpressionSetupError;
171
172 Target *target = exe_ctx.GetTargetPtr();
173 if (!target)
174 {
175 if (log)
176 log->Printf("== [UserExpression::Evaluate] Passed a NULL target, can't run expressions.");
177 return lldb::eExpressionSetupError;
178 }
179
180 Process *process = exe_ctx.GetProcessPtr();
181
182 if (process == NULL || process->GetState() != lldb::eStateStopped)
183 {
184 if (execution_policy == eExecutionPolicyAlways)
185 {
186 if (log)
187 log->Printf("== [UserExpression::Evaluate] Expression may not run, but is not constant ==");
188
189 error.SetErrorString ("expression needed to run but couldn't");
190
191 return execution_results;
192 }
193 }
194
195 if (process == NULL || !process->CanJIT())
196 execution_policy = eExecutionPolicyNever;
197
198 const char *full_prefix = NULL;
199 const char *option_prefix = options.GetPrefix();
200 std::string full_prefix_storage;
201 if (expr_prefix && option_prefix)
202 {
203 full_prefix_storage.assign(expr_prefix);
204 full_prefix_storage.append(option_prefix);
205 if (!full_prefix_storage.empty())
206 full_prefix = full_prefix_storage.c_str();
207 }
208 else if (expr_prefix)
209 full_prefix = expr_prefix;
210 else
211 full_prefix = option_prefix;
212
Dawn Perchik1bbaede2015-10-07 22:01:12 +0000213 // If the language was not specified in the expression command,
214 // set it to the language in the target's properties if
215 // specified, else default to the langage for the frame.
216 if (language == lldb::eLanguageTypeUnknown)
217 {
218 if (target->GetLanguage() != lldb::eLanguageTypeUnknown)
219 language = target->GetLanguage();
220 else if (StackFrame *frame = exe_ctx.GetFramePtr())
221 language = frame->GetLanguage();
222 }
223
Jim Ingham151c0322015-09-15 21:13:50 +0000224 lldb::UserExpressionSP user_expression_sp(target->GetUserExpressionForLanguage (expr_cstr,
225 full_prefix,
226 language,
227 desired_type,
Jim Ingham19a63fc2015-11-03 02:11:24 +0000228 options,
Jim Ingham151c0322015-09-15 21:13:50 +0000229 error));
230 if (error.Fail())
231 {
232 if (log)
233 log->Printf ("== [UserExpression::Evaluate] Getting expression: %s ==", error.AsCString());
234 return lldb::eExpressionSetupError;
235 }
Jim Ingham151c0322015-09-15 21:13:50 +0000236
237 if (log)
238 log->Printf("== [UserExpression::Evaluate] Parsing expression %s ==", expr_cstr);
239
240 const bool keep_expression_in_memory = true;
241 const bool generate_debug_info = options.GetGenerateDebugInfo();
242
243 if (options.InvokeCancelCallback (lldb::eExpressionEvaluationParse))
244 {
245 error.SetErrorString ("expression interrupted by callback before parse");
Sean Callanan579e70c2016-03-19 00:03:59 +0000246 result_valobj_sp = ValueObjectConstResult::Create(exe_ctx.GetBestExecutionContextScope(), error);
Jim Ingham151c0322015-09-15 21:13:50 +0000247 return lldb::eExpressionInterrupted;
248 }
249
Sean Callanan579e70c2016-03-19 00:03:59 +0000250 DiagnosticManager diagnostic_manager;
251
252 if (!user_expression_sp->Parse(diagnostic_manager, exe_ctx, execution_policy, keep_expression_in_memory,
253 generate_debug_info))
Jim Ingham151c0322015-09-15 21:13:50 +0000254 {
255 execution_results = lldb::eExpressionParseError;
Sean Callanan579e70c2016-03-19 00:03:59 +0000256 if (!diagnostic_manager.Diagnostics().size())
257 error.SetExpressionError(execution_results, "expression failed to parse, unknown error");
Jim Ingham151c0322015-09-15 21:13:50 +0000258 else
Sean Callanan579e70c2016-03-19 00:03:59 +0000259 error.SetExpressionError(execution_results, diagnostic_manager.GetString().c_str());
Jim Ingham151c0322015-09-15 21:13:50 +0000260 }
261 else
262 {
Sean Callanan66810412015-10-19 23:11:07 +0000263 // If a pointer to a lldb::ModuleSP was passed in, return the JIT'ed module if one was created
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000264 if (jit_module_sp_ptr)
265 *jit_module_sp_ptr = user_expression_sp->GetJITModule();
Sean Callanan66810412015-10-19 23:11:07 +0000266
Jim Ingham151c0322015-09-15 21:13:50 +0000267 lldb::ExpressionVariableSP expr_result;
268
269 if (execution_policy == eExecutionPolicyNever &&
270 !user_expression_sp->CanInterpret())
271 {
272 if (log)
273 log->Printf("== [UserExpression::Evaluate] Expression may not run, but is not constant ==");
274
Sean Callanan579e70c2016-03-19 00:03:59 +0000275 if (!diagnostic_manager.Diagnostics().size())
276 error.SetExpressionError(lldb::eExpressionSetupError, "expression needed to run but couldn't");
Jim Ingham151c0322015-09-15 21:13:50 +0000277 }
278 else
279 {
280 if (options.InvokeCancelCallback (lldb::eExpressionEvaluationExecution))
281 {
282 error.SetExpressionError (lldb::eExpressionInterrupted, "expression interrupted by callback before execution");
283 result_valobj_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(), error);
284 return lldb::eExpressionInterrupted;
285 }
286
Sean Callanan579e70c2016-03-19 00:03:59 +0000287 diagnostic_manager.Clear();
Jim Ingham151c0322015-09-15 21:13:50 +0000288
289 if (log)
290 log->Printf("== [UserExpression::Evaluate] Executing expression ==");
291
Sean Callanan579e70c2016-03-19 00:03:59 +0000292 execution_results =
293 user_expression_sp->Execute(diagnostic_manager, exe_ctx, options, user_expression_sp, expr_result);
Jim Ingham151c0322015-09-15 21:13:50 +0000294
295 if (options.GetResultIsInternal() && expr_result && process)
296 {
Sean Callananb92bd752015-10-01 16:28:02 +0000297 process->GetTarget().GetPersistentExpressionStateForLanguage(language)->RemovePersistentVariable (expr_result);
Jim Ingham151c0322015-09-15 21:13:50 +0000298 }
299
300 if (execution_results != lldb::eExpressionCompleted)
301 {
302 if (log)
303 log->Printf("== [UserExpression::Evaluate] Execution completed abnormally ==");
304
Sean Callanan579e70c2016-03-19 00:03:59 +0000305 if (!diagnostic_manager.Diagnostics().size())
306 error.SetExpressionError(execution_results, "expression failed to execute, unknown error");
Jim Ingham151c0322015-09-15 21:13:50 +0000307 else
Sean Callanan579e70c2016-03-19 00:03:59 +0000308 error.SetExpressionError(execution_results, diagnostic_manager.GetString().c_str());
Jim Ingham151c0322015-09-15 21:13:50 +0000309 }
310 else
311 {
312 if (expr_result)
313 {
314 result_valobj_sp = expr_result->GetValueObject();
315
316 if (log)
317 log->Printf("== [UserExpression::Evaluate] Execution completed normally with result %s ==",
318 result_valobj_sp->GetValueAsCString());
319 }
320 else
321 {
322 if (log)
323 log->Printf("== [UserExpression::Evaluate] Execution completed normally with no result ==");
324
325 error.SetError(UserExpression::kNoResult, lldb::eErrorTypeGeneric);
326 }
327 }
328 }
329 }
330
331 if (options.InvokeCancelCallback(lldb::eExpressionEvaluationComplete))
332 {
333 error.SetExpressionError (lldb::eExpressionInterrupted, "expression interrupted by callback after complete");
334 return lldb::eExpressionInterrupted;
335 }
336
337 if (result_valobj_sp.get() == NULL)
338 {
339 result_valobj_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(), error);
340 }
341
342 return execution_results;
343}