blob: 0b969806280e743246ceae820eb14a0299762e6e [file] [log] [blame]
Ryan Brown998c8a1c12015-11-02 19:30:40 +00001//===-- LLVMUserExpression.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// C Includes
11// C++ Includes
12
13// Project includes
14#include "lldb/Expression/LLVMUserExpression.h"
15#include "lldb/Core/ConstString.h"
16#include "lldb/Core/Log.h"
17#include "lldb/Core/Module.h"
18#include "lldb/Core/StreamFile.h"
19#include "lldb/Core/StreamString.h"
20#include "lldb/Core/ValueObjectConstResult.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000021#include "lldb/Expression/DiagnosticManager.h"
Ryan Brown998c8a1c12015-11-02 19:30:40 +000022#include "lldb/Expression/ExpressionSourceCode.h"
23#include "lldb/Expression/IRExecutionUnit.h"
24#include "lldb/Expression/IRInterpreter.h"
25#include "lldb/Expression/Materializer.h"
26#include "lldb/Host/HostInfo.h"
27#include "lldb/Symbol/Block.h"
28#include "lldb/Symbol/ClangASTContext.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000029#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Ryan Brown998c8a1c12015-11-02 19:30:40 +000030#include "lldb/Symbol/Function.h"
31#include "lldb/Symbol/ObjectFile.h"
32#include "lldb/Symbol/SymbolVendor.h"
33#include "lldb/Symbol/Type.h"
Ryan Brown998c8a1c12015-11-02 19:30:40 +000034#include "lldb/Symbol/VariableList.h"
35#include "lldb/Target/ExecutionContext.h"
36#include "lldb/Target/Process.h"
37#include "lldb/Target/StackFrame.h"
38#include "lldb/Target/Target.h"
39#include "lldb/Target/ThreadPlan.h"
40#include "lldb/Target/ThreadPlanCallUserExpression.h"
41
42using namespace lldb_private;
43
Jim Ingham19a63fc2015-11-03 02:11:24 +000044LLVMUserExpression::LLVMUserExpression(ExecutionContextScope &exe_scope,
45 const char *expr,
46 const char *expr_prefix,
47 lldb::LanguageType language,
48 ResultType desired_type,
49 const EvaluateExpressionOptions &options)
50 : UserExpression(exe_scope, expr, expr_prefix, language, desired_type, options),
Ryan Brown998c8a1c12015-11-02 19:30:40 +000051 m_stack_frame_bottom(LLDB_INVALID_ADDRESS),
52 m_stack_frame_top(LLDB_INVALID_ADDRESS),
53 m_transformed_text(),
54 m_execution_unit_sp(),
55 m_materializer_ap(),
56 m_jit_module_wp(),
57 m_enforce_valid_object(true),
58 m_in_cplusplus_method(false),
59 m_in_objectivec_method(false),
60 m_in_static_method(false),
61 m_needs_object_ptr(false),
Ryan Brown998c8a1c12015-11-02 19:30:40 +000062 m_target(NULL),
63 m_can_interpret(false),
64 m_materialized_address(LLDB_INVALID_ADDRESS)
65{
66}
67
68LLVMUserExpression::~LLVMUserExpression()
69{
70 if (m_target)
71 {
72 lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock());
73 if (jit_module_sp)
74 m_target->GetImages().Remove(jit_module_sp);
75 }
76}
77
78lldb::ExpressionResults
Jim Inghamff7ac6a2016-04-12 17:17:35 +000079LLVMUserExpression::DoExecute(DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
80 const EvaluateExpressionOptions &options, lldb::UserExpressionSP &shared_ptr_to_me,
81 lldb::ExpressionVariableSP &result)
Ryan Brown998c8a1c12015-11-02 19:30:40 +000082{
83 // The expression log is quite verbose, and if you're just tracking the execution of the
84 // expression, it's quite convenient to have these logs come out with the STEP log as well.
85 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
86
87 if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret)
88 {
89 lldb::addr_t struct_address = LLDB_INVALID_ADDRESS;
90
Sean Callanan579e70c2016-03-19 00:03:59 +000091 if (!PrepareToExecuteJITExpression(diagnostic_manager, exe_ctx, struct_address))
Ryan Brown998c8a1c12015-11-02 19:30:40 +000092 {
Sean Callanan579e70c2016-03-19 00:03:59 +000093 diagnostic_manager.Printf(eDiagnosticSeverityError,
94 "errored out in %s, couldn't PrepareToExecuteJITExpression", __FUNCTION__);
Ryan Brown998c8a1c12015-11-02 19:30:40 +000095 return lldb::eExpressionSetupError;
96 }
97
98 lldb::addr_t function_stack_bottom = LLDB_INVALID_ADDRESS;
99 lldb::addr_t function_stack_top = LLDB_INVALID_ADDRESS;
100
101 if (m_can_interpret)
102 {
103 llvm::Module *module = m_execution_unit_sp->GetModule();
104 llvm::Function *function = m_execution_unit_sp->GetFunction();
105
106 if (!module || !function)
107 {
Sean Callanan579e70c2016-03-19 00:03:59 +0000108 diagnostic_manager.PutCString(eDiagnosticSeverityError, "supposed to interpret, but nothing is there");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000109 return lldb::eExpressionSetupError;
110 }
111
112 Error interpreter_error;
113
114 std::vector<lldb::addr_t> args;
115
Sean Callanan579e70c2016-03-19 00:03:59 +0000116 if (!AddArguments(exe_ctx, args, struct_address, diagnostic_manager))
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000117 {
Sean Callanan579e70c2016-03-19 00:03:59 +0000118 diagnostic_manager.Printf(eDiagnosticSeverityError, "errored out in %s, couldn't AddArguments",
119 __FUNCTION__);
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000120 return lldb::eExpressionSetupError;
121 }
122
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000123 function_stack_bottom = m_stack_frame_bottom;
124 function_stack_top = m_stack_frame_top;
125
126 IRInterpreter::Interpret(*module, *function, args, *m_execution_unit_sp.get(), interpreter_error,
Ted Woodward7071c5fd2016-03-01 21:53:26 +0000127 function_stack_bottom, function_stack_top, exe_ctx);
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000128
129 if (!interpreter_error.Success())
130 {
Sean Callanan579e70c2016-03-19 00:03:59 +0000131 diagnostic_manager.Printf(eDiagnosticSeverityError, "supposed to interpret, but failed: %s",
132 interpreter_error.AsCString());
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000133 return lldb::eExpressionDiscarded;
134 }
135 }
136 else
137 {
138 if (!exe_ctx.HasThreadScope())
139 {
Sean Callanan579e70c2016-03-19 00:03:59 +0000140 diagnostic_manager.Printf(eDiagnosticSeverityError, "%s called with no thread selected", __FUNCTION__);
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000141 return lldb::eExpressionSetupError;
142 }
143
144 Address wrapper_address(m_jit_start_addr);
145
146 std::vector<lldb::addr_t> args;
147
Sean Callanan579e70c2016-03-19 00:03:59 +0000148 if (!AddArguments(exe_ctx, args, struct_address, diagnostic_manager))
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000149 {
Sean Callanan579e70c2016-03-19 00:03:59 +0000150 diagnostic_manager.Printf(eDiagnosticSeverityError, "errored out in %s, couldn't AddArguments",
151 __FUNCTION__);
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000152 return lldb::eExpressionSetupError;
153 }
154
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000155 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression(exe_ctx.GetThreadRef(), wrapper_address,
156 args, options, shared_ptr_to_me));
157
Sean Callanan579e70c2016-03-19 00:03:59 +0000158 StreamString ss;
159 if (!call_plan_sp || !call_plan_sp->ValidatePlan(&ss))
160 {
161 diagnostic_manager.PutCString(eDiagnosticSeverityError, ss.GetData());
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000162 return lldb::eExpressionSetupError;
Sean Callanan579e70c2016-03-19 00:03:59 +0000163 }
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000164
165 ThreadPlanCallUserExpression *user_expression_plan =
166 static_cast<ThreadPlanCallUserExpression *>(call_plan_sp.get());
167
168 lldb::addr_t function_stack_pointer = user_expression_plan->GetFunctionStackPointer();
169
170 function_stack_bottom = function_stack_pointer - HostInfo::GetPageSize();
171 function_stack_top = function_stack_pointer;
172
173 if (log)
174 log->Printf("-- [UserExpression::Execute] Execution of expression begins --");
175
176 if (exe_ctx.GetProcessPtr())
177 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
178
179 lldb::ExpressionResults execution_result =
Sean Callanan579e70c2016-03-19 00:03:59 +0000180 exe_ctx.GetProcessRef().RunThreadPlan(exe_ctx, call_plan_sp, options, diagnostic_manager);
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000181
182 if (exe_ctx.GetProcessPtr())
183 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
184
185 if (log)
186 log->Printf("-- [UserExpression::Execute] Execution of expression completed --");
187
188 if (execution_result == lldb::eExpressionInterrupted || execution_result == lldb::eExpressionHitBreakpoint)
189 {
190 const char *error_desc = NULL;
191
192 if (call_plan_sp)
193 {
194 lldb::StopInfoSP real_stop_info_sp = call_plan_sp->GetRealStopInfo();
195 if (real_stop_info_sp)
196 error_desc = real_stop_info_sp->GetDescription();
197 }
198 if (error_desc)
Sean Callanan579e70c2016-03-19 00:03:59 +0000199 diagnostic_manager.Printf(eDiagnosticSeverityError, "Execution was interrupted, reason: %s.",
200 error_desc);
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000201 else
Sean Callanan579e70c2016-03-19 00:03:59 +0000202 diagnostic_manager.PutCString(eDiagnosticSeverityError, "Execution was interrupted.");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000203
204 if ((execution_result == lldb::eExpressionInterrupted && options.DoesUnwindOnError()) ||
205 (execution_result == lldb::eExpressionHitBreakpoint && options.DoesIgnoreBreakpoints()))
Sean Callanan579e70c2016-03-19 00:03:59 +0000206 diagnostic_manager.AppendMessageToDiagnostic(
207 "The process has been returned to the state before expression evaluation.");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000208 else
209 {
210 if (execution_result == lldb::eExpressionHitBreakpoint)
211 user_expression_plan->TransferExpressionOwnership();
Sean Callanan579e70c2016-03-19 00:03:59 +0000212 diagnostic_manager.AppendMessageToDiagnostic(
213 "The process has been left at the point where it was interrupted, "
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000214 "use \"thread return -x\" to return to the state before expression evaluation.");
215 }
216
217 return execution_result;
218 }
219 else if (execution_result == lldb::eExpressionStoppedForDebug)
220 {
Sean Callanan579e70c2016-03-19 00:03:59 +0000221 diagnostic_manager.PutCString(
222 eDiagnosticSeverityRemark,
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000223 "Execution was halted at the first instruction of the expression "
224 "function because \"debug\" was requested.\n"
225 "Use \"thread return -x\" to return to the state before expression evaluation.");
226 return execution_result;
227 }
228 else if (execution_result != lldb::eExpressionCompleted)
229 {
Sean Callanan579e70c2016-03-19 00:03:59 +0000230 diagnostic_manager.Printf(eDiagnosticSeverityError, "Couldn't execute function; result was %s",
231 Process::ExecutionResultAsCString(execution_result));
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000232 return execution_result;
233 }
234 }
235
Sean Callanan579e70c2016-03-19 00:03:59 +0000236 if (FinalizeJITExecution(diagnostic_manager, exe_ctx, result, function_stack_bottom, function_stack_top))
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000237 {
238 return lldb::eExpressionCompleted;
239 }
240 else
241 {
242 return lldb::eExpressionResultUnavailable;
243 }
244 }
245 else
246 {
Sean Callanan579e70c2016-03-19 00:03:59 +0000247 diagnostic_manager.PutCString(eDiagnosticSeverityError,
248 "Expression can't be run, because there is no JIT compiled function");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000249 return lldb::eExpressionSetupError;
250 }
251}
252
253bool
Sean Callanan579e70c2016-03-19 00:03:59 +0000254LLVMUserExpression::FinalizeJITExecution(DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000255 lldb::ExpressionVariableSP &result, lldb::addr_t function_stack_bottom,
256 lldb::addr_t function_stack_top)
257{
258 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
259
260 if (log)
261 log->Printf("-- [UserExpression::FinalizeJITExecution] Dematerializing after execution --");
262
263 if (!m_dematerializer_sp)
264 {
Sean Callanan579e70c2016-03-19 00:03:59 +0000265 diagnostic_manager.Printf(eDiagnosticSeverityError,
266 "Couldn't apply expression side effects : no dematerializer is present");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000267 return false;
268 }
269
270 Error dematerialize_error;
271
272 m_dematerializer_sp->Dematerialize(dematerialize_error, function_stack_bottom, function_stack_top);
273
274 if (!dematerialize_error.Success())
275 {
Sean Callanan579e70c2016-03-19 00:03:59 +0000276 diagnostic_manager.Printf(eDiagnosticSeverityError, "Couldn't apply expression side effects : %s",
277 dematerialize_error.AsCString("unknown error"));
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000278 return false;
279 }
280
281 result = GetResultAfterDematerialization(exe_ctx.GetBestExecutionContextScope());
282
283 if (result)
284 result->TransferAddress();
285
286 m_dematerializer_sp.reset();
287
288 return true;
289}
290
291bool
Sean Callanan579e70c2016-03-19 00:03:59 +0000292LLVMUserExpression::PrepareToExecuteJITExpression(DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000293 lldb::addr_t &struct_address)
294{
295 lldb::TargetSP target;
296 lldb::ProcessSP process;
297 lldb::StackFrameSP frame;
298
299 if (!LockAndCheckContext(exe_ctx, target, process, frame))
300 {
Sean Callanan579e70c2016-03-19 00:03:59 +0000301 diagnostic_manager.PutCString(eDiagnosticSeverityError,
302 "The context has changed before we could JIT the expression!");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000303 return false;
304 }
305
306 if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret)
307 {
308 if (m_materialized_address == LLDB_INVALID_ADDRESS)
309 {
310 Error alloc_error;
311
312 IRMemoryMap::AllocationPolicy policy =
313 m_can_interpret ? IRMemoryMap::eAllocationPolicyHostOnly : IRMemoryMap::eAllocationPolicyMirror;
314
Jim Ingham2c381412015-11-04 20:32:27 +0000315 const bool zero_memory = false;
316
317 m_materialized_address = m_execution_unit_sp->Malloc(m_materializer_ap->GetStructByteSize(),
318 m_materializer_ap->GetStructAlignment(),
319 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
320 policy,
321 zero_memory,
322 alloc_error);
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000323
324 if (!alloc_error.Success())
325 {
Sean Callanan579e70c2016-03-19 00:03:59 +0000326 diagnostic_manager.Printf(eDiagnosticSeverityError,
327 "Couldn't allocate space for materialized struct: %s",
328 alloc_error.AsCString());
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000329 return false;
330 }
331 }
332
333 struct_address = m_materialized_address;
334
335 if (m_can_interpret && m_stack_frame_bottom == LLDB_INVALID_ADDRESS)
336 {
337 Error alloc_error;
338
339 const size_t stack_frame_size = 512 * 1024;
340
Jim Ingham2c381412015-11-04 20:32:27 +0000341 const bool zero_memory = false;
342
343 m_stack_frame_bottom = m_execution_unit_sp->Malloc(stack_frame_size,
344 8,
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000345 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
Jim Ingham2c381412015-11-04 20:32:27 +0000346 IRMemoryMap::eAllocationPolicyHostOnly,
347 zero_memory,
348 alloc_error);
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000349
350 m_stack_frame_top = m_stack_frame_bottom + stack_frame_size;
351
352 if (!alloc_error.Success())
353 {
Sean Callanan579e70c2016-03-19 00:03:59 +0000354 diagnostic_manager.Printf(eDiagnosticSeverityError, "Couldn't allocate space for the stack frame: %s",
355 alloc_error.AsCString());
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000356 return false;
357 }
358 }
359
360 Error materialize_error;
361
362 m_dematerializer_sp =
363 m_materializer_ap->Materialize(frame, *m_execution_unit_sp, struct_address, materialize_error);
364
365 if (!materialize_error.Success())
366 {
Sean Callanan579e70c2016-03-19 00:03:59 +0000367 diagnostic_manager.Printf(eDiagnosticSeverityError, "Couldn't materialize: %s",
368 materialize_error.AsCString());
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000369 return false;
370 }
371 }
372 return true;
373}
374
375lldb::ModuleSP
376LLVMUserExpression::GetJITModule()
377{
378 if (m_execution_unit_sp)
379 return m_execution_unit_sp->GetJITModule();
380 return lldb::ModuleSP();
381}